update eslint and refactor backend

This commit is contained in:
2026-01-04 12:37:46 +01:00
parent 43824ce284
commit 090b3c10d1
13 changed files with 438 additions and 152 deletions

View File

@@ -1,14 +1,16 @@
import { Database } from 'bun:sqlite';
import type { Group, User } from '@/interfaces';
import { SQL } from 'bun';
// create tables if they don't exist
const createTableScript = `
-- GROUP TRABLE --
CREATE TABLE IF NOT EXISTS group (
CREATE TABLE IF NOT EXISTS groups (
id INTEGER PRIMARY KEY AUTOINCREMENT,
code TEXT UNIQUE NOT NULL,
name TEXT NOT NULL,
mail TEXT NOT NULL,
image BLOB,
phase TEXT DEFAULT 'gathering',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
@@ -17,20 +19,21 @@ const createTableScript = `
id INTEGER PRIMARY KEY AUTOINCREMENT,
mail TEXT NOT NULL,
image BLOB,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- GROUP_MEMBER TABLE --
CREATE TABLE IF NOT EXISTS group_member (
-- MEMBERSHIP TABLE --
CREATE TABLE IF NOT EXISTS membership (
id INTEGER PRIMARY KEY AUTOINCREMENT,
group_id INTEGER,
user_id INTEGER,
UNIQUE(email, group_id),
UNIQUE(user_id, group_id),
FOREIGN KEY(group_id) REFERENCES groups(id),
FOREIGN KEY(user_id) REFERENCES users(id)
);
-- BAN TABLE --
CREATE TABLE IF NOT EXISTS ban (
CREATE TABLE IF NOT EXISTS bans (
id INTEGER PRIMARY KEY AUTOINCREMENT,
group_id INTEGER,
user_id INTEGER,
@@ -38,7 +41,7 @@ const createTableScript = `
FOREIGN KEY(group_id) REFERENCES groups(id),
FOREIGN KEY(user_id) REFERENCES users(id),
FOREIGN KEY(user_id2) REFERENCES users(id)
UNIQUE(group_id, user_id, user_id2),
UNIQUE(group_id, user_id, user_id2)
);
-- WHISHLIST TABLE --
@@ -53,32 +56,110 @@ const createTableScript = `
`;
class DB {
private instance: Database = new Database();
private instance: SQL;
private prepareDB () {
this.instance.run(createTableScript);
private executeScript(script: string, name: string) {
try {
void this.instance`${script}`;
} catch (err) {
console.error(`error executing script ${name}: ${err as Error}`);
}
}
public async getGroup(id: string) {
const stmt = this.instance.prepare('SELECT * FROM groups WHERE code = ?');
const group = await stmt.get(id);
/**
* Prepare the database by creating necessary tables.
*/
private prepareDB () {
this.executeScript(createTableScript, 'createTableScript');
}
/* USERS */
/**
* Create a new user
* @param mail: string
* @returns created user
*/
public async createUser(mail: string): Promise<User> {
const user: User = await this.instance`
INSERT INTO users (mail) VALUES (${mail})
RETURNING *
`;
return user;
}
/**
* Get user by mail
* @param mail: string
* @returns user object or undefined
*/
public async getUserByMail(mail: string): Promise<User | undefined> {
const user: User = await this.instance`
SELECT * FROM users WHERE mail = ${mail}
`;
return user;
}
/* GROUPS */
/**
* Create a new group
* @param name: string
* @param mail: string
* @returns object with id of the created group
*/
public async createGroup( {name, mail}: {name: string, mail: string}): Promise<Group> {
const code = Math.random().toString(36).substring(2, 8).toUpperCase();
const group: Group = await this.instance`
INSERT INTO groups (code, name, mail) VALUES (${code}, ${name}, ${mail})
RETURNING *
`;
return group;
};
/**
* Get group by ID
* @param id: string
* @returns group object or undefined
*/
public async getGroupByCode(code: string): Promise<Group | undefined> {
const group: Group | undefined = await this.instance`
SELECT * FROM groups WHERE code = ${code}
`;
return group;
}
public createGroup(code: string): { id: number | bigint} {
const stmt = this.instance.prepare('INSERT INTO groups (code) VALUES (?)');
const changes = stmt.run(code);
console.log('Inserted group with ID:', changes);
return { id: changes.lastInsertRowid };
};
/* GROUP MEMBER */
/**
* Add user to group
* @param userId: number
* @param groupId: number
* @returns object with id of the created group member entry
*/
public async UserEntersGroup(userId: number, groupId: number): Promise<{ id: number | bigint; }> {
const membership: { id: number | bigint} = await this.instance`
INSERT OR IGNORE INTO membership (user_id, group_id) VALUES (${userId}, ${groupId})
RETURNING *
`;
return membership;
}
public async getGroupMembers(groupId: number): Promise<User[]> {
const users: User[] = await this.instance`
SELECT *
FROM users u
JOIN membership m ON u.id = m.user_id
WHERE m.group_id = ${groupId}
`;
return users;
}
constructor(url: string = './data.sqlite') {
if (this.instance) {
this.instance = new Database(url);
this.prepareDB();
console.log('Database initialized at', url);
}
this.instance = new SQL(url, {adapter: 'sqlite'});
this.prepareDB();
console.log('Database initialized at', url);
}
}