t00-multiuser/server/controller/AdminController.ts

179 lines
6.3 KiB
TypeScript
Raw Normal View History

2024-09-28 01:31:46 +01:00
import AdminBadgesViewModel from "../models/admin/AdminBadgesViewModel";
import AdminBadgeViewModel from "../models/admin/AdminBadgeViewModel";
import AdminDeleteBadgeModel from "../models/admin/AdminDeleteBadgeModel";
2024-09-28 14:31:02 +01:00
import AdminDeletePartyModel from "../models/admin/AdminDeletePartyModel";
import AdminExpireSessionModel from "../models/admin/AdminExpireSessionModel";
2024-09-26 00:47:08 +01:00
import AdminIndexViewModel from "../models/admin/AdminIndexViewModel";
import AdminPartiesViewModel from "../models/admin/AdminPartiesViewModel";
2024-09-28 14:31:02 +01:00
import AdminPartyViewModel from "../models/admin/AdminPartyViewModel";
2024-09-26 00:47:08 +01:00
import AdminUsersViewModel from "../models/admin/AdminUsersViewModel";
2024-09-28 14:31:02 +01:00
import AdminUserViewModel from "../models/admin/AdminUserViewModel";
import AdminWebSessionsViewModel from "../models/admin/AdminWebSessionsViewModel";
import AdminWsSessionsViewModel from "../models/admin/AdminWsSessionsViewModel";
import Session from "../objects/Session";
import WsData from "../objects/WsData";
2024-09-28 01:31:46 +01:00
import BadgeService from "../services/BadgeService";
2024-09-26 00:47:08 +01:00
import PartyService from "../services/PartyService";
import UserService from "../services/UserService";
import Controller from "./Controller";
export default class AdminController_Auth$Admin extends Controller {
public async Index_Get() {
const adminIndexViewModel: AdminIndexViewModel = {
userCount: await UserService.GetUserCount(),
2024-09-28 14:31:02 +01:00
partyCount: await PartyService.GetPartyCount(),
badgeCount: await BadgeService.GetBadgeCount()
2024-09-26 00:47:08 +01:00
};
return this.view(adminIndexViewModel);
}
public async Users_Get() {
const adminUsersViewModel: AdminUsersViewModel = {
users: await UserService.GetAll()
};
return this.view(adminUsersViewModel);
}
2024-09-28 14:31:02 +01:00
public async User_Get(adminUserViewModel: AdminUserViewModel) {
const user = adminUserViewModel.id ? await UserService.GetUser(parseInt(adminUserViewModel.id)) : null;
if (typeof(adminUserViewModel.id) !== "undefined" && user) {
adminUserViewModel.username = user.Username;
adminUserViewModel.userLevel = String(user.UserLevel);
} else {
adminUserViewModel.username = "";
adminUserViewModel.userLevel = "0";
}
return this.view(adminUserViewModel);
}
public async User_Post(adminUserViewModel: AdminUserViewModel) {
if (typeof(adminUserViewModel.id) === "undefined") {
return this.badRequest();
}
const user = await UserService.SaveUser(this.session.userId, parseInt(adminUserViewModel.id), adminUserViewModel.username ?? "", parseInt(adminUserViewModel.userLevel ?? ""));
if (!user) {
adminUserViewModel.message = "A user with that username already exists.";
return this.view(adminUserViewModel);
}
return this.redirectToAction("users");
}
2024-09-26 00:47:08 +01:00
public async Parties_Get() {
const adminPartiesViewModel: AdminPartiesViewModel = {
parties: await PartyService.GetAll()
};
return this.view(adminPartiesViewModel);
}
2024-09-28 01:31:46 +01:00
2024-09-28 14:31:02 +01:00
public async Party_Get(adminPartyViewModel: AdminPartyViewModel) {
const party = adminPartyViewModel.id ? await PartyService.GetParty(parseInt(adminPartyViewModel.id)) : null;
if (typeof(adminPartyViewModel.id) !== "undefined" && party) {
adminPartyViewModel.partyRef = party.PartyRef;
adminPartyViewModel.name = party.Name;
} else {
adminPartyViewModel.partyRef = "";
adminPartyViewModel.name = "";
}
return this.view(adminPartyViewModel);
}
public async Party_Post(adminPartyViewModel: AdminPartyViewModel) {
if (typeof(adminPartyViewModel.id) === "undefined") {
return this.badRequest();
}
await PartyService.SaveParty(this.session.userId, parseInt(adminPartyViewModel.id), adminPartyViewModel.name ?? "", adminPartyViewModel.partyRef ?? "");
return this.redirectToAction("parties");
}
public async DeleteParty_Get(adminDeletePartyModel: AdminDeletePartyModel) {
if (typeof(adminDeletePartyModel.id) === "undefined" || typeof(adminDeletePartyModel.id) !== "string") {
return this.badRequest();
}
const partyId = parseInt(adminDeletePartyModel.id);
await PartyService.DeletePartyAdmin(this.session.userId, partyId);
return this.redirectToAction("parties");
}
2024-09-28 01:31:46 +01:00
public async Badges_Get() {
const adminBadgesViewModel: AdminBadgesViewModel = {
badges: await BadgeService.LoadAll()
};
return this.view(adminBadgesViewModel);
}
public async Badge_Get(adminBadgeViewModel: AdminBadgeViewModel) {
const badge = adminBadgeViewModel.id ? await BadgeService.LoadBadge(parseInt(adminBadgeViewModel.id)) : null;
if (typeof(adminBadgeViewModel.id) !== "undefined" && badge) {
adminBadgeViewModel.name = badge.Name;
adminBadgeViewModel.description = badge.Description;
adminBadgeViewModel.imageUrl = badge.ImageUrl;
adminBadgeViewModel.forUrl = badge.ForUrl;
} else {
adminBadgeViewModel.name = "";
adminBadgeViewModel.description = "";
adminBadgeViewModel.imageUrl = "";
adminBadgeViewModel.forUrl = "";
}
return this.view(adminBadgeViewModel);
}
public async Badge_Post(adminBadgeViewModel: AdminBadgeViewModel) {
if (typeof(adminBadgeViewModel.id) === "undefined") {
return this.badRequest();
}
await BadgeService.SaveBadge(this.session.userId, parseInt(adminBadgeViewModel.id), adminBadgeViewModel.name ?? "", adminBadgeViewModel.description ?? "", adminBadgeViewModel.imageUrl ?? "", adminBadgeViewModel.forUrl ?? "");
return this.redirectToAction("badges");
}
public async DeleteBadge_Get(adminDeleteBadgeModel: AdminDeleteBadgeModel) {
if (typeof(adminDeleteBadgeModel.id) === "undefined" || typeof(adminDeleteBadgeModel.id) !== "string") {
return this.badRequest();
}
const badgeId = parseInt(adminDeleteBadgeModel.id);
await BadgeService.DeleteBadge(this.session.userId, badgeId);
return this.redirectToAction("badges");
}
2024-09-28 14:31:02 +01:00
public async WebSessions_Get() {
const adminWebSessionsViewModel: AdminWebSessionsViewModel = {
sessions: Session.Sessions
};
return this.view(adminWebSessionsViewModel);
}
public async ExpireSession_Get(adminExpireSessionModel: AdminExpireSessionModel) {
if (typeof(adminExpireSessionModel.key) === "undefined" || typeof(adminExpireSessionModel.key) !== "string") {
return this.badRequest();
}
Session.Sessions.remove(adminExpireSessionModel.key);
return this.redirectToAction("websessions");
}
public async WsSessions_Get() {
const adminWsSessionsViewModel: AdminWsSessionsViewModel = {
users: WsData.users
};
return this.view(adminWsSessionsViewModel);
}
2024-09-26 00:47:08 +01:00
}