t00-multiuser/server/repos/BadgeRepo.ts

79 lines
3.5 KiB
TypeScript
Raw Permalink 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() {
2024-10-12 11:52:13 +01:00
const dbBadge = await Database.Instance.query("SELECT * FROM Badge WHERE IsDeleted = 0", []);
2024-09-26 10:31:24 +01:00
const badges = new Array<Badge>();
2024-10-12 11:52:13 +01:00
for (const row of dbBadge) {
2024-09-26 10:31:24 +01:00
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() {
2024-10-12 11:52:13 +01:00
const countResult = await Database.Instance.query("SELECT COUNT(Id) FROM `Badge` WHERE IsDeleted = 0 LIMIT 1", []);
2024-09-28 14:31:02 +01:00
2024-10-12 11:52:13 +01:00
return countResult[0]["COUNT(Id)"] as number;
}
public static async selectList(query?: string) {
const dbBadge = await Database.Instance.query("SELECT * FROM Badge WHERE IsDeleted = 0 AND Name LIKE ?", [ query ? `%${query}%` : "%%" ]);
const badges = new Array<Badge>();
for (const row of dbBadge) {
const badge = new Badge();
populateBadgeFromDB(badge, row);
badges.push(badge);
}
return badges;
2024-09-28 14:31:02 +01:00
}
2024-09-28 01:31:46 +01:00
public static async insertUpdate(badge:Badge) {
if (badge.Id === Number.MIN_VALUE) {
2024-10-13 16:55:23 +01:00
badge.Id = (await Database.Instance.query("INSERT Badge (Name, Description, Hint, ImageUrl, ForUrl, IsSecret, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
2024-10-13 14:24:34 +01:00
badge.Name, badge.Description, badge.Hint, 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-13 14:24:34 +01:00
await Database.Instance.query(`UPDATE Badge SET Name = ?, Description = ?, Hint = ?, ImageUrl = ?, ForUrl = ?, IsSecret = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
badge.Name, badge.Description, badge.Hint, 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;
2024-10-13 14:24:34 +01:00
badge.Hint = dbBadge.Hint;
2024-09-26 10:31:24 +01:00
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
}