diff options
Diffstat (limited to 'server/models/pod')
-rw-r--r-- | server/models/pod/pod-interface.ts | 4 | ||||
-rw-r--r-- | server/models/pod/pod.ts | 19 |
2 files changed, 22 insertions, 1 deletions
diff --git a/server/models/pod/pod-interface.ts b/server/models/pod/pod-interface.ts index fc763acac..7e095d424 100644 --- a/server/models/pod/pod-interface.ts +++ b/server/models/pod/pod-interface.ts | |||
@@ -3,6 +3,7 @@ import * as Promise from 'bluebird' | |||
3 | 3 | ||
4 | // Don't use barrel, import just what we need | 4 | // Don't use barrel, import just what we need |
5 | import { Pod as FormattedPod } from '../../../shared/models/pods/pod.model' | 5 | import { Pod as FormattedPod } from '../../../shared/models/pods/pod.model' |
6 | import { ResultList } from '../../../shared/models/result-list.model' | ||
6 | 7 | ||
7 | export namespace PodMethods { | 8 | export namespace PodMethods { |
8 | export type ToFormattedJSON = (this: PodInstance) => FormattedPod | 9 | export type ToFormattedJSON = (this: PodInstance) => FormattedPod |
@@ -13,6 +14,8 @@ export namespace PodMethods { | |||
13 | 14 | ||
14 | export type List = () => Promise<PodInstance[]> | 15 | export type List = () => Promise<PodInstance[]> |
15 | 16 | ||
17 | export type ListForApi = (start: number, count: number, sort: string) => Promise< ResultList<PodInstance> > | ||
18 | |||
16 | export type ListAllIds = (transaction: Sequelize.Transaction) => Promise<number[]> | 19 | export type ListAllIds = (transaction: Sequelize.Transaction) => Promise<number[]> |
17 | 20 | ||
18 | export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string) => Promise<number[]> | 21 | export type ListRandomPodIdsWithRequest = (limit: number, tableWithPods: string, tableWithPodsJoins: string) => Promise<number[]> |
@@ -32,6 +35,7 @@ export interface PodClass { | |||
32 | countAll: PodMethods.CountAll | 35 | countAll: PodMethods.CountAll |
33 | incrementScores: PodMethods.IncrementScores | 36 | incrementScores: PodMethods.IncrementScores |
34 | list: PodMethods.List | 37 | list: PodMethods.List |
38 | listForApi: PodMethods.ListForApi | ||
35 | listAllIds: PodMethods.ListAllIds | 39 | listAllIds: PodMethods.ListAllIds |
36 | listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest | 40 | listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest |
37 | listBadPods: PodMethods.ListBadPods | 41 | listBadPods: PodMethods.ListBadPods |
diff --git a/server/models/pod/pod.ts b/server/models/pod/pod.ts index 1440ac9b4..e4d7db48a 100644 --- a/server/models/pod/pod.ts +++ b/server/models/pod/pod.ts | |||
@@ -4,7 +4,7 @@ import * as Sequelize from 'sequelize' | |||
4 | import { FRIEND_SCORE, PODS_SCORE } from '../../initializers' | 4 | import { FRIEND_SCORE, PODS_SCORE } from '../../initializers' |
5 | import { logger, isHostValid } from '../../helpers' | 5 | import { logger, isHostValid } from '../../helpers' |
6 | 6 | ||
7 | import { addMethodsToModel } from '../utils' | 7 | import { addMethodsToModel, getSort } from '../utils' |
8 | import { | 8 | import { |
9 | PodInstance, | 9 | PodInstance, |
10 | PodAttributes, | 10 | PodAttributes, |
@@ -17,6 +17,7 @@ let toFormattedJSON: PodMethods.ToFormattedJSON | |||
17 | let countAll: PodMethods.CountAll | 17 | let countAll: PodMethods.CountAll |
18 | let incrementScores: PodMethods.IncrementScores | 18 | let incrementScores: PodMethods.IncrementScores |
19 | let list: PodMethods.List | 19 | let list: PodMethods.List |
20 | let listForApi: PodMethods.ListForApi | ||
20 | let listAllIds: PodMethods.ListAllIds | 21 | let listAllIds: PodMethods.ListAllIds |
21 | let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest | 22 | let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest |
22 | let listBadPods: PodMethods.ListBadPods | 23 | let listBadPods: PodMethods.ListBadPods |
@@ -78,6 +79,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da | |||
78 | countAll, | 79 | countAll, |
79 | incrementScores, | 80 | incrementScores, |
80 | list, | 81 | list, |
82 | listForApi, | ||
81 | listAllIds, | 83 | listAllIds, |
82 | listRandomPodIdsWithRequest, | 84 | listRandomPodIdsWithRequest, |
83 | listBadPods, | 85 | listBadPods, |
@@ -142,6 +144,21 @@ list = function () { | |||
142 | return Pod.findAll() | 144 | return Pod.findAll() |
143 | } | 145 | } |
144 | 146 | ||
147 | listForApi = function (start: number, count: number, sort: string) { | ||
148 | const query = { | ||
149 | offset: start, | ||
150 | limit: count, | ||
151 | order: [ getSort(sort) ] | ||
152 | } | ||
153 | |||
154 | return Pod.findAndCountAll(query).then(({ rows, count }) => { | ||
155 | return { | ||
156 | data: rows, | ||
157 | total: count | ||
158 | } | ||
159 | }) | ||
160 | } | ||
161 | |||
145 | listAllIds = function (transaction: Sequelize.Transaction) { | 162 | listAllIds = function (transaction: Sequelize.Transaction) { |
146 | const query = { | 163 | const query = { |
147 | attributes: [ 'id' ], | 164 | attributes: [ 'id' ], |