t00-multiuser/server/repos/BadgeRepo.ts

65 lines
3 KiB
TypeScript
Raw Normal View History

2024-09-26 10:31:24 +01:00
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;
}
}
2024-09-28 01:31:46 +01:00
2024-09-28 14:31:02 +01:00
public static async selectBadgeCount() {
const countResult = await Database.Instance.query("SELECT COUNT(Id) FROM `Badge` WHERE IsDeleted = 0 LIMIT 1");
return countResult[0]["COUNT(Id)"];
}
2024-09-28 01:31:46 +01:00
public static async insertUpdate(badge:Badge) {
if (badge.Id === Number.MIN_VALUE) {
2024-10-08 11:04:46 +01:00
badge.Id = (await Database.Instance.query("INSERT Badge (Name, Description, ImageUrl, ForUrl, IsSecret, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
badge.Name, badge.Description, badge.ImageUrl, badge.ForUrl, Number(badge.IsSecret), badge.CreatedByUserId, badge.CreatedDatetime.getTime(), badge.LastModifiedByUserId ?? null, badge.LastModifiedDatetime?.getTime() ?? null, badge.DeletedByUserId ?? null, badge.DeletedDatetime?.getTime() ?? null, Number(badge.IsDeleted)
2024-09-28 01:31:46 +01:00
]))[0]["Id"];
} else {
2024-10-08 11:04:46 +01:00
await Database.Instance.query(`UPDATE Badge SET Name = ?, Description = ?, ImageUrl = ?, ForUrl = ?, IsSecret = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
badge.Name, badge.Description, badge.ImageUrl, badge.ForUrl, Number(badge.IsSecret), badge.CreatedByUserId, badge.CreatedDatetime.getTime(), badge.LastModifiedByUserId ?? null, badge.LastModifiedDatetime?.getTime() ?? null, badge.DeletedByUserId ?? null, badge.DeletedDatetime?.getTime() ?? null, Number(badge.IsDeleted), badge.Id
2024-09-28 01:31:46 +01:00
]);
}
return badge;
}
2024-09-26 10:31:24 +01:00
}
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;
2024-10-08 11:04:46 +01:00
badge.IsSecret = dbBadge.IsSecret[0] === 1;
2024-09-26 10:31:24 +01:00
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);
2024-10-08 11:04:46 +01:00
badge.IsDeleted = dbBadge.IsDeleted[0] === 1;
2024-09-26 10:31:24 +01:00
}