90 lines
No EOL
3.7 KiB
TypeScript
90 lines
No EOL
3.7 KiB
TypeScript
import Database from "../objects/Database";
|
|
import Party from "../entities/Party";
|
|
import User from "../entities/User";
|
|
import RepoBase from "./RepoBase";
|
|
|
|
export default class PartyRepo {
|
|
public static async selectAll() {
|
|
const dbUser = await Database.Instance.query("SELECT * FROM Party WHERE IsDeleted = 0");
|
|
const parties = new Array<Party>();
|
|
|
|
for (const row of dbUser) {
|
|
const party = new Party();
|
|
populatePartyFromDB(party, row);
|
|
parties.push(party);
|
|
}
|
|
|
|
return parties;
|
|
}
|
|
|
|
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 selectPartyCount() {
|
|
const countResult = await Database.Instance.query("SELECT COUNT(Id) FROM `Party` WHERE IsDeleted = 0 LIMIT 1");
|
|
|
|
return countResult[0]["COUNT(Id)"];
|
|
}
|
|
|
|
public static async insertUpdate(party:Party) {
|
|
if (party.Id === Number.MIN_VALUE) {
|
|
party.Id = (await Database.Instance.query("INSERT Party (Name, PartyRef, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING 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)
|
|
]))[0]["Id"];
|
|
} 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
|
|
]);
|
|
}
|
|
|
|
return party;
|
|
}
|
|
}
|
|
|
|
function populatePartyFromDB(party:Party, dbParty:any) {
|
|
party.Id = dbParty.Id;
|
|
party.PartyRef = dbParty.PartyRef;
|
|
party.Name = dbParty.Name;
|
|
party.CreatedByUserId = dbParty.CreatedByUserId;
|
|
party.CreatedDatetime = new Date(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 === 1;
|
|
} |