t00-multiuser/server/repos/UserRepo.ts

88 lines
3.7 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-09-26 00:47:08 +01:00
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;
}
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-09-26 00:47:08 +01:00
public static async selectUserCount() {
2024-09-28 14:31:02 +01:00
const countResult = await Database.Instance.query("SELECT COUNT(Id) FROM `User` WHERE IsDeleted = 0 LIMIT 1");
2024-09-26 00:47:08 +01:00
return countResult[0]["COUNT(Id)"];
}
2024-04-21 15:35:47 +01:00
public static async insertUpdate(user:User) {
if (user.Id === Number.MIN_VALUE) {
user.Id = (await Database.Instance.query("INSERT User (UserLevelId, Username, PasswordHash, PasswordSalt, HasUsedClient, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
2024-09-26 00:47:08 +01:00
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)
2024-09-28 14:31:02 +01:00
]))[0]["Id"];
2024-04-21 15:35:47 +01:00
} else {
await Database.Instance.query(`UPDATE User SET UserLevelId = ?, Username = ?, PasswordHash = ?, PasswordSalt = ?, APIKey = ?, HasUsedClient = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
user.UserLevel, user.Username, user.PasswordHash, user.PasswordSalt, user.APIKey, Number(user.HasUsedClient), 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
]);
}
2024-09-28 14:31:02 +01:00
return user;
2024-04-21 15:35:47 +01:00
}
}
function populateUserFromDB(user:User, dbUser:any) {
user.Id = dbUser.Id;
2024-09-26 00:47:08 +01:00
user.UserLevel = dbUser.UserLevelId;
2024-04-21 15:35:47 +01:00
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-10-08 11:04:46 +01:00
user.HasUsedClient = dbUser.HasUsedClient[0] === 1;
2024-04-21 15:35:47 +01:00
user.CreatedByUserId = dbUser.CreatedByUserId;
2024-09-23 23:55:00 +01:00
user.CreatedDatetime = new Date(dbUser.CreatedDatetime);
2024-04-21 15:35:47 +01:00
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-10-08 11:04:46 +01:00
user.IsDeleted = dbUser.IsDeleted[0] === 1;
2024-04-21 15:35:47 +01:00
}