t00-multiuser/server/controller/AdminController.ts

216 lines
No EOL
8.2 KiB
TypeScript

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 AdminUserBadgesViewModel from "../models/admin/AdminUserBadgesViewModel";
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 FunkyArray from "funky-array";
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";
import UserBadgeListItem from "../entities/list/UserBadgeListItem";
import AdminRemoveUserBadgeModel from "../models/admin/AdminRemoveUserBadgeModel";
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(),
userBadgeCount: await BadgeService.GetUnlockedBadgeCount()
};
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_Auth$BadgeEditor() {
const adminBadgesViewModel: AdminBadgesViewModel = {
badges: await BadgeService.LoadAll()
};
return this.view(adminBadgesViewModel);
}
public async Badge_Get_Auth$BadgeEditor(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;
adminBadgeViewModel.isSecret = badge.IsSecret;
} else {
adminBadgeViewModel.name = "";
adminBadgeViewModel.description = "";
adminBadgeViewModel.imageUrl = "";
adminBadgeViewModel.forUrl = "";
adminBadgeViewModel.isSecret = false;
}
return this.view(adminBadgeViewModel);
}
public async Badge_Post_Auth$BadgeEditor(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 ?? "", (adminBadgeViewModel.isSecret?.toString() ?? "") === "on");
return this.redirectToAction("badges");
}
public async DeleteBadge_Get_Auth$BadgeEditor(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);
}
public async UserBadges_Get(adminUserBadgesViewModel: AdminUserBadgesViewModel) {
const unlockedBadgeListItems = await BadgeService.GetUnlockedBadgeList(adminUserBadgesViewModel.badgeq, adminUserBadgesViewModel.userq);
const unlockedBadgeByBadgeId = new FunkyArray<number, Array<UserBadgeListItem>>();
let unlockedBadgeByBadgeIdArray: Array<UserBadgeListItem> | undefined;
for (const unlockedBadgeListItem of unlockedBadgeListItems) {
if (!(unlockedBadgeByBadgeIdArray = unlockedBadgeByBadgeId.get(unlockedBadgeListItem.BadgeId))) {
unlockedBadgeByBadgeId.set(unlockedBadgeListItem.BadgeId, unlockedBadgeByBadgeIdArray = new Array<UserBadgeListItem>());
}
unlockedBadgeByBadgeIdArray.push(unlockedBadgeListItem);
}
adminUserBadgesViewModel.badges = await BadgeService.LoadList(adminUserBadgesViewModel.badgeq);
adminUserBadgesViewModel.unlockByBadgeId = unlockedBadgeByBadgeId
return this.view(adminUserBadgesViewModel);
}
public async RemoveUserBadge_Get(adminRemoveUserBadgeModel: AdminRemoveUserBadgeModel) {
if (typeof(adminRemoveUserBadgeModel.id) === "undefined" || typeof(adminRemoveUserBadgeModel.id) !== "string") {
return this.badRequest();
}
const userBadgeId = parseInt(adminRemoveUserBadgeModel.id);
UserService.DeleteUnlockedBadge(this.session.userId, userBadgeId);
return this.redirectToAction("userbadges");
}
}