|
| 1 | +import { Collection, Guild } from 'discord.js'; |
| 2 | +import { Pool, PoolClient } from 'pg'; |
| 3 | +import { databaseConfig } from '../config/database'; |
| 4 | +const pool: Pool = new Pool(databaseConfig); |
| 5 | + |
| 6 | +type GuildRecord = { |
| 7 | + uuid: string; |
| 8 | + name: string; |
| 9 | + member_count: number; |
| 10 | + owner_id: string; |
| 11 | +}; |
| 12 | + |
| 13 | +export async function createGuildTable() { |
| 14 | + const client: PoolClient = await pool.connect(); |
| 15 | + if (client) { |
| 16 | + try { |
| 17 | + await client.query('BEGIN'); |
| 18 | + const createGuildTableQuery = |
| 19 | + 'CREATE TABLE IF NOT EXISTS Guild(uuid TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL, member_count INTEGER NOT NULL, owner_id TEXT NOT NULL)'; |
| 20 | + await client.query(createGuildTableQuery); |
| 21 | + await client.query('COMMIT'); |
| 22 | + } catch (error) { |
| 23 | + await client.query('ROLLBACK'); |
| 24 | + console.log(error); |
| 25 | + //TODO: Add error handling |
| 26 | + } finally { |
| 27 | + client.release(); |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +export async function getGuilds() { |
| 32 | + const client: PoolClient = await pool.connect(); |
| 33 | + if (client) { |
| 34 | + try { |
| 35 | + await client.query('BEGIN'); |
| 36 | + const getAllGuildsQuery = 'SELECT * FROM Guild'; |
| 37 | + const allGuilds = await client.query(getAllGuildsQuery); |
| 38 | + return allGuilds; |
| 39 | + } catch (error) { |
| 40 | + await client.query('ROLLBACK'); |
| 41 | + console.log(error); |
| 42 | + //TODO: Add error handling |
| 43 | + } finally { |
| 44 | + client.release(); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | +export async function populateGuilds(existingGuilds: Collection<string, Guild>) { |
| 49 | + try { |
| 50 | + const guildsInDatabase = await getGuilds(); |
| 51 | + existingGuilds.forEach(async (guild: Guild) => { |
| 52 | + const isInDatabase = |
| 53 | + guildsInDatabase && |
| 54 | + guildsInDatabase.rows.find((guildDb: GuildRecord) => guildDb.uuid === guild.id); |
| 55 | + if (!isInDatabase) { |
| 56 | + await insertNewGuild(guild); |
| 57 | + } |
| 58 | + }); |
| 59 | + } catch (error) { |
| 60 | + console.log(error); |
| 61 | + //TODO: Add error handling |
| 62 | + } |
| 63 | +} |
| 64 | +export async function insertNewGuild(newGuild: Guild) { |
| 65 | + const client: PoolClient = await pool.connect(); |
| 66 | + if (client) { |
| 67 | + try { |
| 68 | + await client.query('BEGIN'); |
| 69 | + const insertNewGuildQuery = |
| 70 | + 'INSERT INTO Guild (uuid, name, member_count, owner_id) VALUES ($1, $2, $3, $4)'; |
| 71 | + await client.query(insertNewGuildQuery, [ |
| 72 | + newGuild.id, |
| 73 | + newGuild.name, |
| 74 | + newGuild.memberCount, |
| 75 | + newGuild.ownerId, |
| 76 | + ]); |
| 77 | + await client.query('COMMIT'); |
| 78 | + } catch (error) { |
| 79 | + await client.query('ROLLBACK'); |
| 80 | + console.log(error); |
| 81 | + //TODO: Add error handling |
| 82 | + } finally { |
| 83 | + client.release(); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
| 87 | +export async function deleteGuild(existingGuild: Guild) { |
| 88 | + const client: PoolClient = await pool.connect(); |
| 89 | + if (client) { |
| 90 | + try { |
| 91 | + await client.query('BEGIN'); |
| 92 | + const deleteGuildQuery = 'DELETE from Guild WHERE uuid = ($1)'; |
| 93 | + await client.query(deleteGuildQuery, [existingGuild.id]); |
| 94 | + await client.query('COMMIT'); |
| 95 | + } catch (error) { |
| 96 | + await client.query('ROLLBACK'); |
| 97 | + console.log(error); |
| 98 | + } finally { |
| 99 | + client.release(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments