t00-multiuser/server/repos/PartyRepo.ts

53 lines
2.5 KiB
TypeScript
Raw Normal View History

2024-04-22 02:01:14 +01:00
import Database from "../objects/Database";
import Party from "../objects/Party";
import User from "../objects/User";
import RepoBase from "./RepoBase";
export default class PartyRepo {
public static async selectById(id:number) {
const dbParty = await Database.Instance.query("SELECT * FROM Party WHERE Id = ? LIMIT 1", [id]);
if (dbParty == null || dbParty.length === 0) {
return null;
} else {
const party = new Party();
populatePartyFromDB(party, dbParty[0]);
return party;
}
}
public static async selectByPartyRef(partyRef:string) {
const dbParty = await Database.Instance.query("SELECT * FROM Party WHERE PartyRef = ? LIMIT 1", [partyRef]);
if (dbParty == null || dbParty.length === 0) {
return null;
} else {
const party = new Party();
populatePartyFromDB(party, dbParty[0]);
return party;
}
}
public static async insertUpdate(user:User) {
if (user.Id === Number.MIN_VALUE) {
await Database.Instance.query("INSERT Party (Username, PasswordHash, PasswordSalt, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [
user.Username, user.PasswordHash, user.PasswordSalt, user.CreatedByUserId, user.CreatedDatetime.getTime(), user.LastModifiedByUserId, user.LastModifiedDatetime?.getTime(), user.DeletedByUserId, user.DeletedDatetime?.getTime(), Number(user.IsDeleted)
]);
} else {
await Database.Instance.query(`UPDATE Party SET Username = ?, PasswordHash = ?, PasswordSalt = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ?, WHERE Id = ?`, [
user.Username, user.PasswordHash, user.PasswordSalt, user.CreatedByUserId, user.CreatedDatetime.getTime(), user.LastModifiedByUserId, user.LastModifiedDatetime?.getTime(), user.DeletedByUserId, user.DeletedDatetime?.getTime(), Number(user.IsDeleted), user.Id
]);
}
}
}
function populatePartyFromDB(party:Party, dbParty:any) {
party.Id = dbParty.Id;
party.PartyRef = dbParty.PartyRef;
party.Name = dbParty.Name;
party.CreatedByUserId = dbParty.CreatedByUserId;
party.CreatedDatetime = dbParty.CreatedDatetime;
party.LastModifiedByUserId = dbParty.LastModifiedByUserId;
party.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbParty.LastModifiedDatetime);
party.DeletedByUserId = dbParty.DeletedByUserId;
party.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbParty.DeletedDatetime);
party.IsDeleted = dbParty.IsDeleted;
}