add delete + split out UserService

This commit is contained in:
Holly Stubbs 2024-09-23 23:55:00 +01:00
parent 9c43486406
commit ad341d4208
Signed by: tgpholly
GPG Key ID: B8583C4B7D18119E
9 changed files with 111 additions and 67 deletions

View File

@ -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 = {

View File

@ -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");
}
}

View File

@ -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", "");

View File

@ -0,0 +1,3 @@
export default interface DeletePartyModel {
id: string
}

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}
}

View File

@ -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();

View File

@ -37,7 +37,7 @@
<a href="/party/setactive?id=<%= party.Id %>" class="btn btn-sm btn-success me-2">Set Current</a>
<% } %>
<% if (party.CreatedByUserId === user.Id) { %>
<a class="btn btn-sm btn-danger disabled" title="You may not leave a party if you created it." onclick="alert('You may not leave a party if you created it.')">Leave</a>
<a href="/party/delete?id=<%= party.Id %>" class="btn btn-sm btn-danger" onclick="return confirm(`Are you sure you want to delete '<%= party.Name %>'?\nThis will remove all users in this party from it.\nThis action cannot be undone.`)">Delete</a>
<% } else { %>
<a href="/party/leave?id=<%= party.Id %>" class="btn btn-sm btn-danger" onclick="return confirm(`Are you sure you want to leave '<%= party.Name %>'?`)">Leave</a>
<% } %>