import UserBadgeListItem from "../entities/list/UserBadgeListItem"; import UserBadge from "../entities/UserBadge"; import Database from "../objects/Database"; import RepoBase from "./RepoBase"; export default abstract class UserBadgeRepo { public static async selectById(id:number) { const dbUserBadge = await Database.Instance.query("SELECT * FROM UserBadge WHERE Id = ? AND IsDeleted = 0 LIMIT 1", [id]); if (dbUserBadge == null || dbUserBadge.length === 0) { return null; } else { const userBadge = new UserBadge(); populateUserBadgeFromDB(userBadge, dbUserBadge[0]); return userBadge; } } public static async selectAll() { const dbUserBadge = await Database.Instance.query("SELECT * FROM UserBadge WHERE IsDeleted = 0", []); const userBadges = new Array(); for (const row of dbUserBadge) { const userBadge = new UserBadge(); populateUserBadgeFromDB(userBadge, row); userBadges.push(userBadge); } return userBadges; } public static async selectByUserId(userId:number) { const dbUserBadge = await Database.Instance.query("SELECT * FROM UserBadge WHERE UserId = ? AND IsDeleted = 0", [ userId ]); const userBadges = new Array(); for (const row of dbUserBadge) { const userBadge = new UserBadge(); populateUserBadgeFromDB(userBadge, row); userBadges.push(userBadge); } return userBadges; } public static async selectByUserIdBadgeId(userId:number, badgeId:number) { const dbUserBadge = await Database.Instance.query("SELECT * FROM UserBadge WHERE UserId = ? AND BadgeId = ? AND IsDeleted = 0 LIMIT 1", [ userId, badgeId ]); if (dbUserBadge == null || dbUserBadge.length === 0) { return null; } else { const userBadge = new UserBadge(); populateUserBadgeFromDB(userBadge, dbUserBadge[0]); return userBadge; } } public static async selectUnlockedBadgeCount() { const countResult = await Database.Instance.query("SELECT COUNT(Id) FROM `UserBadge` WHERE IsDeleted = 0 LIMIT 1", []); return countResult[0]["COUNT(Id)"] as number; } public static async selectUnlockList(badgeQuery?: string, userQuery?: string) { const dbUserBadgeListItems = await Database.Instance.query('SELECT UserBadge.Id AS "Id", Badge.Id AS "BadgeId", `User`.`Username`, UserBadge.CreatedDatetime AS "UnlockDatetime" FROM UserBadge JOIN Badge ON Badge.Id = UserBadge.BadgeId AND Badge.IsDeleted = 0 JOIN `User` ON `User`.Id = UserBadge.UserId AND `User`.IsDeleted = 0 WHERE UserBadge.IsDeleted = 0 AND `User`.`Username` LIKE ? AND Badge.Name LIKE ?;', [ userQuery ? `%${userQuery}%` : "%%", badgeQuery ? `%${badgeQuery}%` : "%%" ]); const userBadgeListItems = new Array(); for (const row of dbUserBadgeListItems) { const userBadgeListItem = new UserBadgeListItem(); populateUserBadgeListItemFromDB(userBadgeListItem, row); userBadgeListItems.push(userBadgeListItem); } return userBadgeListItems; } public static async insertUpdate(userBadge:UserBadge) { if (userBadge.Id === Number.MIN_VALUE) { userBadge.Id = (await Database.Instance.query("INSERT UserBadge (UserId, BadgeId, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [ userBadge.UserId, userBadge.BadgeId, userBadge.CreatedByUserId, userBadge.CreatedDatetime.getTime(), userBadge.LastModifiedByUserId ?? null, userBadge.LastModifiedDatetime?.getTime() ?? null, userBadge.DeletedByUserId ?? null, userBadge.DeletedDatetime?.getTime() ?? null, Number(userBadge.IsDeleted) ]))[0]["Id"]; } else { await Database.Instance.query(`UPDATE UserBadge SET UserId = ?, BadgeId = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [ userBadge.UserId, userBadge.BadgeId, userBadge.CreatedByUserId, userBadge.CreatedDatetime.getTime(), userBadge.LastModifiedByUserId ?? null, userBadge.LastModifiedDatetime?.getTime() ?? null, userBadge.DeletedByUserId ?? null, userBadge.DeletedDatetime?.getTime() ?? null, Number(userBadge.IsDeleted), userBadge.Id ]); } return userBadge; } } function populateUserBadgeFromDB(userBadge:UserBadge, dbBadge:any) { userBadge.Id = dbBadge.Id; userBadge.UserId = dbBadge.UserId; userBadge.BadgeId = dbBadge.BadgeId; userBadge.CreatedByUserId = dbBadge.CreatedByUserId; userBadge.CreatedDatetime = new Date(dbBadge.CreatedDatetime); userBadge.LastModifiedByUserId = dbBadge.LastModifiedByUserId; userBadge.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbBadge.LastModifiedDatetime); userBadge.DeletedByUserId = dbBadge.DeletedByUserId; userBadge.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbBadge.DeletedDatetime); userBadge.IsDeleted = dbBadge.IsDeleted[0] === 1; } function populateUserBadgeListItemFromDB(userBadgeListItem:UserBadgeListItem, dbBadgeListItem:any) { userBadgeListItem.Id = dbBadgeListItem.Id; userBadgeListItem.BadgeId = dbBadgeListItem.BadgeId; userBadgeListItem.Username = dbBadgeListItem.Username; userBadgeListItem.UnlockDatetime = new Date(dbBadgeListItem.UnlockDatetime); }