|
| 1 | +import { PoolClient } from 'pg'; |
| 2 | +import { Warning } from '../../types/database/TablesTypes'; |
| 3 | +import { DatabaseService } from '../service/Databas'; |
| 4 | +export class WarningDatabaseService { |
| 5 | + private _db: DatabaseService; |
| 6 | + constructor(private _client: PoolClient) { |
| 7 | + this._db = new DatabaseService(_client); |
| 8 | + } |
| 9 | + |
| 10 | + // Create a new warning |
| 11 | + async create(warning: Omit<Warning, 'id'>): Promise<Warning> { |
| 12 | + return await this._db.insert<Warning>('Warning', { |
| 13 | + user_id: warning.user_id, |
| 14 | + group_id: warning.group_id, |
| 15 | + reason: warning.reason, |
| 16 | + warned_at: warning.warned_at || new Date(), |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + // Update an existing warning (reason) |
| 21 | + async update(warning: Warning): Promise<Warning | null> { |
| 22 | + const updatedWarning = await this._db.update<Warning>( |
| 23 | + 'Warning', |
| 24 | + { |
| 25 | + reason: warning.reason, |
| 26 | + warned_at: warning.warned_at || new Date(), |
| 27 | + }, |
| 28 | + { id: warning.id } |
| 29 | + ); |
| 30 | + |
| 31 | + return updatedWarning ?? null; |
| 32 | + } |
| 33 | + |
| 34 | + // Delete a specific warning |
| 35 | + async delete(warningId: number): Promise<boolean> { |
| 36 | + const deletedWarnings = await this._db.delete<Warning>('Warning', { id: warningId }); |
| 37 | + return deletedWarnings.length > 0; |
| 38 | + } |
| 39 | + |
| 40 | + // Fetch a warning by ID |
| 41 | + async getById(warningId: number): Promise<Warning | null> { |
| 42 | + const query = `SELECT * FROM "Warning" WHERE id = $1;`; |
| 43 | + const result = await this._db.query<Warning>(query, [warningId]); |
| 44 | + return result.rows[0] || null; |
| 45 | + } |
| 46 | + |
| 47 | + // Fetch all warnings for a specific user |
| 48 | + async getByUserId(userId: number): Promise<Warning[]> { |
| 49 | + const query = ` |
| 50 | + SELECT * FROM "Warning" |
| 51 | + WHERE user_id = $1 |
| 52 | + ORDER BY warned_at DESC; |
| 53 | + `; |
| 54 | + const result = await this._db.query<Warning>(query, [userId]); |
| 55 | + return result.rows; |
| 56 | + } |
| 57 | + |
| 58 | + // Fetch all warnings for a specific group |
| 59 | + async getByGroupId(groupId: number): Promise<Warning> { |
| 60 | + const query = ` |
| 61 | + SELECT * FROM "Warning" |
| 62 | + WHERE group_id = $1 |
| 63 | + ORDER BY warned_at DESC; |
| 64 | + `; |
| 65 | + const result = await this._db.query<Warning>(query, [groupId]); |
| 66 | + return result.rows[0]; |
| 67 | + } |
| 68 | + async getAll(filter: { groupId?: number; userId?: number } = {}): Promise<Warning[]> { |
| 69 | + const conditions: string[] = []; |
| 70 | + const params: any[] = []; |
| 71 | + |
| 72 | + if (filter.groupId) { |
| 73 | + conditions.push(`group_id = $${params.length + 1}`); |
| 74 | + params.push(filter.groupId); |
| 75 | + } |
| 76 | + if (filter.userId) { |
| 77 | + conditions.push(`user_id = $${params.length + 1}`); |
| 78 | + params.push(filter.userId); |
| 79 | + } |
| 80 | + |
| 81 | + const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''; |
| 82 | + const query = ` |
| 83 | + SELECT * FROM "Warning" |
| 84 | + ${whereClause} |
| 85 | + ORDER BY warned_at DESC; |
| 86 | + `; |
| 87 | + |
| 88 | + const result = await this._db.query<Warning>(query, params); |
| 89 | + return result.rows; |
| 90 | + } |
| 91 | + |
| 92 | + async save(groupId: number, userId: number, reason: string): Promise<Warning> { |
| 93 | + let warn = await this.getByGroupId(groupId); |
| 94 | + if (!warn) { |
| 95 | + const newGroupData: Omit<Warning, 'id'> = { |
| 96 | + group_id: groupId, |
| 97 | + reason: reason, |
| 98 | + user_id: userId, |
| 99 | + warned_at: new Date(), |
| 100 | + }; |
| 101 | + warn = await this.create(newGroupData); |
| 102 | + } |
| 103 | + return warn!; |
| 104 | + } |
| 105 | +} |
0 commit comments