Initial API

master
Dessa Simpson 2020-07-01 23:06:14 -07:00
parent ce518aeeb5
commit b7d8f14eb4
3 changed files with 140 additions and 3 deletions

View File

@ -1,2 +1,5 @@
CREATE VIEW requests_vw AS
SELECT * FROM requests WHERE state = 'Requested' ORDER BY score DESC;
SELECT * FROM requests WHERE state = 'Requested' ORDER BY score DESC, id DESC;
CREATE VIEW requests_all_vw AS
SELECT * FROM requests ORDER BY id DESC;

View File

@ -1,12 +1,72 @@
import * as config from "./config";
import * as requests from "./requests";
import { QueryResult } from "pg";
import express from "express";
import db from "./db";
const app = express();
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.get("/test", (request, response) => response.send("Test"))
app.get("/api/getRequests", async (request, response) => {
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
requests.getRequests(requestCount).then((val: QueryResult) => response.send(val))
.catch((e: any) => console.error(e.stack));
});
app.get("/api/getAllRequests", async (request, response) => {
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
requests.getAllRequests(requestCount).then((val: QueryResult) => response.send(val))
.catch((e: any) => console.error(e.stack));
});
app.post("/api/addRequest", async (request, response) => {
response.type('text/plain');
if (!request.body.url) {
response.status(400);
response.send("Missing url");
return
}
if (!request.body.requester) {
response.status(400);
response.send("Missing requester");
return
}
var url = request.body.url as string;
var requester = request.body.requester as string;
requests.addRequest(url,requester).then((val: string) => response.send(val))
.catch((e: any) => console.error(e.stack));
});
app.post("/api/updateRequestScore", async (request, response) => {
response.type('text/plain');
if (!request.body.url) {
response.status(400);
response.send("Missing url");
return
}
if (!request.body.scoreDiff) {
response.status(400);
response.send("Missing scoreDiff");
return
}
var url = request.body.url as string;
var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
requests.updateRequestScore(url,scoreDiff).then((val: string) => response.send(val))
.catch((e: any) => console.error(e.stack));
});
app.post("/api/deleteRequest", async (request, response) => {
response.type('text/plain');
if (!request.body.url) {
response.status(400);
response.send("Missing url");
return
}
var url = request.body.url as string;
requests.deleteRequest(url).then((val: string) => response.send(val))
.catch((e: any) => console.error(e.stack));
});
const server = app.listen(config.port, () => {
console.log(`Listening on port ${config.port}`);

74
src/requests.ts Normal file
View File

@ -0,0 +1,74 @@
import express from "express";
import pg from "pg";
import db from "./db";
// getRequests
const getRequestsQuery = {
name: "getRequests",
text: "SELECT * FROM requests_vw LIMIT $1"
}
export async function getRequests(count: number) {
var query = Object.assign(getRequestsQuery, { values: [count] });
return db.query(query)
.then((result: pg.QueryResult) => result.rows);
};
// getAllRequests
const getAllRequestsQuery = {
name: "getAllRequests",
text: "SELECT * FROM requests_all_vw LIMIT $1"
}
export async function getAllRequests(count: number) {
var query = Object.assign(getAllRequestsQuery, { values: [count] });
return db.query(query)
.then((result: pg.QueryResult) => result.rows);
};
// addRequest
const checkRequestExistsQuery = {
name: "checkRequestExists",
text: "SELECT * FROM requests WHERE url = $1"
}
const addRequestQuery = {
name: "addRequest",
text: "INSERT INTO requests (url,requester) VALUES ($1,$2)"
}
export async function addRequest(url: string, requester: string) {
var query = Object.assign(checkRequestExistsQuery, { values: [url] });
var result = await db.query(query);
if (result.rowCount > 0) {
return `Song already requested by ${result.rows[0].requester}. State: ${result.rows[0].state}`
}
var query = Object.assign(addRequestQuery, { values: [url,requester] });
return db.query(query)
.then((result: pg.QueryResult) => "Song request added.");
};
// updateRequestScore
const updateRequestScoreQuery = {
name: "updateRequestScore",
text: "UPDATE requests SET score = score + $2 WHERE url = $1"
}
export async function updateRequestScore(url: string, scoreDiff: number) {
var query = Object.assign(updateRequestScoreQuery, { values: [url,scoreDiff] });
return db.query(query)
.then((result: pg.QueryResult) => "Song request score updated.");
};
// deleteRequest
const deleteRequestQuery = {
name: "deleteRequest",
text: "DELETE FROM requests WHERE url = $1"
}
export async function deleteRequest(url: string) {
var query = Object.assign(deleteRequestQuery, { values: [url] });
return db.query(query)
.then((result: pg.QueryResult) => "Song request deleted.");
};