74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
|
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.");
|
||
|
};
|
||
|
|