137 lines
No EOL
5.4 KiB
TypeScript
137 lines
No EOL
5.4 KiB
TypeScript
import Database from "../objects/Database";
|
|
import Media from "../entities/Media";
|
|
import MediaCount from "../entities/MediaCount";
|
|
|
|
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 SelectTotalMediaSizeByUserId(userId: number) {
|
|
const dbCount = await Database.Instance.query('SELECT SUM(FileSize) FROM Media WHERE IsDeleted = 0 AND UserId = ?', [ userId ]);
|
|
|
|
return dbCount[0]["SUM(FileSize)"] ?? 0;
|
|
}
|
|
|
|
public static async SelectTotalMediaByUserId(userId: number) {
|
|
const dbCount = await Database.Instance.query('SELECT COUNT(Id) FROM Media WHERE IsDeleted = 0 AND UserId = ?', [ userId ]);
|
|
|
|
return dbCount[0]["COUNT(Id)"];
|
|
}
|
|
|
|
public static async SelectMediaTypeCountsByUserId(userId: number) {
|
|
const dbMedia = await Database.Instance.query('SELECT MediaType AS "Type", COUNT(Id) AS "Count" FROM Media WHERE IsDeleted = 0 AND UserId = ? GROUP BY MediaType ORDER BY "Count"', [ userId ]);
|
|
const mediaCountList = new Array<MediaCount>();
|
|
|
|
for (const row of dbMedia) {
|
|
const mediaCount = new MediaCount();
|
|
PopulateMediaCountFromDB(mediaCount, row);
|
|
mediaCountList.push(mediaCount);
|
|
}
|
|
|
|
return mediaCountList;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
function PopulateMediaCountFromDB(mediaCount: MediaCount, dbMediaCount: any) {
|
|
mediaCount.Type = dbMediaCount.Type;
|
|
mediaCount.Count = dbMediaCount.Count;
|
|
} |