Add database table

This commit is contained in:
Ethan Lane 2022-08-17 17:58:56 +01:00
parent bc24a4ecfe
commit eef01b1c3d
Signed by: Vylpes
GPG key ID: EED233CC06D12504
4 changed files with 84 additions and 0 deletions

View file

@ -0,0 +1,7 @@
export enum AuditType {
General,
Warn,
Mute,
Kick,
Ban,
}

60
src/entity/Audit.ts Normal file
View file

@ -0,0 +1,60 @@
import { Column, Entity, getConnection, ManyToOne } from "typeorm";
import { AuditType } from "../constants/AuditType";
import BaseEntity from "../contracts/BaseEntity";
import StringTools from "../helpers/StringTools";
import Server from "./Server";
@Entity()
export default class Audit extends BaseEntity {
constructor(userId: string, auditType: AuditType, reason: string, moderatorId: string) {
super();
this.AuditId = StringTools.RandomString(5).toUpperCase();
this.UserId = userId;
this.AuditType = auditType;
this.Reason = reason;
this.ModeratorId = moderatorId;
}
@Column()
AuditId: string;
@Column()
UserId: string;
@Column()
AuditType: AuditType;
@Column()
Reason: string;
@Column()
ModeratorId: string;
@ManyToOne(() => Server, x => x.Audits)
Server: Server;
public AssignToServer(server: Server) {
this.Server = server;
}
public static async FetchAuditsByUserId(userId: string): Promise<Audit[] | undefined> {
const connection = getConnection();
const repository = connection.getRepository(Audit);
const all = await repository.find({ UserId: userId });
return all;
}
public static async FetchAuditByAuditId(auditId: string): Promise<Audit | undefined> {
const connection = getConnection();
const repository = connection.getRepository(Audit);
const single = await repository.findOne({ AuditId: auditId });
return single;
}
}

View file

@ -1,5 +1,6 @@
import { Entity, OneToMany } from "typeorm"; import { Entity, OneToMany } from "typeorm";
import BaseEntity from "../contracts/BaseEntity"; import BaseEntity from "../contracts/BaseEntity";
import Audit from "./Audit";
import Role from "./Role"; import Role from "./Role";
import Setting from "./Setting"; import Setting from "./Setting";
@ -17,6 +18,9 @@ export default class Server extends BaseEntity {
@OneToMany(() => Role, x => x.Server) @OneToMany(() => Role, x => x.Server)
Roles: Role[]; Roles: Role[];
@OneToMany(() => Audit, x => x.Server)
Audits: Audit[];
public AddSettingToServer(setting: Setting) { public AddSettingToServer(setting: Setting) {
this.Settings.push(setting); this.Settings.push(setting);
} }

View file

@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from "typeorm"
export class CreateAudit1660754832945 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(`CREATE TABLE audit (Id varchar(255), WhenCreated datetime, WhenUpdated datetime, auditId varchar(255), userId varchar(255), auditType int, reason varchar(255), moderatorId varchar(255), serverId varchar(255), PRIMARY KEY (Id), FOREIGN KEY (serverId) REFERENCES server(id))`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(`DROP TABLE audit`);
}
}