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) {
|
|
|
|
badge.Id = (await Database.Instance.query("INSERT Badge (Name, Description, ImageUrl, ForUrl, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
|
|
|
|
badge.Name, badge.Description, badge.ImageUrl, badge.ForUrl, badge.CreatedByUserId, badge.CreatedDatetime.getTime(), badge.LastModifiedByUserId ?? null, badge.LastModifiedDatetime?.getTime() ?? null, badge.DeletedByUserId ?? null, badge.DeletedDatetime?.getTime() ?? null, Number(badge.IsDeleted)
|
|
|
|
]))[0]["Id"];
|
|
|
|
} else {
|
|
|
|
await Database.Instance.query(`UPDATE Badge SET Name = ?, Description = ?, ImageUrl = ?, ForUrl = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
|
|
|
|
badge.Name, badge.Description, badge.ImageUrl, badge.ForUrl, badge.CreatedByUserId, badge.CreatedDatetime.getTime(), badge.LastModifiedByUserId ?? null, badge.LastModifiedDatetime?.getTime() ?? null, badge.DeletedByUserId ?? null, badge.DeletedDatetime?.getTime() ?? null, Number(badge.IsDeleted), badge.Id
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
}
|