Add updateRequestState endpoint
parent
75e5022090
commit
0f3d921d96
21
src/app.ts
21
src/app.ts
|
@ -39,6 +39,27 @@ app.post("/api/addRequest", async (request, response) => {
|
||||||
.catch((e: any) => errorHandler(request,response,e));
|
.catch((e: any) => errorHandler(request,response,e));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
app.post("/api/updateRequestState", async (request, response) => {
|
||||||
|
response.type('text/plain');
|
||||||
|
if (!request.body.url) {
|
||||||
|
response.status(400);
|
||||||
|
response.send("Missing url");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (!request.body.state) {
|
||||||
|
response.status(400);
|
||||||
|
response.send("Missing scoreDiff");
|
||||||
|
return
|
||||||
|
}
|
||||||
|
var url = request.body.url as string;
|
||||||
|
var state = request.body.state as string;
|
||||||
|
requests.updateRequestState(url,state).then((val: [number,string]) => {
|
||||||
|
response.status(val[0]);
|
||||||
|
response.send(val[1]);
|
||||||
|
})
|
||||||
|
.catch((e: any) => errorHandler(request,response,e));
|
||||||
|
});
|
||||||
|
|
||||||
app.post("/api/updateRequestScore", async (request, response) => {
|
app.post("/api/updateRequestScore", async (request, response) => {
|
||||||
response.type('text/plain');
|
response.type('text/plain');
|
||||||
if (!request.body.url) {
|
if (!request.body.url) {
|
||||||
|
|
|
@ -47,6 +47,29 @@ export async function addRequest(url: string, requester: string) {
|
||||||
.then((result: pg.QueryResult) => "Song request added.");
|
.then((result: pg.QueryResult) => "Song request added.");
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// updateRequestState
|
||||||
|
const checkValidStateQuery = {
|
||||||
|
name: "checkValidState",
|
||||||
|
text: "SELECT * FROM states WHERE state = $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
const updateRequestStateQuery = {
|
||||||
|
name: "updateRequestState",
|
||||||
|
text: "UPDATE requests SET state = $2 WHERE url = $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateRequestState(url: string, state: string) {
|
||||||
|
var query = Object.assign(checkValidStateQuery, { values: [state] });
|
||||||
|
var result = await db.query(query);
|
||||||
|
if (result.rowCount < 1) {
|
||||||
|
return [400,"Invalid state"]
|
||||||
|
}
|
||||||
|
|
||||||
|
var query = Object.assign(updateRequestStateQuery, { values: [url,state] });
|
||||||
|
return db.query(query)
|
||||||
|
.then((result: pg.QueryResult) => [200,"Song request state updated."]);
|
||||||
|
};
|
||||||
|
|
||||||
// updateRequestScore
|
// updateRequestScore
|
||||||
const updateRequestScoreQuery = {
|
const updateRequestScoreQuery = {
|
||||||
name: "updateRequestScore",
|
name: "updateRequestScore",
|
||||||
|
|
Loading…
Reference in New Issue