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