import Database from "../objects/Database"; import UserParty from "../objects/UserParty"; import RepoBase from "./RepoBase"; export default class UserPartyRepo { public static async selectById(id:number) { const dbUserParty = await Database.Instance.query("SELECT * FROM UserParty WHERE Id = ? AND IsDeleted = 0 LIMIT 1", [id]); if (dbUserParty == null || dbUserParty.length === 0) { return null; } else { const userParty = new UserParty(); populateUserPartyFromDB(userParty, dbUserParty[0]); return userParty; } } public static async selectByUserId(userId:number) { const dbUserParties = await Database.Instance.query("SELECT * FROM UserParty WHERE UserId = ? AND IsDeleted = 0", [userId]); if (dbUserParties == null || dbUserParties.length === 0) { return null; } else { const userParties = new Array(); for (const dbUserParty of dbUserParties) { const userParty = new UserParty(); populateUserPartyFromDB(userParty, dbUserParty[0]); userParties.push(userParty); } return userParties; } } public static async insertUpdate(userParty:UserParty) { if (userParty.Id === Number.MIN_VALUE) { await Database.Instance.query("INSERT UserParty (UserId, PartyId, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [ userParty.UserId, userParty.PartyId, userParty.CreatedByUserId, userParty.CreatedDatetime.getTime(), userParty.LastModifiedByUserId ?? null, userParty.LastModifiedDatetime?.getTime() ?? null, userParty.DeletedByUserId ?? null, userParty.DeletedDatetime?.getTime() ?? null, Number(userParty.IsDeleted) ]); } else { await Database.Instance.query(`UPDATE UserParty SET UserId = ?, PartyId = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ?, WHERE Id = ?`, [ userParty.UserId, userParty.PartyId, userParty.CreatedByUserId, userParty.CreatedDatetime.getTime(), userParty.LastModifiedByUserId ?? null, userParty.LastModifiedDatetime?.getTime() ?? null, userParty.DeletedByUserId ?? null, userParty.DeletedDatetime?.getTime() ?? null, Number(userParty.IsDeleted), userParty.Id ]); } } } function populateUserPartyFromDB(userParty:UserParty, dbUserParty:any) { userParty.Id = dbUserParty.Id; userParty.UserId = dbUserParty.UserId; userParty.PartyId = dbUserParty.PartyId; userParty.CreatedByUserId = dbUserParty.CreatedByUserId; userParty.CreatedDatetime = new Date(dbUserParty.CreatedDatetime); userParty.LastModifiedByUserId = dbUserParty.LastModifiedByUserId; userParty.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUserParty.LastModifiedDatetime); userParty.DeletedByUserId = dbUserParty.DeletedByUserId; userParty.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUserParty.DeletedDatetime); userParty.IsDeleted = dbUserParty.IsDeleted; }