From 8a02bd0433b7101c5ea36e87a4edb63204d2adec Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Oct 2017 09:43:01 +0200 Subject: Add pod list endpoint with pagination, sort... --- server/models/pod/pod.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'server/models/pod/pod.ts') 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' import { FRIEND_SCORE, PODS_SCORE } from '../../initializers' import { logger, isHostValid } from '../../helpers' -import { addMethodsToModel } from '../utils' +import { addMethodsToModel, getSort } from '../utils' import { PodInstance, PodAttributes, @@ -17,6 +17,7 @@ let toFormattedJSON: PodMethods.ToFormattedJSON let countAll: PodMethods.CountAll let incrementScores: PodMethods.IncrementScores let list: PodMethods.List +let listForApi: PodMethods.ListForApi let listAllIds: PodMethods.ListAllIds let listRandomPodIdsWithRequest: PodMethods.ListRandomPodIdsWithRequest let listBadPods: PodMethods.ListBadPods @@ -78,6 +79,7 @@ export default function (sequelize: Sequelize.Sequelize, DataTypes: Sequelize.Da countAll, incrementScores, list, + listForApi, listAllIds, listRandomPodIdsWithRequest, listBadPods, @@ -142,6 +144,21 @@ list = function () { return Pod.findAll() } +listForApi = function (start: number, count: number, sort: string) { + const query = { + offset: start, + limit: count, + order: [ getSort(sort) ] + } + + return Pod.findAndCountAll(query).then(({ rows, count }) => { + return { + data: rows, + total: count + } + }) +} + listAllIds = function (transaction: Sequelize.Transaction) { const query = { attributes: [ 'id' ], -- cgit v1.2.3