t00-multiuser/server/repos/UserRepo.ts

65 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-04-21 15:35:47 +01:00
import Database from "../objects/Database";
2024-09-19 00:41:40 +01:00
import User from "../entities/User";
2024-04-22 02:01:14 +01:00
import RepoBase from "./RepoBase";
2024-04-21 15:35:47 +01:00
2024-04-22 02:01:14 +01:00
export default class UserRepo {
2024-04-21 15:35:47 +01:00
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;
}
}
2024-04-25 02:37:37 +01:00
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;
}
}
2024-04-21 15:35:47 +01:00
public static async insertUpdate(user:User) {
if (user.Id === Number.MIN_VALUE) {
2024-04-26 03:01:06 +01:00
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)
2024-04-21 15:35:47 +01:00
]);
} else {
2024-04-25 02:37:37 +01:00
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
2024-04-21 15:35:47 +01:00
]);
}
}
}
function populateUserFromDB(user:User, dbUser:any) {
user.Id = dbUser.Id;
user.Username = dbUser.Username;
user.PasswordHash = dbUser.PasswordHash;
user.PasswordSalt = dbUser.PasswordSalt;
2024-04-25 02:37:37 +01:00
user.APIKey = dbUser.APIKey;
2024-04-21 15:35:47 +01:00
user.CreatedByUserId = dbUser.CreatedByUserId;
user.CreatedDatetime = dbUser.CreatedDatetime;
user.LastModifiedByUserId = dbUser.LastModifiedByUserId;
2024-04-22 02:01:14 +01:00
user.LastModifiedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.LastModifiedDatetime);
2024-04-21 15:35:47 +01:00
user.DeletedByUserId = dbUser.DeletedByUserId;
2024-04-22 02:01:14 +01:00
user.DeletedDatetime = RepoBase.convertNullableDatetimeIntToDate(dbUser.DeletedDatetime);
2024-09-19 00:41:40 +01:00
user.IsDeleted = dbUser.IsDeleted === 1;
2024-04-21 15:35:47 +01:00
}