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
65
source/Database/Repositories/GroupConfigurationRepository.ts
Normal file
65
source/Database/Repositories/GroupConfigurationRepository.ts
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
import {Repository} from "./Repository";
|
||||
import GroupConfiguration, {DBGroupConfiguration} from "../tables/GroupConfiguration";
|
||||
import {GroupConfigurationModel} from "../Models/GroupConfigurationModel";
|
||||
import {GroupModel} from "../Models/GroupModel";
|
||||
import {Nullable} from "../../types/Nullable";
|
||||
import {DatabaseConnection} from "../DatabaseConnection";
|
||||
import {GroupRepository} from "./GroupRepository";
|
||||
|
||||
export class GroupConfigurationRepository extends Repository<GroupConfigurationModel, DBGroupConfiguration> {
|
||||
|
||||
constructor(
|
||||
protected readonly database: DatabaseConnection,
|
||||
private readonly groupRepository: GroupRepository,
|
||||
) {
|
||||
super(
|
||||
database,
|
||||
GroupConfiguration
|
||||
);
|
||||
}
|
||||
|
||||
public findGroupConfigurations(group: GroupModel): GroupConfigurationModel[] {
|
||||
return this.database.fetchAll<number, DBGroupConfiguration>(`
|
||||
SELECT * FROM groupConfiguration WHERE groupid = ?`,
|
||||
group.id
|
||||
).map((config) => {
|
||||
return this.convertToModelType(config, group);
|
||||
})
|
||||
}
|
||||
|
||||
public findConfigurationByPath(group: GroupModel, path: string): Nullable<GroupConfigurationModel> {
|
||||
const result = this.database.fetch<number, DBGroupConfiguration>(`
|
||||
SELECT * FROM groupConfiguration WHERE groupid = ? AND key = ?`,
|
||||
group.id,
|
||||
path
|
||||
);
|
||||
|
||||
if (!result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.convertToModelType(result, group);
|
||||
}
|
||||
|
||||
|
||||
protected convertToModelType(intermediateModel: DBGroupConfiguration | undefined, group: Nullable<GroupModel> = null): GroupConfigurationModel {
|
||||
if (!intermediateModel) {
|
||||
throw new Error("No intermediate model provided");
|
||||
}
|
||||
|
||||
return {
|
||||
id: intermediateModel.id,
|
||||
group: group ?? this.groupRepository.getById(intermediateModel.id),
|
||||
key: intermediateModel.key,
|
||||
value: intermediateModel.value,
|
||||
}
|
||||
}
|
||||
|
||||
protected convertToCreateObject(instance: Partial<GroupConfigurationModel>): object {
|
||||
return {
|
||||
groupid: instance.group?.id ?? undefined,
|
||||
key: instance.key ?? undefined,
|
||||
value: instance.value ?? undefined,
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue