Adds Event system and automatic messages
This commit is contained in:
parent
0e10ea3cab
commit
2f826fbf36
20 changed files with 428 additions and 18 deletions
|
|
@ -34,6 +34,30 @@ export class PlaydateRepository extends Repository<PlaydateModel, DBPlaydate> {
|
|||
|
||||
return finds.map((playdate) => this.convertToModelType(playdate, group));
|
||||
}
|
||||
findPlaydatesInRange(fromDate: Date|number, toDate: Date|number, group: GroupModel | undefined = undefined) {
|
||||
if (fromDate instanceof Date) {
|
||||
fromDate = fromDate.getTime();
|
||||
}
|
||||
if (toDate instanceof Date) {
|
||||
toDate = toDate.getTime();
|
||||
}
|
||||
|
||||
let sql = `SELECT * FROM ${this.schema.name} WHERE time_from > ? AND time_from < ?`;
|
||||
const params = [fromDate, toDate];
|
||||
|
||||
if (group) {
|
||||
sql = `${sql} AND groupid = ?`
|
||||
params.push(group.id)
|
||||
}
|
||||
|
||||
const finds = this.database.fetchAll<number, DBPlaydate>(
|
||||
sql,
|
||||
...params
|
||||
);
|
||||
|
||||
return finds.map((playdate) => this.convertToModelType(playdate, group));
|
||||
}
|
||||
|
||||
protected convertToModelType(intermediateModel: DBPlaydate | undefined, fixedGroup: Nullable<GroupModel> = null): PlaydateModel {
|
||||
if (!intermediateModel) {
|
||||
throw new Error("Unable to convert the playdate model");
|
||||
|
|
|
|||
|
|
@ -3,8 +3,12 @@ import {Model} from "../Models/Model";
|
|||
import { Nullable } from "../types/Nullable";
|
||||
import {DatabaseDefinition} from "../Database/DatabaseDefinition";
|
||||
import {debug} from "node:util";
|
||||
import {Container} from "../Container/Container";
|
||||
import {EventHandler} from "../Events/EventHandler";
|
||||
import {ElementCreatedEvent} from "../Events/ElementCreatedEvent";
|
||||
|
||||
export class Repository<ModelType extends Model, IntermediateModelType = unknown> {
|
||||
|
||||
constructor(
|
||||
protected readonly database: DatabaseConnection,
|
||||
public readonly schema: DatabaseDefinition,
|
||||
|
|
@ -30,7 +34,11 @@ export class Repository<ModelType extends Model, IntermediateModelType = unknown
|
|||
const sql = `INSERT INTO ${this.schema.name}(${Object.keys(createObject).join(',')})
|
||||
VALUES (${Object.keys(createObject).map(() => "?").join(',')})`;
|
||||
const result = this.database.execute(sql, ...Object.values(createObject));
|
||||
return result.lastInsertRowid;
|
||||
const id = result.lastInsertRowid;
|
||||
|
||||
Container.get<EventHandler>(EventHandler.name).dispatch(new ElementCreatedEvent<ModelType>(this.schema.name, instance, id));
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
public update(instance: Partial<ModelType>&{id: number}): boolean {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue