import AdminBadgesViewModel from "../models/admin/AdminBadgesViewModel"; import AdminBadgeViewModel from "../models/admin/AdminBadgeViewModel"; import AdminDeleteBadgeModel from "../models/admin/AdminDeleteBadgeModel"; import AdminDeletePartyModel from "../models/admin/AdminDeletePartyModel"; import AdminExpireSessionModel from "../models/admin/AdminExpireSessionModel"; import AdminIndexViewModel from "../models/admin/AdminIndexViewModel"; import AdminPartiesViewModel from "../models/admin/AdminPartiesViewModel"; import AdminPartyViewModel from "../models/admin/AdminPartyViewModel"; import AdminUsersViewModel from "../models/admin/AdminUsersViewModel"; 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"; import BadgeService from "../services/BadgeService"; 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(), partyCount: await PartyService.GetPartyCount(), badgeCount: await BadgeService.GetBadgeCount() }; return this.view(adminIndexViewModel); } public async Users_Get() { const adminUsersViewModel: AdminUsersViewModel = { users: await UserService.GetAll() }; return this.view(adminUsersViewModel); } 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"); } public async Parties_Get() { const adminPartiesViewModel: AdminPartiesViewModel = { parties: await PartyService.GetAll() }; return this.view(adminPartiesViewModel); } 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"); } 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"); } 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); } }