refactor(models): Moved models and Repositories to database
This commit is contained in:
parent
d5f5fe5f1a
commit
9155f630d9
18 changed files with 42 additions and 42 deletions
102
source/Database/Repositories/PlaydateRepository.ts
Normal file
102
source/Database/Repositories/PlaydateRepository.ts
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import {Repository} from "./Repository";
|
||||
import {PlaydateModel} from "../Models/PlaydateModel";
|
||||
import Playdate, {DBPlaydate} from "../tables/Playdate";
|
||||
import {DatabaseConnection} from "../DatabaseConnection";
|
||||
import {GroupRepository} from "./GroupRepository";
|
||||
import {GroupModel} from "../Models/GroupModel";
|
||||
import {Nullable} from "../../types/Nullable";
|
||||
|
||||
export class PlaydateRepository extends Repository<PlaydateModel, DBPlaydate> {
|
||||
|
||||
constructor(
|
||||
protected readonly database: DatabaseConnection,
|
||||
private readonly groupRepository: GroupRepository,
|
||||
) {
|
||||
super(
|
||||
database,
|
||||
Playdate
|
||||
);
|
||||
}
|
||||
|
||||
findFromGroup(group: GroupModel, all = false) {
|
||||
let sql = `SELECT * FROM ${this.schema.name} WHERE groupid = ?`;
|
||||
const params = [group.id];
|
||||
|
||||
if (!all) {
|
||||
sql += " AND time_from > ?"
|
||||
params.push(new Date().getTime())
|
||||
}
|
||||
|
||||
const finds = this.database.fetchAll<number, DBPlaydate>(
|
||||
sql,
|
||||
...params
|
||||
);
|
||||
|
||||
return finds.map((playdate) => this.convertToModelType(playdate, group));
|
||||
}
|
||||
|
||||
findPlaydatesInRange(fromDate: Date | number, toDate: Date | number | undefined = undefined, 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 > ?`;
|
||||
const params = [fromDate];
|
||||
|
||||
if (toDate) {
|
||||
sql = `${sql} AND time_from < ?`
|
||||
params.push(toDate);
|
||||
}
|
||||
|
||||
if (group) {
|
||||
sql = `${sql} AND groupid = ?`
|
||||
params.push(<number>group.id)
|
||||
}
|
||||
|
||||
const finds = this.database.fetchAll<number, DBPlaydate>(
|
||||
sql,
|
||||
...params
|
||||
);
|
||||
|
||||
return finds.map((playdate) => this.convertToModelType(playdate, group));
|
||||
}
|
||||
|
||||
getNextPlaydateForGroup(group: GroupModel): PlaydateModel | null {
|
||||
const sql = `SELECT * FROM ${this.schema.name} WHERE groupid = ? AND time_from > ? ORDER BY time_from LIMIT 1`;
|
||||
|
||||
const find = this.database.fetch<number, DBPlaydate>(
|
||||
sql,
|
||||
group.id,
|
||||
Date.now()
|
||||
)
|
||||
|
||||
if (!find) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.convertToModelType(find, group)
|
||||
}
|
||||
|
||||
protected convertToModelType(intermediateModel: DBPlaydate | undefined, fixedGroup: Nullable<GroupModel> = null): PlaydateModel {
|
||||
if (!intermediateModel) {
|
||||
throw new Error("Unable to convert the playdate model");
|
||||
}
|
||||
return {
|
||||
id: intermediateModel.id,
|
||||
group: fixedGroup ?? this.groupRepository.getById(intermediateModel.groupid),
|
||||
from_time: new Date(intermediateModel.time_from),
|
||||
to_time: new Date(intermediateModel.time_to),
|
||||
};
|
||||
}
|
||||
|
||||
protected convertToCreateObject(instance: Partial<PlaydateModel>): object {
|
||||
return {
|
||||
groupid: instance.group?.id ?? null,
|
||||
time_from: instance.from_time?.getTime() ?? 0,
|
||||
time_to: instance.to_time?.getTime() ?? 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue