2024-04-22 02:01:14 +01:00
import Database from "../objects/Database" ;
2024-09-19 00:41:40 +01:00
import Party from "../entities/Party" ;
import User from "../entities/User" ;
2024-04-22 02:01:14 +01:00
import RepoBase from "./RepoBase" ;
export default class PartyRepo {
2024-09-26 00:47:08 +01:00
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 ;
}
2024-04-22 02:01:14 +01:00
public static async selectById ( id :number ) {
2024-04-23 17:01:25 +01:00
const dbParty = await Database . Instance . query ( "SELECT * FROM Party WHERE Id = ? AND IsDeleted = 0 LIMIT 1" , [ id ] ) ;
2024-04-22 02:01:14 +01:00
if ( dbParty == null || dbParty . length === 0 ) {
return null ;
} else {
const party = new Party ( ) ;
populatePartyFromDB ( party , dbParty [ 0 ] ) ;
return party ;
}
}
2024-04-23 17:01:25 +01:00
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 ;
}
}
2024-04-22 02:01:14 +01:00
public static async selectByPartyRef ( partyRef :string ) {
2024-04-23 17:01:25 +01:00
const dbParty = await Database . Instance . query ( "SELECT * FROM Party WHERE PartyRef = ? AND IsDeleted = 0 LIMIT 1" , [ partyRef ] ) ;
2024-04-22 02:01:14 +01:00
if ( dbParty == null || dbParty . length === 0 ) {
return null ;
} else {
const party = new Party ( ) ;
populatePartyFromDB ( party , dbParty [ 0 ] ) ;
return party ;
}
}
2024-09-26 00:47:08 +01:00
public static async selectPartyCount() {
2024-09-28 14:31:02 +01:00
const countResult = await Database . Instance . query ( "SELECT COUNT(Id) FROM `Party` WHERE IsDeleted = 0 LIMIT 1" ) ;
2024-09-26 00:47:08 +01:00
return countResult [ 0 ] [ "COUNT(Id)" ] ;
}
2024-04-23 17:01:25 +01:00
public static async insertUpdate ( party :Party ) {
if ( party . Id === Number . MIN_VALUE ) {
2024-09-28 14:31:02 +01:00
party . Id = ( await Database . Instance . query ( "INSERT Party (Name, PartyRef, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;" , [
2024-04-23 17:01:25 +01:00
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 )
2024-09-28 14:31:02 +01:00
] ) ) [ 0 ] [ "Id" ] ;
2024-04-22 02:01:14 +01:00
} else {
2024-09-23 23:55:00 +01:00
await Database . Instance . query ( "UPDATE Party SET Name = ?, PartyRef = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?" , [
2024-04-23 17:01:25 +01:00
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
2024-04-22 02:01:14 +01:00
] ) ;
}
2024-09-28 14:31:02 +01:00
return party ;
2024-04-22 02:01:14 +01:00
}
}
function populatePartyFromDB ( party :Party , dbParty :any ) {
party . Id = dbParty . Id ;
party . PartyRef = dbParty . PartyRef ;
party . Name = dbParty . Name ;
party . CreatedByUserId = dbParty . CreatedByUserId ;
2024-09-23 23:55:00 +01:00
party . CreatedDatetime = new Date ( dbParty . CreatedDatetime ) ;
2024-04-22 02:01:14 +01:00
party . LastModifiedByUserId = dbParty . LastModifiedByUserId ;
party . LastModifiedDatetime = RepoBase . convertNullableDatetimeIntToDate ( dbParty . LastModifiedDatetime ) ;
party . DeletedByUserId = dbParty . DeletedByUserId ;
party . DeletedDatetime = RepoBase . convertNullableDatetimeIntToDate ( dbParty . DeletedDatetime ) ;
2024-09-19 00:41:40 +01:00
party . IsDeleted = dbParty . IsDeleted === 1 ;
2024-04-22 02:01:14 +01:00
}