2020-07-02 06:06:14 +00:00
|
|
|
import pg from "pg";
|
|
|
|
import db from "./db";
|
|
|
|
|
|
|
|
// getRequests
|
|
|
|
const getRequestsQuery = {
|
|
|
|
name: "getRequests",
|
2020-08-08 04:32:06 +00:00
|
|
|
text: "SELECT * FROM get_requests() LIMIT $1"
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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",
|
2020-08-08 04:32:06 +00:00
|
|
|
text: "SELECT * FROM get_requests_all() LIMIT $1"
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllRequests(count: number) {
|
|
|
|
var query = Object.assign(getAllRequestsQuery, { values: [count] });
|
|
|
|
return db.query(query)
|
|
|
|
.then((result: pg.QueryResult) => result.rows);
|
|
|
|
};
|
|
|
|
|
2020-08-08 04:32:06 +00:00
|
|
|
// getRequestsVoted
|
|
|
|
const getRequestsVotedQuery = {
|
|
|
|
name: "getRequestsVoted",
|
|
|
|
text: "SELECT * FROM get_requests_voted($2) LIMIT $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getRequestsVoted(count: number, user: number) {
|
|
|
|
var query = Object.assign(getRequestsVotedQuery, { values: [count,user] });
|
|
|
|
return db.query(query)
|
|
|
|
.then((result: pg.QueryResult) => result.rows);
|
|
|
|
};
|
|
|
|
|
|
|
|
// getAllRequestsVoted
|
|
|
|
const getAllRequestsVotedQuery = {
|
|
|
|
name: "getAllRequestsVoted",
|
|
|
|
text: "SELECT * FROM get_requests_all_voted($2) LIMIT $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getAllRequestsVoted(count: number,user: number) {
|
|
|
|
var query = Object.assign(getAllRequestsVotedQuery, { values: [count,user] });
|
|
|
|
return db.query(query)
|
|
|
|
.then((result: pg.QueryResult) => result.rows);
|
|
|
|
};
|
|
|
|
|
2020-07-02 06:06:14 +00:00
|
|
|
// addRequest
|
2020-07-04 16:34:03 +00:00
|
|
|
const validUrlRegexes = [
|
|
|
|
/^https:\/\/www\.youtube\.com\/watch\?v=[a-zA-Z0-9_-]{11}$/
|
|
|
|
];
|
|
|
|
|
2020-07-02 06:06:14 +00:00
|
|
|
const checkRequestExistsQuery = {
|
|
|
|
name: "checkRequestExists",
|
|
|
|
text: "SELECT * FROM requests WHERE url = $1"
|
|
|
|
}
|
|
|
|
|
|
|
|
const addRequestQuery = {
|
|
|
|
name: "addRequest",
|
2020-08-08 04:32:06 +00:00
|
|
|
text: "CALL add_request($1,$2)"
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
|
2020-08-03 05:06:28 +00:00
|
|
|
export async function addRequest(url: string, requester: string): Promise<[number,string]> {
|
2020-07-04 16:34:03 +00:00
|
|
|
var validUrl = false;
|
|
|
|
for (var regex of validUrlRegexes) {
|
|
|
|
if (regex.test(url)) {
|
|
|
|
validUrl = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!validUrl) return [400, "Invalid song URL."];
|
2020-07-02 06:06:14 +00:00
|
|
|
var query = Object.assign(checkRequestExistsQuery, { values: [url] });
|
|
|
|
var result = await db.query(query);
|
|
|
|
if (result.rowCount > 0) {
|
2020-07-07 04:24:04 +00:00
|
|
|
return [200,`Song already requested by ${result.rows[0].requester}. State: ${result.rows[0].state}`]
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
var query = Object.assign(addRequestQuery, { values: [url,requester] });
|
|
|
|
return db.query(query)
|
2020-08-03 05:06:28 +00:00
|
|
|
.then(() => [201,"Song request added."] as [number,string]);
|
2020-07-02 06:06:14 +00:00
|
|
|
};
|
|
|
|
|
2020-07-02 20:32:55 +00:00
|
|
|
// 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"
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:06:28 +00:00
|
|
|
export async function updateRequestState(url: string, state: string): Promise<[number,string]> {
|
2020-07-02 20:32:55 +00:00
|
|
|
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)
|
2020-08-03 05:06:28 +00:00
|
|
|
.then(() => [200,"Song request state updated."] as [number,string]);
|
2020-07-02 20:32:55 +00:00
|
|
|
};
|
|
|
|
|
2020-07-02 06:06:14 +00:00
|
|
|
// updateRequestScore
|
|
|
|
const updateRequestScoreQuery = {
|
|
|
|
name: "updateRequestScore",
|
|
|
|
text: "UPDATE requests SET score = score + $2 WHERE url = $1"
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:06:28 +00:00
|
|
|
export async function updateRequestScore(url: string, scoreDiff: number): Promise<[number,string]> {
|
2020-07-02 06:06:14 +00:00
|
|
|
var query = Object.assign(updateRequestScoreQuery, { values: [url,scoreDiff] });
|
|
|
|
return db.query(query)
|
2020-08-03 05:06:28 +00:00
|
|
|
.then(() => [200,"Song request score updated."] as [number,string]);
|
2020-07-02 06:06:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// deleteRequest
|
|
|
|
const deleteRequestQuery = {
|
|
|
|
name: "deleteRequest",
|
|
|
|
text: "DELETE FROM requests WHERE url = $1"
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:06:28 +00:00
|
|
|
export async function deleteRequest(url: string): Promise<[number,string]> {
|
2020-07-02 06:06:14 +00:00
|
|
|
var query = Object.assign(deleteRequestQuery, { values: [url] });
|
|
|
|
return db.query(query)
|
2020-08-03 05:06:28 +00:00
|
|
|
.then(() => [200,"Song request deleted."] as [number,string]);
|
2020-07-02 06:06:14 +00:00
|
|
|
};
|
2020-08-08 04:32:06 +00:00
|
|
|
|
|
|
|
const checkVoteExistsQuery = {
|
|
|
|
name: "checkVoteExists",
|
|
|
|
text: "SELECT * FROM votes WHERE requesturl = $1 AND userid = $2"
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function addVote(url: string, user: string): Promise<[number,string]> {
|
|
|
|
var query = Object.assign(checkVoteExistsQuery, { values: [url,user] });
|
|
|
|
var result = await db.query(query);
|
|
|
|
if (result.rowCount > 0) {
|
|
|
|
return [200,`Song already voted on`]
|
|
|
|
}
|
|
|
|
return db.query("CALL add_vote($1,$2)",[url,user])
|
|
|
|
.then(() => [201,"Successfully cast vote."] as [number,string]);
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function deleteVote(url: string, user: string): Promise<[number,string]> {
|
|
|
|
return db.query("CALL delete_vote($1,$2)",[url,user])
|
|
|
|
.then(() => [201,"Successfully retracted vote."] as [number,string]);
|
|
|
|
};
|