Initial API
This commit is contained in:
parent
ce518aeeb5
commit
b7d8f14eb4
3 changed files with 140 additions and 3 deletions
64
src/app.ts
64
src/app.ts
|
@ -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}`);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue