t00-multiuser/server/repos/BadgeRepo.ts
2024-09-26 10:31:24 +01:00

44 lines
No EOL
1.5 KiB
TypeScript

import Badge from "../entities/Badge";
import Database from "../objects/Database";
import RepoBase from "./RepoBase";
export default abstract class BadgeRepo {
public static async selectAll() {
const dbUser = await Database.Instance.query("SELECT * FROM Badge WHERE IsDeleted = 0");
const badges = new Array<Badge>();
for (const row of dbUser) {
const badge = new Badge();
populateBadgeFromDB(badge, row);
badges.push(badge);
}
return badges;
}
public static async selectById(id:number) {
const dbBadge = await Database.Instance.query("SELECT * FROM Badge WHERE Id = ? AND IsDeleted = 0 LIMIT 1", [id]);
if (dbBadge == null || dbBadge.length === 0) {
return null;
} else {
const badge = new Badge();
populateBadgeFromDB(badge, dbBadge[0]);
return badge;
}
}
}
function populateBadgeFromDB(badge:Badge, dbBadge:any) {
badge.Id = dbBadge.Id;
badge.Name = dbBadge.Name;
badge.Description = dbBadge.Description;
badge.ImageUrl = dbBadge.ImageUrl;
badge.ForUrl = dbBadge.ForUrl;
badge.CreatedByUserId = dbBadge.CreatedByUserId;
badge.CreatedDatetime = new Date(dbBadge.CreatedDatetime);
badge.LastModifiedByUserId = dbBadge.LastModifiedByUserId;
badge.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbBadge.LastModifiedDatetime);
badge.DeletedByUserId = dbBadge.DeletedByUserId;
badge.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbBadge.DeletedDatetime);
badge.IsDeleted = dbBadge.IsDeleted === 1;
}