finish up current features

This commit is contained in:
Crylia
2024-09-13 21:03:14 +02:00
parent 72b7c978a3
commit 5ed26bd488
10 changed files with 124 additions and 108 deletions

View File

@@ -1,17 +1,8 @@
const { client } = require('./database')
const { getClient } = require('./database')
const CreateBirthday = async (user, birthday) => {
try {
const alreadyReportedResult = await client.query(`
SELECT discorduser FROM birthday
WHERE discorduser = $1;`,
[user]
)
if (alreadyReportedResult.rows.length === 1) {
return alreadyReportedResult.rows[0]
}
const client = getClient()
const userResult = await client.query(`
SELECT * FROM discorduser
WHERE name = $1;`,
@@ -26,13 +17,13 @@ const CreateBirthday = async (user, birthday) => {
)
}
await client.query(`
const res = await client.query(`
INSERT INTO birthday (date, discorduser)
VALUES ($1, $2);`,
[new Date(birthday.split('.').reverse().join('-')).toISOString().slice(0, 10), user]
)
return true
return res.rowCount > 0
} catch (error) {
console.error('Error creating birthday entry:', error)
return false
@@ -41,6 +32,8 @@ const CreateBirthday = async (user, birthday) => {
const ReadBirthday = async (user) => {
try {
const client = getClient()
if (user) {
const res = await client.query(`
SELECT * FROM birthday
@@ -62,6 +55,8 @@ const ReadBirthday = async (user) => {
const UpdateBirthday = async (user, birthday) => {
try {
const client = getClient()
if (!birthday) return false
await client.query(`
@@ -80,6 +75,8 @@ const UpdateBirthday = async (user, birthday) => {
const DeleteBirthday = async (user) => {
try {
const client = getClient()
if (!user) return false
await client.query(`

View File

@@ -1,34 +1,30 @@
const { client } = require('./database')
const { getClient } = require('./database')
const CreateBlacklist = async (reportedUser, reason, reportedByUser) => {
try {
const alreadyReportedResult = await client.query(`
SELECT name FROM blacklist
WHERE name = $1`,
const client = getClient()
const alreadyReportedResult = await client.query(
"SELECT name FROM blacklist WHERE name = $1",
[reportedUser]
)
if (alreadyReportedResult.rows.length === 1) {
if (alreadyReportedResult.rows.length === 1)
return alreadyReportedResult.rows[0]
}
const userResult = await client.query(`
SELECT * FROM discorduser
WHERE name = $1;`,
const userResult = await client.query(
"SELECT * FROM discorduser WHERE name = $1",
[reportedByUser]
)
if (userResult.rows.length === 0) {
await client.query(`
INSERT INTO discorduser (name)
VALUES ($1);`,
if (userResult.rows.length === 0)
await client.query(
"INSERT INTO discorduser (name) VALUES ($1)",
[reportedByUser]
)
}
await client.query(`
INSERT INTO blacklist (name, reason, reportedby)
VALUES ($1, $2, $3);`,
await client.query(
"INSERT INTO blacklist (name, reason, reportedby) VALUES ($1, $2, $3)",
[reportedUser, reason, reportedByUser]
)
@@ -41,16 +37,17 @@ const CreateBlacklist = async (reportedUser, reason, reportedByUser) => {
const ReadBlacklist = async (user) => {
try {
const client = getClient()
if (user) {
const res = await client.query(`
SELECT * FROM blacklist
WHERE name=$1`,
const res = await client.query(
"SELECT * FROM blacklist WHERE name=$1",
[user]
)
return res.rows[0] || false
} else {
const res = await client.query(`
SELECT * FROM blacklist`
const res = await client.query(
"SELECT * FROM blacklist"
)
return res.rows
}
@@ -62,12 +59,12 @@ const ReadBlacklist = async (user) => {
const UpdateBlacklist = async (reportedUser, reason) => {
try {
const client = getClient()
if (!reportedUser) return false
await client.query(`
UPDATE blacklist
SET reason = $2
WHERE name = $1;`,
await client.query(
"UPDATE blacklist SET reason = $2 WHERE name = $1",
[reportedUser, reason]
)
return true
@@ -79,11 +76,12 @@ const UpdateBlacklist = async (reportedUser, reason) => {
const DeleteBlacklist = async (user) => {
try {
const client = getClient()
if (!user) return false
await client.query(`
DELETE FROM blacklist
WHERE name = $1;`,
await client.query(
"DELETE FROM blacklist WHERE name = $1",
[user]
)
return true

View File

@@ -1,25 +1,32 @@
const { Client } = require('pg')
require('dotenv').config()
const DB_USER = process.env.DB_USER
const DB_HOST = process.env.DB_HOST
const DB_NAME = process.env.DB_NAME
const DB_PASS = process.env.DB_PASS
const client = new Client({
connectionString: `postgresql://${DB_USER}:${DB_PASS}@${DB_HOST}/${DB_NAME}`
})
let client = null
const connectDatabase = async () => {
const createClient = (config) => {
client = new Client(config);
return client;
};
const connectDatabase = async (client) => {
try {
await client.connect()
console.log('Database connected')
} catch (error) {
console.error('Database connection error:', err)
console.error('Database connection error:', error)
throw new Error('Connection failed');
}
}
const getClient = () => {
if (!client) {
throw new Error('Client has not been initialized. Call createClient first.');
}
return client;
};
module.exports = {
client,
createClient,
connectDatabase,
getClient,
}

View File

@@ -1,9 +1,11 @@
const { client } = require('./database')
const { getClient } = require('./database')
const ReadEvents = async () => {
try {
const res = await client.query(`
SELECT * FROM event_times;`
const client = getClient()
const res = await client.query(
"SELECT * FROM event_times"
)
return res.rows
} catch (error) {
@@ -14,8 +16,10 @@ const ReadEvents = async () => {
const GetEventRole = async () => {
try {
const res = await client.query(`
SELECT * FROM event_role_view;`
const client = getClient()
const res = await client.query(
"SELECT * FROM event_role_view"
)
const rolesEventMap = new Map()
res.rows.forEach(row => rolesEventMap.set(row.event_name, row.role))
@@ -29,8 +33,10 @@ const GetEventRole = async () => {
const GetIconRole = async () => {
try {
const res = await client.query(`
SELECT role, icon_name FROM event_roles;`
const client = getClient()
const res = await client.query(
"SELECT role, icon_name FROM event_roles"
)
const rolesEventMap = new Map()
res.rows.forEach(row => rolesEventMap.set(row.icon_name, row.role))

View File

@@ -1,22 +1,22 @@
const { client } = require('./database')
const { getClient } = require('./database')
const CreateStatic = async (name, creator, members, size) => {
try {
await client.query(`
INSERT INTO static (name, creator, size)
VALUES ($1, $2, $3)
RETURNING id;`,
const client = getClient()
const result = await client.query(
"INSERT INTO static (name, creator, size) VALUES ($1, $2, $3) RETURNING id",
[name, creator, size]
)
const staticId = result.rows[0].id;
const staticId = result.rows[0].id
const memberValues = members.map(member => `(${staticId}, '${member}')`).join(',')
await client.query(`
INSERT INTO static_members (static_id, member)
VALUES ${memberValues};`
)
for (const member of members) {
await client.query(
"INSERT INTO static_members (static_id, member) VALUES ($1, $2)",
[staticId, member]
)
}
return true
} catch (error) {
@@ -27,9 +27,10 @@ const CreateStatic = async (name, creator, members, size) => {
const ReadStatic = async (name) => {
try {
const res = await client.query(`
SELECT * FROM static
WHERE name = $1;`,
const client = getClient()
const res = await client.query(
"SELECT * FROM static WHERE name = $1",
[name]
)
return res.rows
@@ -41,9 +42,10 @@ const ReadStatic = async (name) => {
const DeleteStatic = async (name) => {
try {
await client.query(`
DELETE FROM static
WHERE name = $1;`,
const client = getClient()
await client.query(
"DELETE FROM static WHERE name = $1",
[name]
)
} catch (error) {