87 lines
No EOL
3.6 KiB
TypeScript
87 lines
No EOL
3.6 KiB
TypeScript
import Database from "../objects/Database";
|
|
import User from "../entities/User";
|
|
import RepoBase from "./RepoBase";
|
|
|
|
export default class UserRepo {
|
|
public static async selectAll() {
|
|
const dbUser = await Database.Instance.query("SELECT * FROM User WHERE IsDeleted = 0");
|
|
const users = new Array<User>();
|
|
|
|
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 selectByAPIKey(apiKey:string) {
|
|
const dbUser = await Database.Instance.query("SELECT * FROM User WHERE APIKey = ? LIMIT 1", [apiKey]);
|
|
if (dbUser == null || dbUser.length === 0) {
|
|
return null;
|
|
} else {
|
|
const user = new User();
|
|
populateUserFromDB(user, dbUser[0]);
|
|
return user;
|
|
}
|
|
}
|
|
|
|
public static async selectUserCount() {
|
|
const countResult = await Database.Instance.query("SELECT COUNT(Id) FROM `User` WHERE IsDeleted = 0 LIMIT 1");
|
|
|
|
return countResult[0]["COUNT(Id)"];
|
|
}
|
|
|
|
public static async insertUpdate(user:User) {
|
|
if (user.Id === Number.MIN_VALUE) {
|
|
user.Id = (await Database.Instance.query("INSERT User (UserLevelId, Username, PasswordHash, PasswordSalt, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
|
|
user.UserLevel, 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)
|
|
]))[0]["Id"];
|
|
} else {
|
|
await Database.Instance.query(`UPDATE User SET UserLevelId = ?, Username = ?, PasswordHash = ?, PasswordSalt = ?, APIKey = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
|
|
user.UserLevel, user.Username, user.PasswordHash, user.PasswordSalt, user.APIKey, 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.UserLevel = dbUser.UserLevelId;
|
|
user.Username = dbUser.Username;
|
|
user.PasswordHash = dbUser.PasswordHash;
|
|
user.PasswordSalt = dbUser.PasswordSalt;
|
|
user.APIKey = dbUser.APIKey;
|
|
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 === 1;
|
|
} |