106 lines
No EOL
4.2 KiB
TypeScript
106 lines
No EOL
4.2 KiB
TypeScript
import Database from "../objects/Database";
|
|
import Media from "../entities/Media";
|
|
|
|
export default abstract class MediaRepo {
|
|
public static async SelectAll() {
|
|
const dbMedia = await Database.Instance.query("SELECT * FROM Media WHERE IsDeleted = 0");
|
|
const mediaList = new Array<Media>();
|
|
|
|
for (const row of dbMedia) {
|
|
const media = new Media();
|
|
PopulateMediaFromDB(media, row);
|
|
mediaList.push(media);
|
|
}
|
|
|
|
return mediaList;
|
|
}
|
|
|
|
public static async SelectById(id: number) {
|
|
const dbMedia = await Database.Instance.query("SELECT * FROM Media WHERE Id = ? LIMIT 1", [id]);
|
|
if (dbMedia == null || dbMedia.length === 0) {
|
|
return null;
|
|
} else {
|
|
const media = new Media();
|
|
PopulateMediaFromDB(media, dbMedia[0]);
|
|
return media;
|
|
}
|
|
}
|
|
|
|
public static async SelectByMediaTag(mediaTag: string) {
|
|
const dbMedia = await Database.Instance.query("SELECT * FROM Media WHERE MediaTag = ? LIMIT 1", [mediaTag]);
|
|
if (dbMedia == null || dbMedia.length === 0) {
|
|
return null;
|
|
} else {
|
|
const media = new Media();
|
|
PopulateMediaFromDB(media, dbMedia[0]);
|
|
return media;
|
|
}
|
|
}
|
|
|
|
public static async SelectByUserHash(currentUserId: number, hash: string) {
|
|
const dbMedia = await Database.Instance.query("SELECT * FROM Media WHERE UserId = ? AND Hash = ? LIMIT 1", [currentUserId, hash]);
|
|
if (dbMedia == null || dbMedia.length === 0) {
|
|
return null;
|
|
} else {
|
|
const media = new Media();
|
|
PopulateMediaFromDB(media, dbMedia[0]);
|
|
return media;
|
|
}
|
|
}
|
|
|
|
public static async SelectByHash(hash: string) {
|
|
const dbMedia = await Database.Instance.query("SELECT * FROM Media WHERE Hash = ? LIMIT 1", [hash]);
|
|
if (dbMedia == null || dbMedia.length === 0) {
|
|
return null;
|
|
} else {
|
|
const media = new Media();
|
|
PopulateMediaFromDB(media, dbMedia[0]);
|
|
return media;
|
|
}
|
|
}
|
|
|
|
public static async SelectRecentMedia(userId: number, amount: number) {
|
|
const dbMedia = await Database.Instance.query("SELECT * FROM Media WHERE UserId = ? ORDER BY Id DESC LIMIT ?", [userId, amount]);
|
|
const mediaList = new Array<Media>();
|
|
|
|
for (const row of dbMedia) {
|
|
const media = new Media();
|
|
PopulateMediaFromDB(media, row);
|
|
mediaList.push(media);
|
|
}
|
|
|
|
return mediaList;
|
|
}
|
|
|
|
public static async InsertUpdate(media: Media) {
|
|
if (media.Id === Number.MIN_VALUE) {
|
|
media.Id = (await Database.Instance.query("INSERT Media (UserId, DomainId, FileName, MediaTag, MediaType, Hash, FileSize, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
|
|
media.UserId, media.DomainId, media.FileName, media.MediaTag, media.MediaType, media.Hash, media.FileSize, media.CreatedByUserId, media.CreatedDatetime, media.LastModifiedByUserId ?? null, media.LastModifiedDatetime ?? null, media.DeletedByUserId ?? null, media.DeletedDatetime ?? null, Number(media.IsDeleted)
|
|
]))[0]["Id"];
|
|
} else {
|
|
await Database.Instance.query(`UPDATE Media SET UserId = ?, DomainId = ?, FileName = ?, MediaTag = ?, MediaType = ?, Hash = ?, FileSize = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
|
|
media.UserId, media.DomainId, media.FileName, media.MediaTag, media.Hash, media.FileSize, media.CreatedByUserId, media.CreatedDatetime, media.LastModifiedByUserId ?? null, media.LastModifiedDatetime ?? null, media.DeletedByUserId ?? null, media.DeletedDatetime ?? null, Number(media.IsDeleted), media.Id
|
|
]);
|
|
}
|
|
|
|
return media;
|
|
}
|
|
}
|
|
|
|
function PopulateMediaFromDB(media: Media, dbMedia: any) {
|
|
media.Id = dbMedia.Id;
|
|
media.UserId = dbMedia.UserId;
|
|
media.DomainId = dbMedia.DomainId;
|
|
media.FileName = dbMedia.FileName;
|
|
media.MediaTag = dbMedia.MediaTag;
|
|
media.MediaType = dbMedia.MediaType;
|
|
media.Hash = dbMedia.Hash;
|
|
media.FileSize = dbMedia.FileSize;
|
|
media.CreatedByUserId = dbMedia.CreatedByUserId;
|
|
media.CreatedDatetime = dbMedia.CreatedDatetime;
|
|
media.LastModifiedByUserId = dbMedia.LastModifiedByUserId;
|
|
media.LastModifiedDatetime = dbMedia.LastModifiedDatetime;
|
|
media.DeletedByUserId = dbMedia.DeletedByUserId;
|
|
media.DeletedDatetime = dbMedia.DeletedDatetime;
|
|
media.IsDeleted = dbMedia.IsDeleted[0] === 1;
|
|
} |