52 lines
2.4 KiB
TypeScript
52 lines
2.4 KiB
TypeScript
|
import Database from "../objects/Database";
|
||
|
import User from "../objects/User";
|
||
|
|
||
|
export default class UsersRepo {
|
||
|
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 insertUpdate(user:User) {
|
||
|
if (user.Id === Number.MIN_VALUE) {
|
||
|
await Database.Instance.query("INSERT users (Username, PasswordHash, PasswordSalt, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [
|
||
|
user.Username, user.PasswordHash, user.PasswordSalt, user.CreatedByUserId, user.CreatedDatetime.getTime(), user.LastModifiedByUserId, user.LastModifiedDatetime.getTime(), user.DeletedByUserId, user.DeletedDatetime.getTime(), Number(user.IsDeleted)
|
||
|
]);
|
||
|
} else {
|
||
|
await Database.Instance.query(`UPDATE users SET Username = ?, PasswordHash = ?, PasswordSalt = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ?, WHERE Id = ?`, [
|
||
|
user.Username, user.PasswordHash, user.PasswordSalt, user.CreatedByUserId, user.CreatedDatetime.getTime(), user.LastModifiedByUserId, user.LastModifiedDatetime.getTime(), user.DeletedByUserId, user.DeletedDatetime.getTime(), Number(user.IsDeleted), user.Id
|
||
|
]);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function populateUserFromDB(user:User, dbUser:any) {
|
||
|
user.Id = dbUser.Id;
|
||
|
user.Username = dbUser.Username;
|
||
|
user.PasswordHash = dbUser.PasswordHash;
|
||
|
user.PasswordSalt = dbUser.PasswordSalt;
|
||
|
user.CreatedByUserId = dbUser.CreatedByUserId;
|
||
|
user.CreatedDatetime = dbUser.CreatedDatetime;
|
||
|
user.LastModifiedByUserId = dbUser.LastModifiedByUserId;
|
||
|
user.LastModifiedDatetime = dbUser.LastModifiedDatetime;
|
||
|
user.DeletedByUserId = dbUser.DeletedByUserId;
|
||
|
user.DeletedDatetime = dbUser.DeletedDatetime;
|
||
|
user.IsDeleted = dbUser.IsDeleted;
|
||
|
}
|