From ad341d420891bd61a079d7f3a4f6be4ce52af070 Mon Sep 17 00:00:00 2001 From: Holly Date: Mon, 23 Sep 2024 23:55:00 +0100 Subject: [PATCH] add delete + split out UserService --- server/controller/HomeController.ts | 4 +- server/controller/PartyController.ts | 19 +++++- server/index.ts | 3 +- server/models/party/DeletePartyModel.ts | 3 + server/repos/PartyRepo.ts | 4 +- server/repos/UserRepo.ts | 2 +- server/services/PartyService.ts | 84 +++++++++++++++++++++++++ server/services/UserService.ts | 57 ----------------- server/templates/home/home.ejs | 2 +- 9 files changed, 111 insertions(+), 67 deletions(-) create mode 100644 server/models/party/DeletePartyModel.ts create mode 100644 server/services/PartyService.ts diff --git a/server/controller/HomeController.ts b/server/controller/HomeController.ts index 8e84c63..c7c2d99 100644 --- a/server/controller/HomeController.ts +++ b/server/controller/HomeController.ts @@ -1,7 +1,7 @@ -import { FastifyReply, FastifyRequest } from "fastify"; import Controller from "./Controller"; import UserService from "../services/UserService"; import HomeViewModel from "../models/home/HomeViewModel"; +import PartyService from "../services/PartyService"; export default class HomeController extends Controller { public async Index_Get_AllowAnonymous() { @@ -11,7 +11,7 @@ export default class HomeController extends Controller { return this.unauthorised(); } - const parties = await UserService.GetUserParties(this.session.userId); + const parties = await PartyService.GetUserParties(this.session.userId); const activeUserParty = await UserService.GetActiveParty(this.session.userId); const homeViewModel: HomeViewModel = { diff --git a/server/controller/PartyController.ts b/server/controller/PartyController.ts index c6e9265..5b7a10a 100644 --- a/server/controller/PartyController.ts +++ b/server/controller/PartyController.ts @@ -1,7 +1,9 @@ import CreateEditPartyViewModel from "../models/party/CreateEditPartyViewModel"; +import DeletePartyModel from "../models/party/DeletePartyModel"; import JoinPartyViewModel from "../models/party/JoinPartyViewModel"; import LeavePartyModel from "../models/party/LeavePartyModel"; import SetActivePartyModel from "../models/party/SetActivePartyModel"; +import PartyService from "../services/PartyService"; import UserService from "../services/UserService"; import Controller from "./Controller"; @@ -15,13 +17,13 @@ export default class PartyController extends Controller { return this.badRequest(); } - const party = await UserService.GetPartyByPartyRef(createEditPartyViewModel.partyRef); + const party = await PartyService.GetPartyByPartyRef(createEditPartyViewModel.partyRef); if (party) { createEditPartyViewModel.message = "That Party ID is already taken!"; return this.view("createEdit", createEditPartyViewModel); } - await UserService.CreateParty(this.session.userId, createEditPartyViewModel.name, createEditPartyViewModel.partyRef); + await PartyService.CreateParty(this.session.userId, createEditPartyViewModel.name, createEditPartyViewModel.partyRef); return this.redirectToAction("index", "home"); } @@ -35,7 +37,7 @@ export default class PartyController extends Controller { return this.badRequest(); } - const party = await UserService.GetPartyByPartyRef(joinPartyViewModel.partyRef); + const party = await PartyService.GetPartyByPartyRef(joinPartyViewModel.partyRef); if (!party) { joinPartyViewModel.message = "That Join Code / Party ID is invalid."; return this.view(joinPartyViewModel); @@ -79,4 +81,15 @@ export default class PartyController extends Controller { return this.redirectToAction("index", "home"); } + + public async Delete_Get(deletePartyModel:DeletePartyModel) { + const partyId = parseInt(deletePartyModel.id ?? "-1"); + if (typeof(deletePartyModel.id) !== "string" || isNaN(partyId)) { + return this.badRequest(); + } + + await PartyService.DeleteParty(this.session.userId, partyId); + + return this.redirectToAction("index", "home"); + } } \ No newline at end of file diff --git a/server/index.ts b/server/index.ts index 591081b..02f2faa 100644 --- a/server/index.ts +++ b/server/index.ts @@ -39,6 +39,7 @@ import HomeController from "./controller/HomeController"; import AccountController from "./controller/AccountController"; import PartyController from "./controller/PartyController"; import ApiController from "./controller/ApiController"; +import PartyService from "./services/PartyService"; Console.customHeader(`MultiProbe server started at ${new Date()}`); @@ -214,7 +215,7 @@ websocketServer.on("connection", (socket) => { const dbUserParty = await UserService.GetActiveParty(dbUser.Id); let dbParty: Party | null = null; if (dbUserParty) { - dbParty = await UserService.GetParty(dbUserParty.PartyId); + dbParty = await PartyService.GetParty(dbUserParty.PartyId); } let page = rawURL.toLowerCase().replace(".htm", "").replace(".html", ""); diff --git a/server/models/party/DeletePartyModel.ts b/server/models/party/DeletePartyModel.ts new file mode 100644 index 0000000..e476241 --- /dev/null +++ b/server/models/party/DeletePartyModel.ts @@ -0,0 +1,3 @@ +export default interface DeletePartyModel { + id: string +} \ No newline at end of file diff --git a/server/repos/PartyRepo.ts b/server/repos/PartyRepo.ts index e830a30..2fcad19 100644 --- a/server/repos/PartyRepo.ts +++ b/server/repos/PartyRepo.ts @@ -48,7 +48,7 @@ export default class PartyRepo { party.Name, party.PartyRef, party.CreatedByUserId, party.CreatedDatetime.getTime(), party.LastModifiedByUserId ?? null, party.LastModifiedDatetime?.getTime() ?? null, party.DeletedByUserId ?? null, party.DeletedDatetime?.getTime() ?? null, Number(party.IsDeleted) ]); } else { - await Database.Instance.query(`UPDATE Party SET Name = ?, PartyRef = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ?, WHERE Id = ?`, [ + await Database.Instance.query("UPDATE Party SET Name = ?, PartyRef = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?", [ party.Name, party.PartyRef, party.CreatedByUserId, party.CreatedDatetime.getTime(), party.LastModifiedByUserId ?? null, party.LastModifiedDatetime?.getTime() ?? null, party.DeletedByUserId ?? null, party.DeletedDatetime?.getTime() ?? null, Number(party.IsDeleted), party.Id ]); } @@ -60,7 +60,7 @@ function populatePartyFromDB(party:Party, dbParty:any) { party.PartyRef = dbParty.PartyRef; party.Name = dbParty.Name; party.CreatedByUserId = dbParty.CreatedByUserId; - party.CreatedDatetime = dbParty.CreatedDatetime; + party.CreatedDatetime = new Date(dbParty.CreatedDatetime); party.LastModifiedByUserId = dbParty.LastModifiedByUserId; party.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbParty.LastModifiedDatetime); party.DeletedByUserId = dbParty.DeletedByUserId; diff --git a/server/repos/UserRepo.ts b/server/repos/UserRepo.ts index 26dd454..5969835 100644 --- a/server/repos/UserRepo.ts +++ b/server/repos/UserRepo.ts @@ -56,7 +56,7 @@ function populateUserFromDB(user:User, dbUser:any) { user.PasswordSalt = dbUser.PasswordSalt; user.APIKey = dbUser.APIKey; user.CreatedByUserId = dbUser.CreatedByUserId; - user.CreatedDatetime = dbUser.CreatedDatetime; + user.CreatedDatetime = new Date(dbUser.CreatedDatetime); user.LastModifiedByUserId = dbUser.LastModifiedByUserId; user.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.LastModifiedDatetime); user.DeletedByUserId = dbUser.DeletedByUserId; diff --git a/server/services/PartyService.ts b/server/services/PartyService.ts new file mode 100644 index 0000000..95eeb67 --- /dev/null +++ b/server/services/PartyService.ts @@ -0,0 +1,84 @@ +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 { + 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; + } + } + + 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 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; + } + + console.log(party); + + 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; + } + } +} \ No newline at end of file diff --git a/server/services/UserService.ts b/server/services/UserService.ts index 5d130ce..e52d62d 100644 --- a/server/services/UserService.ts +++ b/server/services/UserService.ts @@ -1,9 +1,7 @@ import { Console } from "hsconsole"; import User from "../entities/User"; -import PartyRepo from "../repos/PartyRepo"; import UserRepo from "../repos/UserRepo"; import PasswordUtility from "../utilities/PasswordUtility"; -import Party from "../entities/Party"; import UserParty from "../entities/UserParty"; import UserPartyRepo from "../repos/UserPartyRepo"; @@ -53,15 +51,6 @@ export default class UserService { } } - 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 GetUserPartyForUser(userId:number, partyId:number) { try { return await UserPartyRepo.selectByUserIdPartyId(userId, partyId); @@ -71,24 +60,6 @@ export default class UserService { } } - 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(); @@ -105,34 +76,6 @@ export default class UserService { } } - 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; - } - } - public static async AddUserToParty(userId:number, partyId:number) { try { const userParty = new UserParty(); diff --git a/server/templates/home/home.ejs b/server/templates/home/home.ejs index b872d0b..2a32a60 100644 --- a/server/templates/home/home.ejs +++ b/server/templates/home/home.ejs @@ -37,7 +37,7 @@ Set Current <% } %> <% if (party.CreatedByUserId === user.Id) { %> - Leave + Delete <% } else { %> Leave <% } %>