t00-multiuser/server/repos/UserPartyRepo.ts

86 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-04-23 17:01:25 +01:00
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<UserParty>();
for (const dbUserParty of dbUserParties) {
const userParty = new UserParty();
populateUserPartyFromDB(userParty, dbUserParty[0]);
userParties.push(userParty);
}
return userParties;
}
}
public static async selectByUserIdPartyId(userId:number, partyId:number) {
const dbUserParty = await Database.Instance.query("SELECT * FROM UserParty WHERE UserId = ? AND PartyId = ? AND IsDeleted = 0", [userId, partyId]);
if (dbUserParty == null || dbUserParty.length === 0) {
return null;
} else {
const userParty = new UserParty();
populateUserPartyFromDB(userParty, dbUserParty[0]);
return userParty;
}
}
public static async deactivateAll(userId:number) {
await Database.Instance.query("UPDATE UserParty SET IsActive = 0, LastModifiedByUserId = ?, LastModifiedDatetime = ? WHERE UserId = ? AND IsActive = 1", [
userId, Date.now(), userId
]);
}
public static async selectActive(userId:number) {
const dbUserParty = await Database.Instance.query("SELECT * FROM UserParty WHERE UserId = ? AND IsActive = 1 AND IsDeleted = 0 LIMIT 1", [userId]);
if (dbUserParty == null || dbUserParty.length === 0) {
return null;
} else {
const userParty = new UserParty();
populateUserPartyFromDB(userParty, dbUserParty[0]);
return userParty;
}
}
2024-04-23 17:01:25 +01:00
public static async insertUpdate(userParty:UserParty) {
if (userParty.Id === Number.MIN_VALUE) {
await Database.Instance.query("INSERT UserParty (UserId, PartyId, IsActive, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [
userParty.UserId, userParty.PartyId, Number(userParty.IsActive), userParty.CreatedByUserId, userParty.CreatedDatetime.getTime(), userParty.LastModifiedByUserId ?? null, userParty.LastModifiedDatetime?.getTime() ?? null, userParty.DeletedByUserId ?? null, userParty.DeletedDatetime?.getTime() ?? null, Number(userParty.IsDeleted)
2024-04-23 17:01:25 +01:00
]);
} else {
await Database.Instance.query(`UPDATE UserParty SET UserId = ?, PartyId = ?, IsActive = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
userParty.UserId, userParty.PartyId, Number(userParty.IsActive), userParty.CreatedByUserId, userParty.CreatedDatetime.getTime(), userParty.LastModifiedByUserId ?? null, userParty.LastModifiedDatetime?.getTime() ?? null, userParty.DeletedByUserId ?? null, userParty.DeletedDatetime?.getTime() ?? null, Number(userParty.IsDeleted), userParty.Id
2024-04-23 17:01:25 +01:00
]);
}
}
}
function populateUserPartyFromDB(userParty:UserParty, dbUserParty:any) {
userParty.Id = dbUserParty.Id;
userParty.UserId = dbUserParty.UserId;
userParty.PartyId = dbUserParty.PartyId;
userParty.IsActive = dbUserParty.IsActive;
2024-04-23 17:01:25 +01:00
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;
}