54 lines
No EOL
2.9 KiB
TypeScript
54 lines
No EOL
2.9 KiB
TypeScript
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 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 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, dbUser:any) {
|
|
userBadge.Id = dbUser.Id;
|
|
userBadge.UserId = dbUser.UserId;
|
|
userBadge.BadgeId = dbUser.BadgeId;
|
|
userBadge.CreatedByUserId = dbUser.CreatedByUserId;
|
|
userBadge.CreatedDatetime = new Date(dbUser.CreatedDatetime);
|
|
userBadge.LastModifiedByUserId = dbUser.LastModifiedByUserId;
|
|
userBadge.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.LastModifiedDatetime);
|
|
userBadge.DeletedByUserId = dbUser.DeletedByUserId;
|
|
userBadge.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.DeletedDatetime);
|
|
userBadge.IsDeleted = dbUser.IsDeleted === 1;
|
|
} |