Add sorting to API

master
Dessa Simpson 2021-02-26 23:20:49 -07:00
parent b968f054d5
commit 28d5ce09dd
3 changed files with 65 additions and 39 deletions

View File

@ -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<any>) => 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<any>) => 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<any>) => 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<any>) => response.send({
total: requestsTotal,
requests: val

View File

@ -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"

View File

@ -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);
};