import Database from "../objects/Database"; import RepoBase from "./RepoBase"; import User from "../entities/User"; export default abstract class UserRepo { public static async SelectAll() { const dbUser = await Database.Instance.query("SELECT * FROM User WHERE IsDeleted = 0"); const users = new Array(); for (const row of dbUser) { const user = new User(); PopulateUserFromDB(user, row); users.push(user); } return users; } public static async SelectById(id:number) { const dbUser = await Database.Instance.query("SELECT * FROM User WHERE Id = ? LIMIT 1", [id]); if (dbUser == null || dbUser.length === 0) { return null; } else { const user = new User(); PopulateUserFromDB(user, dbUser[0]); return user; } } public static async SelectByUsername(username:string) { const dbUser = await Database.Instance.query("SELECT * FROM User WHERE Username = ? LIMIT 1", [username]); if (dbUser == null || dbUser.length === 0) { return null; } else { const user = new User(); PopulateUserFromDB(user, dbUser[0]); return user; } } public static async SelectByEmailAddress(emailAddress:string) { const dbUser = await Database.Instance.query("SELECT * FROM User WHERE EmailAddress = ? LIMIT 1", [emailAddress]); if (dbUser == null || dbUser.length === 0) { return null; } else { const user = new User(); PopulateUserFromDB(user, dbUser[0]); return user; } } public static async InsertUpdate(user:User) { if (user.Id === Number.MIN_VALUE) { user.Id = (await Database.Instance.query("INSERT User (UserTypeId, Username, PasswordHash, PasswordSalt, ApiKey, UploadKey, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [ user.UserType, user.Username, user.PasswordHash, user.PasswordSalt, user.ApiKey, user.UploadKey, user.CreatedByUserId, user.CreatedDatetime.getTime(), user.LastModifiedByUserId ?? null, user.LastModifiedDatetime?.getTime() ?? null, user.DeletedByUserId ?? null, user.DeletedDatetime?.getTime() ?? null, Number(user.IsDeleted) ]))[0]["Id"]; } else { await Database.Instance.query(`UPDATE User SET UserTypeId = ?, Username = ?, PasswordHash = ?, PasswordSalt = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [ user.UserType, user.Username, user.PasswordHash, user.PasswordSalt, user.CreatedByUserId, user.CreatedDatetime.getTime(), user.LastModifiedByUserId ?? null, user.LastModifiedDatetime?.getTime() ?? null, user.DeletedByUserId ?? null, user.DeletedDatetime?.getTime() ?? null, Number(user.IsDeleted), user.Id ]); } return user; } } function PopulateUserFromDB(user:User, dbUser:any) { user.Id = dbUser.Id; user.UserType = dbUser.UserTypeId; user.Username = dbUser.Username; user.PasswordHash = dbUser.PasswordHash; user.PasswordSalt = dbUser.PasswordSalt; user.CreatedByUserId = dbUser.CreatedByUserId; user.CreatedDatetime = new Date(dbUser.CreatedDatetime); user.LastModifiedByUserId = dbUser.LastModifiedByUserId; user.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.LastModifiedDatetime); user.DeletedByUserId = dbUser.DeletedByUserId; user.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.DeletedDatetime); user.IsDeleted = dbUser.IsDeleted[0] === 1; }