aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-10-19 09:43:01 +0200
committerChocobozzz <florian.bigard@gmail.com>2017-10-19 09:43:01 +0200
commit8a02bd0433b7101c5ea36e87a4edb63204d2adec (patch)
treed7ab4b6164aef752c216bd2f22f8b3b270a724b8 /server/models
parent9fd540562c356cb54b98861d2d9e7d4fbfcd00e0 (diff)
downloadPeerTube-8a02bd0433b7101c5ea36e87a4edb63204d2adec.tar.gz
PeerTube-8a02bd0433b7101c5ea36e87a4edb63204d2adec.tar.zst
PeerTube-8a02bd0433b7101c5ea36e87a4edb63204d2adec.zip
Add pod list endpoint with pagination, sort...
Diffstat (limited to 'server/models')
-rw-r--r--server/models/pod/pod-interface.ts4
-rw-r--r--server/models/pod/pod.ts19
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
5import { Pod as FormattedPod } from '../../../shared/models/pods/pod.model' 5import { Pod as FormattedPod } from '../../../shared/models/pods/pod.model'
6import { ResultList } from '../../../shared/models/result-list.model'
6 7
7export namespace PodMethods { 8export 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'
4import { FRIEND_SCORE, PODS_SCORE } from '../../initializers' 4import { FRIEND_SCORE, PODS_SCORE } from '../../initializers'
5import { logger, isHostValid } from '../../helpers' 5import { logger, isHostValid } from '../../helpers'
6 6
7import { addMethodsToModel } from '../utils' 7import { addMethodsToModel, getSort } from '../utils'
8import { 8import {
9 PodInstance, 9 PodInstance,
10 PodAttributes, 10 PodAttributes,
@@ -17,6 +17,7 @@ let toFormattedJSON: PodMethods.ToFormattedJSON
17let countAll: PodMethods.CountAll 17let countAll: PodMethods.CountAll
18let incrementScores: PodMethods.IncrementScores 18let incrementScores: PodMethods.IncrementScores
19let list: PodMethods.List 19let list: PodMethods.List
20let listForApi: PodMethods.ListForApi
20let listAllIds: PodMethods.ListAllIds 21let listAllIds: PodMethods.ListAllIds
21let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest 22let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest
22let listBadPods: PodMethods.ListBadPods 23let 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
147listForApi = 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
145listAllIds = function (transaction: Sequelize.Transaction) { 162listAllIds = function (transaction: Sequelize.Transaction) {
146 const query = { 163 const query = {
147 attributes: [ 'id' ], 164 attributes: [ 'id' ],