import { Console } from "hsconsole"; import Badge from "../entities/Badge"; import BadgeRepo from "../repos/BadgeRepo"; import BadgeCache from "../objects/BadgeCache"; import UserBadgeRepo from "../repos/UserBadgeRepo"; export default abstract class BadgeService { public static async SaveBadge(currentUserId:number, id:number | undefined, name:string, description:string, hint: string, imageUrl: string, forUrl:string, isSecret: boolean) { 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; badge.Hint = hint; badge.ImageUrl = imageUrl; badge.ForUrl = forUrl; badge.IsSecret = isSecret; badge = await BadgeRepo.insertUpdate(badge); await BadgeCache.RefreshCache(); return badge; } 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; } } public static async LoadList(query?: string) { try { return await BadgeRepo.selectList(query); } catch (e) { Console.printError(`MultiProbe server service error:\n${e}`); throw e; } } 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); } public static async GetBadgeCount() { try { return await BadgeRepo.selectBadgeCount(); } catch (e) { Console.printError(`MultiProbe server service error:\n${e}`); throw e; } } 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; } } }