75 lines
No EOL
1.9 KiB
TypeScript
75 lines
No EOL
1.9 KiB
TypeScript
import { Console } from "hsconsole";
|
|
import Badge from "../entities/Badge";
|
|
import BadgeRepo from "../repos/BadgeRepo";
|
|
import BadgeCache from "../objects/BadgeCache";
|
|
|
|
export default abstract class BadgeService {
|
|
public static async SaveBadge(currentUserId:number, id:number | undefined, name:string, description: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.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 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;
|
|
}
|
|
}
|
|
} |