t00-multiuser/server/controller/PartyController.ts

82 lines
No EOL
2.7 KiB
TypeScript

import CreateEditPartyViewModel from "../models/party/CreateEditPartyViewModel";
import JoinPartyViewModel from "../models/party/JoinPartyViewModel";
import LeavePartyModel from "../models/party/LeavePartyModel";
import SetActivePartyModel from "../models/party/SetActivePartyModel";
import UserService from "../services/UserService";
import Controller from "./Controller";
export default class PartyController extends Controller {
public async Create_Get() {
return this.view("createEdit");
}
public async Create_Post(createEditPartyViewModel: CreateEditPartyViewModel) {
if (typeof(createEditPartyViewModel.name) !== "string" || typeof(createEditPartyViewModel.partyRef) !== "string") {
return this.badRequest();
}
const party = await UserService.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);
return this.redirectToAction("index", "home");
}
public async Join_Get() {
return this.view();
}
public async Join_Post(joinPartyViewModel: JoinPartyViewModel) {
if (typeof(joinPartyViewModel.partyRef) !== "string") {
return this.badRequest();
}
const party = await UserService.GetPartyByPartyRef(joinPartyViewModel.partyRef);
if (!party) {
joinPartyViewModel.message = "That Join Code / Party ID is invalid.";
return this.view(joinPartyViewModel);
}
const userPartyExisting = await UserService.GetUserPartyForUser(this.session.userId, party.Id);
if (userPartyExisting) {
joinPartyViewModel.message = "You are already in this party.";
return this.view(joinPartyViewModel);
}
await UserService.AddUserToParty(this.session.userId, party.Id);
return this.redirectToAction("index", "home");
}
public async Leave_Get(leavePartyModel: LeavePartyModel) {
const partyId = parseInt(leavePartyModel.id ?? "-1");
if (typeof(leavePartyModel.id) !== "string" || isNaN(partyId)) {
return this.badRequest();
}
await UserService.LeaveParty(this.session.userId, partyId);
return this.redirectToAction("index", "home");
}
public async SetActive_Get(setActivePartyModel: SetActivePartyModel) {
const partyId = parseInt(setActivePartyModel.id ?? "-1");
if (typeof(setActivePartyModel.id) !== "string" || isNaN(partyId)) {
return this.badRequest();
}
await UserService.SetActiveParty(this.session.userId, partyId);
return this.redirectToAction("index", "home");
}
public async Deactivate_Get() {
await UserService.DeactivateCurrentParty(this.session.userId);
return this.redirectToAction("index", "home");
}
}