From 28d5ce09ddf5220e624fe2ba4521f73bfcf2e595 Mon Sep 17 00:00:00 2001 From: Dessa Simpson Date: Fri, 26 Feb 2021 23:20:49 -0700 Subject: [PATCH] Add sorting to API --- src/app.ts | 43 +++++++++++++++++++++++++++++++++++++++---- src/queries.ts | 27 --------------------------- src/requests.ts | 34 ++++++++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 39 deletions(-) diff --git a/src/app.ts b/src/app.ts index efaa358..ee577a3 100644 --- a/src/app.ts +++ b/src/app.ts @@ -46,16 +46,34 @@ app.get("/api/getRequests", async (request, response) => { await validateApiToken(request.session); var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); var requestOffset = ( request.query.offset ? parseInt(request.query.offset as string, 10) : 0 ); + var sortDirection = ( request.query.sortDirection == "asc" ? "ASC" : "DESC" ); + switch (request.query.sort) { + case undefined: + case "score": + var requestSort = `score ${sortDirection}, reqTimestamp ASC`; + break; + case "timestamp": + var requestSort = `reqTimestamp ${sortDirection}`; + break; + case "alpha": + var requestSort = `title ${sortDirection}` + break; + default: + response.status(400); + response.send("Invalid sort"); + return; + }; + console.log('foo') var requestsTotal = await requests.getRequestsTotal(); if (request.session.user) { - requests.getRequestsVoted(requestCount,requestOffset,request.session.user.id) + requests.getRequestsVoted(requestCount,requestOffset,requestSort,request.session.user.id) .then((val: Array) => response.send({ total: requestsTotal, requests: val })) .catch((e: any) => errorHandler(request,response,e)); } else { - requests.getRequests(requestCount,requestOffset) + requests.getRequests(requestCount,requestOffset,requestSort) .then((val: Array) => response.send({ total: requestsTotal, requests: val @@ -69,16 +87,33 @@ app.get("/api/getAllRequests", async (request, response) => { await validateApiToken(request.session); var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); var requestOffset = ( request.query.offset ? parseInt(request.query.offset as string, 10) : 0 ); + var sortDirection = ( request.query.sortDirection == "asc" ? "ASC" : "DESC" ); + switch (request.query.sort) { + case undefined: + case "score": + var requestSort = `score ${sortDirection}, reqTimestamp ASC`; + break; + case "timestamp": + var requestSort = `reqTimestamp ${sortDirection}`; + break; + case "alpha": + var requestSort = `title ${sortDirection}` + break; + default: + response.status(400); + response.send("Invalid sort"); + return; + } var requestsTotal = await requests.getAllRequestsTotal(); if (request.session.user) { - requests.getAllRequestsVoted(requestCount,requestOffset,request.session.user.id) + requests.getAllRequestsVoted(requestCount,requestOffset,requestSort,request.session.user.id) .then((val: Array) => response.send({ total: requestsTotal, requests: val })) .catch((e: any) => errorHandler(request,response,e)); } else { - requests.getAllRequests(requestCount,requestOffset) + requests.getAllRequests(requestCount,requestOffset,requestSort) .then((val: Array) => response.send({ total: requestsTotal, requests: val diff --git a/src/queries.ts b/src/queries.ts index b30e482..c61a6b2 100644 --- a/src/queries.ts +++ b/src/queries.ts @@ -52,39 +52,12 @@ export const updateVotePoints = { text: "CALL update_vote_points($1,$2,$3)" } -// Request-related queries -export const getRequests = { - name: "getRequests", - text: "SELECT * FROM requests_vw \ - JOIN states ON requests_vw.state = states.state WHERE active \ - ORDER BY score DESC, reqTimestamp ASC LIMIT $1 OFFSET $2" -} - -export const getRequestsVoted = { - name: "getRequestsVoted", - text: "SELECT * FROM get_requests_voted($3) \ - JOIN states ON get_requests_voted.state = states.state WHERE active \ - ORDER BY score DESC, reqTimestamp ASC LIMIT $1 OFFSET $2" -} - export const getRequestsTotal = { name: "getRequestsTotal", text: "SELECT COUNT(*) FROM requests_vw \ JOIN states ON requests_vw.state = states.state WHERE active" } -export const getAllRequests = { - name: "getAllRequests", - text: "SELECT * FROM requests_vw \ - ORDER BY score DESC, reqTimestamp ASC LIMIT $1 OFFSET $2" -} - -export const getAllRequestsVoted = { - name: "getAllRequestsVoted", - text: "SELECT * FROM get_requests_voted($3) \ - ORDER BY score DESC, reqTimestamp ASC LIMIT $1 OFFSET $2" -} - export const getAllRequestsTotal = { name: "getAllRequestsTotal", text: "SELECT COUNT(*) FROM requests_vw" diff --git a/src/requests.ts b/src/requests.ts index ef3e1e4..0ee4b43 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -4,14 +4,24 @@ import { log, LogLevel } from "./logging" import pg from "pg"; import db from "./db"; -export async function getRequests(count: number, offset: number) { - var query = Object.assign(queries.getRequests, { values: [count,offset] }); +export async function getRequests(count: number, offset: number, sort: string) { + var query = { + text: "SELECT * FROM requests_vw \ + JOIN states ON requests_vw.state = states.state WHERE active \ + ORDER BY " + sort + " LIMIT $1 OFFSET $2", + values: [count,offset] + }; return db.query(query) .then((result: pg.QueryResult) => result.rows); }; -export async function getRequestsVoted(count: number, offset: number, user: number) { - var query = Object.assign(queries.getRequestsVoted, { values: [count,offset,user] }); +export async function getRequestsVoted(count: number, offset: number, sort: string, user: number) { + var query = { + text: "SELECT * FROM get_requests_voted($3) \ + JOIN states ON get_requests_voted.state = states.state WHERE active \ + ORDER BY " + sort + " LIMIT $1 OFFSET $2", + values: [count,offset,user] + }; return db.query(query) .then((result: pg.QueryResult) => result.rows); }; @@ -21,14 +31,22 @@ export async function getRequestsTotal() { .then((result: pg.QueryResult) => result.rows[0]["count"]); }; -export async function getAllRequests(count: number, offset: number) { - var query = Object.assign(queries.getAllRequests, { values: [count,offset] }); +export async function getAllRequests(count: number, offset: number, sort: string) { + var query = { + text: "SELECT * FROM requests_vw \ + ORDER BY " + sort + " LIMIT $1 OFFSET $2", + values: [count,offset] + }; return db.query(query) .then((result: pg.QueryResult) => result.rows); }; -export async function getAllRequestsVoted(count: number, offset: number, user: number) { - var query = Object.assign(queries.getAllRequestsVoted, { values: [count,offset,user] }); +export async function getAllRequestsVoted(count: number, offset: number, sort: string, user: number) { + var query = { + text: "SELECT * FROM get_requests_voted($3) \ + ORDER BY " + sort + " LIMIT $1 OFFSET $2", + values: [count,offset,user] + }; return db.query(query) .then((result: pg.QueryResult) => result.rows); };