import { Console } from "hsconsole"; import User from "../objects/User"; import PartyRepo from "../repos/PartyRepo"; import UserRepo from "../repos/UserRepo"; import PasswordUtility from "../utilities/PasswordUtility"; import Party from "../objects/Party"; import UserParty from "../objects/UserParty"; import UserPartyRepo from "../repos/UserPartyRepo"; export default class UserService { public static async GetUser(id:number) { try { return await UserRepo.selectById(id); } catch (e) { Console.printError(`MultiProbe server service error:\n${e}`); throw e; } } public static async GetUserByUsername(username:string) { try { return await UserRepo.selectByUsername(username); } 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 GetParty(id:number) { try { return await PartyRepo.selectById(id); } catch (e) { Console.printError(`MultiProbe server service error:\n${e}`); throw e; } } 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 CreateUser(currentUserId:number, username:string, password:string) { try { const user = new User(); user.Username = username; user.PasswordSalt = PasswordUtility.GenerateSalt(); user.PasswordHash = await PasswordUtility.HashPassword(user.PasswordSalt, password); user.CreatedByUserId = currentUserId; user.CreatedDatetime = new Date(); await UserRepo.insertUpdate(user); } catch (e) { Console.printError(`MultiProbe server service error:\n${e}`); throw e; } } public static async CreateParty(currentUserId:number, name:string, partyRef:string) { try { const party = new Party(); party.Name = name; party.PartyRef = partyRef; party.CreatedByUserId = currentUserId; party.CreatedDatetime = new Date(); await PartyRepo.insertUpdate(party); const newParty = await PartyRepo.selectByPartyRef(partyRef); if (!newParty) { throw "This shouldn't happen"; } const userParty = new UserParty(); userParty.UserId = currentUserId; userParty.PartyId = newParty.Id; userParty.CreatedByUserId = currentUserId; userParty.CreatedDatetime = new Date(); await UserPartyRepo.insertUpdate(userParty); } catch (e) { Console.printError(`MultiProbe server service error:\n${e}`); throw e; } } }