Adds Deletion
This commit is contained in:
parent
a0b668cb90
commit
0d9cf6a370
8 changed files with 174 additions and 35 deletions
|
|
@ -4,6 +4,8 @@ import Groups, {DBGroup} from "../Database/tables/Groups";
|
|||
import {DatabaseConnection} from "../Database/DatabaseConnection";
|
||||
import {CacheType, CacheTypeReducer, Guild, GuildMember, GuildMemberRoleManager} from "discord.js";
|
||||
import {Nullable} from "../types/Nullable";
|
||||
import {PlaydateRepository} from "./PlaydateRepository";
|
||||
import {Container} from "../Container/Container";
|
||||
|
||||
export class GroupRepository extends Repository<GroupModel, DBGroup> {
|
||||
|
||||
|
|
@ -43,12 +45,31 @@ export class GroupRepository extends Repository<GroupModel, DBGroup> {
|
|||
return dbResult.map((result) => this.convertToModelType(result));
|
||||
}
|
||||
|
||||
public findGroupsByMember(member: GuildMember) {
|
||||
public findGroupsByMember(member: GuildMember, onlyLeader: boolean = false) {
|
||||
if (!member) {
|
||||
throw new Error("Can't find member for guild: none given");
|
||||
}
|
||||
|
||||
return this.findGroupsByRoles(member.guild.id, [...member.roles.cache.keys()])
|
||||
const groups = this.findGroupsByRoles(member.guild.id, [...member.roles.cache.keys()])
|
||||
|
||||
if (!onlyLeader) {
|
||||
return groups;
|
||||
}
|
||||
|
||||
return groups.filter((group: GroupModel) => {
|
||||
return group.leader.memberid === member.id;
|
||||
})
|
||||
}
|
||||
|
||||
public deleteGroup(group: GroupModel): void {
|
||||
this.delete(group.id);
|
||||
|
||||
debugger
|
||||
const repo = Container.get<PlaydateRepository>(PlaydateRepository.name);
|
||||
const playdates = repo.findFromGroup(group, true)
|
||||
playdates.forEach((playdate) => {
|
||||
repo.delete(playdate.id);
|
||||
})
|
||||
}
|
||||
|
||||
protected convertToModelType(intermediateModel: DBGroup | undefined): GroupModel {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ import {DatabaseConnection} from "../Database/DatabaseConnection";
|
|||
import {GroupRepository} from "./GroupRepository";
|
||||
import {GroupModel} from "../Models/GroupModel";
|
||||
import {Nullable} from "../types/Nullable";
|
||||
import playdate from "../Database/tables/Playdate";
|
||||
|
||||
export class PlaydateRepository extends Repository<PlaydateModel, DBPlaydate> {
|
||||
|
||||
|
|
@ -19,16 +18,22 @@ export class PlaydateRepository extends Repository<PlaydateModel, DBPlaydate> {
|
|||
);
|
||||
}
|
||||
|
||||
findFromGroup(group: GroupModel) {
|
||||
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>(
|
||||
`SELECT * FROM ${this.schema.name} WHERE groupid = ? AND time_from > ?`,
|
||||
group.id,
|
||||
new Date().getTime()
|
||||
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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue