EUS/repos/DomainRepo.ts
2025-01-06 06:53:13 +00:00

68 lines
No EOL
3 KiB
TypeScript

import Domain from "../entities/Domain";
import Database from "../objects/Database";
export default class DomainRepo {
public static async SelectAll() {
const dbMedia = await Database.Instance.query("SELECT * FROM Domain WHERE IsDeleted = 0");
const mediaList = new Array<Domain>();
for (const row of dbMedia) {
const media = new Domain();
PopulateDomainFromDB(media, row);
mediaList.push(media);
}
return mediaList;
}
public static async SelectById(id: number) {
const dbMedia = await Database.Instance.query("SELECT * FROM Domain WHERE Id = ? LIMIT 1", [id]);
if (dbMedia == null || dbMedia.length === 0) {
return null;
} else {
const media = new Domain();
PopulateDomainFromDB(media, dbMedia[0]);
return media;
}
}
public static async SelectByDomain(domain: string) {
const dbMedia = await Database.Instance.query("SELECT * FROM Domain WHERE Domain = ? AND IsDeleted = 0 LIMIT 1", [domain]);
if (dbMedia == null || dbMedia.length === 0) {
return null;
} else {
const media = new Domain();
PopulateDomainFromDB(media, dbMedia[0]);
return media;
}
}
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;
}