Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 38 additions & 41 deletions api/db/init.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { pool } from ".";

export async function initTables() {
const createGuildsTable = `
const createGuildsTable = `
CREATE TABLE IF NOT EXISTS guilds (
id VARCHAR(255) NOT NULL PRIMARY KEY,
name VARCHAR(255),
icon VARCHAR(255),
members INT,
cooldown INT DEFAULT 30000,
updates_enabled BOOLEAN DEFAULT FALSE,
updates_channel VARCHAR(255)
cooldown INT DEFAULT 30000
)
`;
const createUsersTable = `
const createUsersTable = `
CREATE TABLE IF NOT EXISTS users (
id VARCHAR(255) NOT NULL,
guild_id VARCHAR(255) NOT NULL,
Expand All @@ -26,55 +24,54 @@ export async function initTables() {
PRIMARY KEY (id, guild_id)
)
`;
// FOREIGN KEY (guild_id) REFERENCES guilds(id)
const createRolesTable = `
// FOREIGN KEY (guild_id) REFERENCES guilds(id)
const createRolesTable = `
CREATE TABLE IF NOT EXISTS roles (
id VARCHAR(255) NOT NULL PRIMARY KEY,
guild_id VARCHAR(255) NOT NULL,
name VARCHAR(255),
level INT NOT NULL
)
`
// FOREIGN KEY (guild_id) REFERENCES guilds(id)
const createUpdatesTable = `
`;
// FOREIGN KEY (guild_id) REFERENCES guilds(id)
const createUpdatesTable = `
CREATE TABLE IF NOT EXISTS updates (
guild_id VARCHAR(255) NOT NULL PRIMARY KEY,
channel_id VARCHAR(255) NOT NULL,
enabled BOOLEAN DEFAULT FALSE
)
`
// FOREIGN KEY (guild_id) REFERENCES guilds(id)

pool.query(createGuildsTable, (err) => {
if (err) {
console.error("Error creating guilds table:", err);
} else {
console.log("Guilds table created");
}
});
`;
// FOREIGN KEY (guild_id) REFERENCES guilds(id)

pool.query(createUsersTable, (err) => {
if (err) {
console.error("Error creating users table:", err);
} else {
console.log("Users table created");
}
});
pool.query(createGuildsTable, (err) => {
if (err) {
console.error("Error creating guilds table:", err);
} else {
console.log("Guilds table created");
}
});

pool.query(createUsersTable, (err) => {
if (err) {
console.error("Error creating users table:", err);
} else {
console.log("Users table created");
}
});

pool.query(createRolesTable, (err) => {
if (err) {
console.error("Error creating roles table:", err);
} else {
console.log("Roles table created");
}
});
pool.query(createRolesTable, (err) => {
if (err) {
console.error("Error creating roles table:", err);
} else {
console.log("Roles table created");
}
});

pool.query(createUpdatesTable, (err) => {
if (err) {
console.error("Error creating updates table:", err);
} else {
console.log("Updates table created");
}
});
pool.query(createUpdatesTable, (err) => {
if (err) {
console.error("Error creating updates table:", err);
} else {
console.log("Updates table created");
}
});
}
2 changes: 0 additions & 2 deletions api/db/queries/guilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ export interface Guild {
icon: string;
members: number;
cooldown: number;
updates_enabled: boolean;
updates_channel: string;
}

export async function getGuild(guildId: string): Promise<[QueryError, null] | [null, Guild | null]> {
Expand Down
9 changes: 4 additions & 5 deletions api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ app.post("/post/:guild", authMiddleware, async (req, res) => {
name,
icon,
members,
updates_enabled: false,
updates_channel: "",
cooldown: 30_000,
});

if (err) {
Expand Down Expand Up @@ -71,10 +70,10 @@ app.post("/post/:guild/:user", authMiddleware, async (req, res) => {
((newXp - currentLevelXp) / (nextLevelXp - currentLevelXp)) * 100;

const updateQuery = `
INSERT INTO users
INSERT INTO users
(id, guild_id, xp, pfp, name, nickname, level, xp_needed_next_level, progress_next_level)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
ON DUPLICATE KEY UPDATE
xp = VALUES(xp),
pfp = VALUES(pfp),
name = VALUES(name),
Expand Down Expand Up @@ -248,7 +247,7 @@ app.post("/admin/:action/:guild/:target", authMiddleware, async (req, res) => {
default:
return res.status(500).json({ message: "Internal server error" });
}
case "cooldown":
case "cooldown":
if (target !== "set" && target !== "get") {
return res.status(400).json({ message: "Illegal request" });
}
Expand Down