EUS/repos/DomainRepo.ts

81 lines
No EOL
3.4 KiB
TypeScript

import Domain from "../entities/Domain";
import Database from "../objects/Database";
export default class DomainRepo {
public static async SelectAll() {
const dbDomain = await Database.Instance.query("SELECT * FROM Domain WHERE IsDeleted = 0");
const domainList = new Array<Domain>();
for (const row of dbDomain) {
const domain = new Domain();
PopulateDomainFromDB(domain, row);
domainList.push(domain);
}
return domainList;
}
public static async SelectByUserId(userId: number) {
const dbDomain = await Database.Instance.query("SELECT * FROM Domain WHERE IsDeleted = 0 AND UserId = ?", [ userId ]);
const domainList = new Array<Domain>();
for (const row of dbDomain) {
const domain = new Domain();
PopulateDomainFromDB(domain, row);
domainList.push(domain);
}
return domainList;
}
public static async SelectById(id: number) {
const dbDomain = await Database.Instance.query("SELECT * FROM Domain WHERE Id = ? LIMIT 1", [id]);
if (dbDomain == null || dbDomain.length === 0) {
return null;
} else {
const domain = new Domain();
PopulateDomainFromDB(domain, dbDomain[0]);
return domain;
}
}
public static async SelectByDomain(domain: string) {
const dbDomain = await Database.Instance.query("SELECT * FROM Domain WHERE Domain = ? AND IsDeleted = 0 LIMIT 1", [domain]);
if (dbDomain == null || dbDomain.length === 0) {
return null;
} else {
const domain = new Domain();
PopulateDomainFromDB(domain, dbDomain[0]);
return domain;
}
}
public static async InsertUpdate(domain: Domain) {
if (domain.Id === Number.MIN_VALUE) {
domain.Id = (await Database.Instance.query("INSERT Domain (UserId, DomainId, FileName, MediaTag, MediaType, Hash, FileSize, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
domain.UserId, Number(domain.HasHttps), domain.Domain, Number(domain.Active), domain.CreatedByUserId, domain.CreatedDatetime, domain.LastModifiedByUserId ?? null, domain.LastModifiedDatetime ?? null, domain.DeletedByUserId ?? null, domain.DeletedDatetime ?? null, Number(domain.IsDeleted)
]))[0]["Id"];
} else {
await Database.Instance.query(`UPDATE Media SET UserId = ?, HasHttps = ?, Domain = ?, Active = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
domain.UserId, Number(domain.HasHttps), domain.Domain, Number(domain.Active), domain.CreatedByUserId, domain.CreatedDatetime, domain.LastModifiedByUserId ?? null, domain.LastModifiedDatetime ?? null, domain.DeletedByUserId ?? null, domain.DeletedDatetime ?? null, Number(domain.IsDeleted), domain.Id
]);
}
return domain;
}
}
function PopulateDomainFromDB(domain: Domain, dbDomain: any) {
domain.Id = dbDomain.Id;
domain.UserId = dbDomain.UserId;
domain.HasHttps = dbDomain.HasHttps[0] === 1;
domain.Domain = dbDomain.Domain;
domain.Active = dbDomain.Active[0] === 1;
domain.CreatedByUserId = dbDomain.CreatedByUserId;
domain.CreatedDatetime = dbDomain.CreatedDatetime;
domain.LastModifiedByUserId = dbDomain.LastModifiedByUserId;
domain.LastModifiedDatetime = dbDomain.LastModifiedDatetime;
domain.DeletedByUserId = dbDomain.DeletedByUserId;
domain.DeletedDatetime = dbDomain.DeletedDatetime;
domain.IsDeleted = dbDomain.IsDeleted[0] === 1;
}