t00-multiuser/server/services/BadgeService.ts

113 lines
2.8 KiB
TypeScript
Raw Permalink Normal View History

2024-09-28 01:31:46 +01:00
import { Console } from "hsconsole";
import Badge from "../entities/Badge";
import BadgeRepo from "../repos/BadgeRepo";
import BadgeCache from "../objects/BadgeCache";
2024-10-12 11:52:13 +01:00
import UserBadgeRepo from "../repos/UserBadgeRepo";
2024-09-28 01:31:46 +01:00
export default abstract class BadgeService {
2024-10-13 14:24:34 +01:00
public static async SaveBadge(currentUserId:number, id:number | undefined, name:string, description:string, hint: string, imageUrl: string, forUrl:string, isSecret: boolean) {
2024-09-28 01:31:46 +01:00
try {
let badge = id ? await BadgeRepo.selectById(id) : null;
if (badge === null) {
badge = new Badge();
badge.CreatedByUserId = currentUserId;
badge.CreatedDatetime = new Date();
} else {
badge.LastModifiedByUserId = currentUserId;
badge.LastModifiedDatetime = new Date();
}
badge.Name = name;
badge.Description = description;
2024-10-13 14:24:34 +01:00
badge.Hint = hint;
2024-09-28 01:31:46 +01:00
badge.ImageUrl = imageUrl;
badge.ForUrl = forUrl;
2024-10-08 11:04:46 +01:00
badge.IsSecret = isSecret;
2024-09-28 01:31:46 +01:00
badge = await BadgeRepo.insertUpdate(badge);
await BadgeCache.RefreshCache();
return badge;
2024-09-28 01:31:46 +01:00
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async LoadBadge(id:number) {
try {
return await BadgeRepo.selectById(id);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async LoadAll() {
try {
return await BadgeRepo.selectAll();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-10-12 11:52:13 +01:00
public static async LoadList(query?: string) {
try {
return await BadgeRepo.selectList(query);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-09-28 01:31:46 +01:00
public static async DeleteBadge(currentUserId:number, id: number) {
const badge = await BadgeRepo.selectById(id);
if (badge == null) {
return null;
}
badge.DeletedByUserId = currentUserId;
badge.DeletedDatetime = new Date();
badge.IsDeleted = true;
return await BadgeRepo.insertUpdate(badge);
}
2024-09-28 14:31:02 +01:00
public static async GetBadgeCount() {
try {
return await BadgeRepo.selectBadgeCount();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-10-12 11:52:13 +01:00
public static async GetUnlockedBadgeCount() {
try {
return await UserBadgeRepo.selectUnlockedBadgeCount();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetAllUnlockedBadges() {
try {
return await UserBadgeRepo.selectAll();
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
public static async GetUnlockedBadgeList(badgeQuery?: string, userQuery?: string) {
try {
return await UserBadgeRepo.selectUnlockList(badgeQuery, userQuery);
} catch (e) {
Console.printError(`MultiProbe server service error:\n${e}`);
throw e;
}
}
2024-09-28 01:31:46 +01:00
}