173 lines
5.5 KiB
JavaScript
173 lines
5.5 KiB
JavaScript
const {Client} = require('pg');
|
|
const {DB_NAME, DB_USER, DB_PASSWORD, DB_ADDRESS, DB_PORT} = require('../config.json');
|
|
|
|
const DATABASE_CLIENT = new Client({
|
|
user: DB_USER,
|
|
password: DB_PASSWORD,
|
|
host: DB_ADDRESS,
|
|
port: DB_PORT,
|
|
database: DB_NAME,
|
|
});
|
|
|
|
async function userExistsInDB(userId, guildId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT user_id FROM member WHERE user_id=$1 AND guild_id=$2",
|
|
[userId, guildId]
|
|
);
|
|
return response.rows.length > 0;
|
|
}
|
|
|
|
async function findPollCreatedByUserAndOpened(userId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT poll_id, question FROM poll WHERE creator=$1 and opened=true",
|
|
[userId]
|
|
);
|
|
return response.rows[0];
|
|
}
|
|
|
|
async function findVoteIdByVoteOptionAndPollId(voteOption, pollId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT vote_id FROM vote WHERE vote_option=$1 AND poll_id=$2",
|
|
[voteOption, pollId]
|
|
);
|
|
return response.rows[0];
|
|
}
|
|
|
|
async function findBetValueMultiplierForPoll(pollId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT vote_id, (1 + (1 - (SUM(vote_member.bet_value) / total.sum))) AS cote " +
|
|
"FROM vote_member " +
|
|
"CROSS JOIN (SELECT SUM(bet_value) " +
|
|
"AS sum FROM vote_member WHERE poll_id=$1) " +
|
|
"AS total " +
|
|
"WHERE poll_id=$1 " +
|
|
"GROUP BY vote_id, total.sum",
|
|
[pollId]
|
|
);
|
|
return response.rows;
|
|
}
|
|
|
|
async function findUsersAndBetValueByVoteId(voteId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT user_id, bet_value FROM vote_member WHERE vote_id=$1",
|
|
[voteId]
|
|
);
|
|
return response.rows;
|
|
}
|
|
|
|
async function addPointsToUser(userId, betValue) {
|
|
await DATABASE_CLIENT.query(
|
|
"UPDATE member SET points = points + $1 WHERE user_id = $2",
|
|
[betValue, userId]
|
|
);
|
|
}
|
|
|
|
async function closePoll(pollId) {
|
|
await DATABASE_CLIENT.query(
|
|
"UPDATE poll SET opened = false WHERE poll_id=$1",
|
|
[pollId]
|
|
);
|
|
}
|
|
|
|
async function findUserById(userId, guildId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT username, points, bet_value FROM member WHERE user_id=$1 AND guild_id=$2",
|
|
[userId, guildId]
|
|
);
|
|
return response.rows[0];
|
|
}
|
|
|
|
async function isLastPollCreatedByUserOpen(userId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT opened FROM poll WHERE creator=$1 AND opened=true",
|
|
[userId]
|
|
);
|
|
return !response.rows.length > 0;
|
|
}
|
|
|
|
async function findUserVoteForPoll(userId, pollId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT vote_id FROM vote_member WHERE user_id=$1 AND poll_id=$2",
|
|
[userId, pollId]
|
|
);
|
|
return response.rows[0];
|
|
}
|
|
|
|
async function findBetValueForUser(userId, guildId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT bet_value FROM member WHERE user_id=$1 AND member.guild_id=$2",
|
|
[userId, guildId]
|
|
);
|
|
return response.rows[0].bet_value;
|
|
}
|
|
|
|
async function substractUserPoints(userId, betValue) {
|
|
await DATABASE_CLIENT.query(
|
|
"UPDATE member SET points = points - $1 WHERE user_id = $2 AND points - $1 >= 50",
|
|
betValue, userId
|
|
);
|
|
}
|
|
|
|
async function bet(userId, pollId, guildId, voteId, betValue) {
|
|
await DATABASE_CLIENT.query(
|
|
"INSERT INTO vote_member (user_id, poll_id, guild_id, vote_id, bet_value) VALUES ($1, $2, $3, $4, $5)",
|
|
[userId, pollId, guildId, voteId, betValue]
|
|
);
|
|
}
|
|
|
|
async function insertVoteOptions(pollId, voteId, voteOption) {
|
|
await DATABASE_CLIENT.query(
|
|
"INSERT INTO vote (poll_id, vote_id, vote_option) VALUES ($1, $2, $3)",
|
|
[pollId, voteId, voteOption]
|
|
);
|
|
}
|
|
|
|
async function insertUserIntoDB(userId, guildId, username) {
|
|
await DATABASE_CLIENT.query(
|
|
"INSERT INTO member (user_id, guild_id, username) VALUES ($1, $2, $3)",
|
|
[userId, guildId, username]
|
|
);
|
|
}
|
|
|
|
async function insertPollIntoDB(creatorId, pollId, guildId, questionLabel) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"INSERT INTO poll(creator, poll_id, guild_id, question, opened) " +
|
|
"VALUES ($1, $2, $3, $4, true)",
|
|
[creatorId, pollId, guildId, questionLabel])
|
|
return response.rows;
|
|
}
|
|
|
|
async function guildExistsInDB(guildId) {
|
|
const response = await DATABASE_CLIENT.query(
|
|
"SELECT guild_id FROM guild WHERE guild_id=$1",
|
|
[guildId]
|
|
);
|
|
return response.rows.length > 0;
|
|
}
|
|
|
|
async function insertGuildIntoDB(guildId) {
|
|
await DATABASE_CLIENT.query(
|
|
"INSERT INTO guild (guild_id) VALUES ($1)",
|
|
[guildId]
|
|
);
|
|
}
|
|
|
|
exports.DATABASE_CLIENT = DATABASE_CLIENT;
|
|
exports.userExistsInDB = userExistsInDB;
|
|
exports.insertPollIntoDB = insertPollIntoDB;
|
|
exports.guildExistsInDB = guildExistsInDB;
|
|
exports.insertGuildIntoDB = insertGuildIntoDB;
|
|
exports.insertUserIntoDB = insertUserIntoDB;
|
|
exports.isLastPollCreatedByUserOpen = isLastPollCreatedByUserOpen;
|
|
exports.findPollCreatedByUserAndOpened = findPollCreatedByUserAndOpened;
|
|
exports.findUserById = findUserById;
|
|
exports.findUserVoteForPoll = findUserVoteForPoll;
|
|
exports.findBetValueForUser = findBetValueForUser;
|
|
exports.bet = bet;
|
|
exports.substractUserPoints = substractUserPoints;
|
|
exports.insertVoteOptions = insertVoteOptions;
|
|
exports.findVoteIdByVoteOptionAndPollId = findVoteIdByVoteOptionAndPollId;
|
|
exports.findBetValueMultiplierForPoll = findBetValueMultiplierForPoll;
|
|
exports.findUsersAndBetValueByVoteId = findUsersAndBetValueByVoteId;
|
|
exports.addPointsToUser = addPointsToUser;
|
|
exports.closePoll = closePoll; |