69 lines
No EOL
3.1 KiB
TypeScript
69 lines
No EOL
3.1 KiB
TypeScript
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 = ? AND IsDeleted = 0 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 selectByUserId(userId:number) {
|
|
const dbParties = await Database.Instance.query("SELECT Party.* FROM Party JOIN UserParty ON Party.Id = UserParty.PartyId WHERE UserParty.UserId = ? AND UserParty.IsDeleted = 0 AND Party.IsDeleted = 0", [userId]);
|
|
const parties = new Array<Party>();
|
|
if (dbParties == null || dbParties.length === 0) {
|
|
return parties;
|
|
} else {
|
|
for (const dbParty of dbParties) {
|
|
const party = new Party();
|
|
populatePartyFromDB(party, dbParty);
|
|
parties.push(party);
|
|
}
|
|
|
|
return parties;
|
|
}
|
|
}
|
|
|
|
public static async selectByPartyRef(partyRef:string) {
|
|
const dbParty = await Database.Instance.query("SELECT * FROM Party WHERE PartyRef = ? AND IsDeleted = 0 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(party:Party) {
|
|
if (party.Id === Number.MIN_VALUE) {
|
|
await Database.Instance.query("INSERT Party (Name, PartyRef, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [
|
|
party.Name, party.PartyRef, party.CreatedByUserId, party.CreatedDatetime.getTime(), party.LastModifiedByUserId ?? null, party.LastModifiedDatetime?.getTime() ?? null, party.DeletedByUserId ?? null, party.DeletedDatetime?.getTime() ?? null, Number(party.IsDeleted)
|
|
]);
|
|
} else {
|
|
await Database.Instance.query(`UPDATE Party SET Name = ?, PartyRef = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ?, WHERE Id = ?`, [
|
|
party.Name, party.PartyRef, party.CreatedByUserId, party.CreatedDatetime.getTime(), party.LastModifiedByUserId ?? null, party.LastModifiedDatetime?.getTime() ?? null, party.DeletedByUserId ?? null, party.DeletedDatetime?.getTime() ?? null, Number(party.IsDeleted), party.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;
|
|
} |