Consistency and cleanup
parent
567b2bba27
commit
34a9c0d0d1
20
src/app.ts
20
src/app.ts
|
@ -2,12 +2,10 @@ import * as config from "./config";
|
||||||
import * as requests from "./requests";
|
import * as requests from "./requests";
|
||||||
import * as twitch from "./twitch";
|
import * as twitch from "./twitch";
|
||||||
import { URLSearchParams } from "url";
|
import { URLSearchParams } from "url";
|
||||||
import { QueryResult } from "pg";
|
|
||||||
import express from "express";
|
import express from "express";
|
||||||
import session from "express-session";
|
import session from "express-session";
|
||||||
import pgSessionStore from "connect-pg-simple";
|
import pgSessionStore from "connect-pg-simple";
|
||||||
import fetch, { Response as FetchResponse } from "node-fetch";
|
import fetch, { Response as FetchResponse } from "node-fetch";
|
||||||
import * as eta from "eta";
|
|
||||||
import db from "./db";
|
import db from "./db";
|
||||||
import errorHandler from "./errors";
|
import errorHandler from "./errors";
|
||||||
|
|
||||||
|
@ -26,13 +24,13 @@ app.use(session({
|
||||||
// API
|
// API
|
||||||
app.get("/api/getRequests", async (request, response) => {
|
app.get("/api/getRequests", async (request, response) => {
|
||||||
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
||||||
requests.getRequests(requestCount).then((val: QueryResult) => response.send(val))
|
requests.getRequests(requestCount).then((val: Array<any>) => response.send(val))
|
||||||
.catch((e: any) => errorHandler(request,response,e));
|
.catch((e: any) => errorHandler(request,response,e));
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/api/getAllRequests", async (request, response) => {
|
app.get("/api/getAllRequests", async (request, response) => {
|
||||||
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
||||||
requests.getAllRequests(requestCount).then((val: QueryResult) => response.send(val))
|
requests.getAllRequests(requestCount).then((val: Array<any>) => response.send(val))
|
||||||
.catch((e: any) => errorHandler(request,response,e));
|
.catch((e: any) => errorHandler(request,response,e));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -92,7 +90,10 @@ app.post("/api/updateRequestScore", async (request, response) => {
|
||||||
}
|
}
|
||||||
var url = request.body.url as string;
|
var url = request.body.url as string;
|
||||||
var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
|
var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
|
||||||
requests.updateRequestScore(url,scoreDiff).then((val: string) => response.send(val))
|
requests.updateRequestScore(url,scoreDiff).then((val: [number,string]) => {
|
||||||
|
response.status(val[0]);
|
||||||
|
response.send(val[1]);
|
||||||
|
})
|
||||||
.catch((e: any) => errorHandler(request,response,e));
|
.catch((e: any) => errorHandler(request,response,e));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -104,7 +105,10 @@ app.post("/api/deleteRequest", async (request, response) => {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var url = request.body.url as string;
|
var url = request.body.url as string;
|
||||||
requests.deleteRequest(url).then((val: string) => response.send(val))
|
requests.deleteRequest(url).then((val: [number,string]) => {
|
||||||
|
response.status(val[0]);
|
||||||
|
response.send(val[1]);
|
||||||
|
})
|
||||||
.catch((e: any) => errorHandler(request,response,e));
|
.catch((e: any) => errorHandler(request,response,e));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -151,9 +155,9 @@ app.get("/", async (request, response) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
// Logout
|
// Logout
|
||||||
app.get ("/logout", async (request, response) => request.session!.destroy((err) => response.redirect(307, '/')));
|
app.get ("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
|
||||||
|
|
||||||
|
|
||||||
const server = app.listen(config.port, () => {
|
app.listen(config.port, () => {
|
||||||
console.log(`Listening on port ${config.port}`);
|
console.log(`Listening on port ${config.port}`);
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const { Pool } = require('pg');
|
import { Pool } from 'pg';
|
||||||
|
|
||||||
const db = new Pool();
|
const db = new Pool();
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ const addRequestQuery = {
|
||||||
text: "INSERT INTO requests (url,requester) VALUES ($1,$2)"
|
text: "INSERT INTO requests (url,requester) VALUES ($1,$2)"
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function addRequest(url: string, requester: string) {
|
export async function addRequest(url: string, requester: string): Promise<[number,string]> {
|
||||||
var validUrl = false;
|
var validUrl = false;
|
||||||
for (var regex of validUrlRegexes) {
|
for (var regex of validUrlRegexes) {
|
||||||
if (regex.test(url)) {
|
if (regex.test(url)) {
|
||||||
|
@ -56,7 +56,7 @@ export async function addRequest(url: string, requester: string) {
|
||||||
}
|
}
|
||||||
var query = Object.assign(addRequestQuery, { values: [url,requester] });
|
var query = Object.assign(addRequestQuery, { values: [url,requester] });
|
||||||
return db.query(query)
|
return db.query(query)
|
||||||
.then((result: pg.QueryResult) => [201,"Song request added."]);
|
.then(() => [201,"Song request added."] as [number,string]);
|
||||||
};
|
};
|
||||||
|
|
||||||
// updateRequestState
|
// updateRequestState
|
||||||
|
@ -70,7 +70,7 @@ const updateRequestStateQuery = {
|
||||||
text: "UPDATE requests SET state = $2 WHERE url = $1"
|
text: "UPDATE requests SET state = $2 WHERE url = $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateRequestState(url: string, state: string) {
|
export async function updateRequestState(url: string, state: string): Promise<[number,string]> {
|
||||||
var query = Object.assign(checkValidStateQuery, { values: [state] });
|
var query = Object.assign(checkValidStateQuery, { values: [state] });
|
||||||
var result = await db.query(query);
|
var result = await db.query(query);
|
||||||
if (result.rowCount < 1) {
|
if (result.rowCount < 1) {
|
||||||
|
@ -79,7 +79,7 @@ export async function updateRequestState(url: string, state: string) {
|
||||||
|
|
||||||
var query = Object.assign(updateRequestStateQuery, { values: [url,state] });
|
var query = Object.assign(updateRequestStateQuery, { values: [url,state] });
|
||||||
return db.query(query)
|
return db.query(query)
|
||||||
.then((result: pg.QueryResult) => [200,"Song request state updated."]);
|
.then(() => [200,"Song request state updated."] as [number,string]);
|
||||||
};
|
};
|
||||||
|
|
||||||
// updateRequestScore
|
// updateRequestScore
|
||||||
|
@ -88,10 +88,10 @@ const updateRequestScoreQuery = {
|
||||||
text: "UPDATE requests SET score = score + $2 WHERE url = $1"
|
text: "UPDATE requests SET score = score + $2 WHERE url = $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateRequestScore(url: string, scoreDiff: number) {
|
export async function updateRequestScore(url: string, scoreDiff: number): Promise<[number,string]> {
|
||||||
var query = Object.assign(updateRequestScoreQuery, { values: [url,scoreDiff] });
|
var query = Object.assign(updateRequestScoreQuery, { values: [url,scoreDiff] });
|
||||||
return db.query(query)
|
return db.query(query)
|
||||||
.then((result: pg.QueryResult) => "Song request score updated.");
|
.then(() => [200,"Song request score updated."] as [number,string]);
|
||||||
};
|
};
|
||||||
|
|
||||||
// deleteRequest
|
// deleteRequest
|
||||||
|
@ -100,9 +100,8 @@ const deleteRequestQuery = {
|
||||||
text: "DELETE FROM requests WHERE url = $1"
|
text: "DELETE FROM requests WHERE url = $1"
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteRequest(url: string) {
|
export async function deleteRequest(url: string): Promise<[number,string]> {
|
||||||
var query = Object.assign(deleteRequestQuery, { values: [url] });
|
var query = Object.assign(deleteRequestQuery, { values: [url] });
|
||||||
return db.query(query)
|
return db.query(query)
|
||||||
.then((result: pg.QueryResult) => "Song request deleted.");
|
.then(() => [200,"Song request deleted."] as [number,string]);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"target": "es5",
|
"target": "es2017",
|
||||||
"module": "commonjs",
|
"module": "commonjs",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"esModuleInterop": true,
|
"esModuleInterop": true,
|
||||||
|
|
Loading…
Reference in New Issue