t00-multiuser/server/services/PartyService.ts

138 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-09-23 23:55:00 +01:00
import { Console } from "hsconsole";
import PartyRepo from "../repos/PartyRepo";
import Party from "../entities/Party";
import UserParty from "../entities/UserParty";
import UserPartyRepo from "../repos/UserPartyRepo";
export default abstract class PartyService {
public static async CreateParty(currentUserId:number, name:string, partyRef:string) {
try {
2024-09-28 14:31:02 +01:00
let party = new Party();
2024-09-23 23:55:00 +01:00
party.Name = name;
party.PartyRef = partyRef;
party.CreatedByUserId = currentUserId;
party.CreatedDatetime = new Date();
2024-09-28 14:31:02 +01:00
party = await PartyRepo.insertUpdate(party);
2024-09-23 23:55:00 +01:00
const userParty = new UserParty();
userParty.UserId = currentUserId;
2024-09-28 14:31:02 +01:00
userParty.PartyId = party.Id;
2024-09-23 23:55:00 +01:00
userParty.CreatedByUserId = currentUserId;
userParty.CreatedDatetime = new Date();
await UserPartyRepo.insertUpdate(userParty);
2024-09-28 14:31:02 +01:00
return party;
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async SaveParty(currentUserId:number, id:number | undefined, name:string, partyRef:string) {
try {
let party = id ? await PartyRepo.selectById(id) : null;
if (!party) {
party = new Party();
party.CreatedByUserId = currentUserId;
party.CreatedDatetime = new Date();
} else {
party.LastModifiedByUserId = currentUserId;
party.LastModifiedDatetime = new Date();
}
party.Name = name;
party.PartyRef = partyRef;
return await PartyRepo.insertUpdate(party);
2024-09-23 23:55:00 +01:00
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetParty(id:number) {
try {
return await PartyRepo.selectById(id);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-09-26 00:47:08 +01:00
public static async GetAll() {
try {
return await PartyRepo.selectAll();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-09-23 23:55:00 +01:00
public static async GetPartyByPartyRef(partyRef:string) {
try {
return await PartyRepo.selectByPartyRef(partyRef);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetUserParties(userId:number) {
try {
return await PartyRepo.selectByUserId(userId);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async DeleteParty(currentUserId:number, partyId:number) {
try {
const party = await PartyRepo.selectById(partyId);
if (!party || party.CreatedByUserId !== currentUserId) {
return null;
}
2024-09-28 14:31:02 +01:00
party.DeletedByUserId = currentUserId;
party.DeletedDatetime = new Date();
party.IsDeleted = true;
await PartyRepo.insertUpdate(party);
return party;
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async DeletePartyAdmin(currentUserId:number, partyId:number) {
try {
const party = await PartyRepo.selectById(partyId);
if (!party) {
return null;
}
2024-09-23 23:55:00 +01:00
party.DeletedByUserId = currentUserId;
party.DeletedDatetime = new Date();
party.IsDeleted = true;
await PartyRepo.insertUpdate(party);
return party;
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-09-26 00:47:08 +01:00
public static async GetPartyCount() {
try {
return await PartyRepo.selectPartyCount();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-09-23 23:55:00 +01:00
}