105 lines
No EOL
4.2 KiB
TypeScript
105 lines
No EOL
4.2 KiB
TypeScript
import Database from "../objects/Database";
|
|
import User from "../entities/User";
|
|
|
|
export default abstract 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 SelectByUploadKey(uploadKey: string) {
|
|
const dbUser = await Database.Instance.query("SELECT * FROM User WHERE UploadKey = ? LIMIT 1", [uploadKey]);
|
|
if (dbUser == null || dbUser.length === 0) {
|
|
return null;
|
|
} else {
|
|
const user = new User();
|
|
PopulateUserFromDB(user, dbUser[0]);
|
|
return user;
|
|
}
|
|
}
|
|
|
|
public static async SelectByEmailAddress(emailAddress: string) {
|
|
const dbUser = await Database.Instance.query("SELECT * FROM User WHERE EmailAddress = ? LIMIT 1", [emailAddress]);
|
|
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) {
|
|
user.Id = (await Database.Instance.query("INSERT User (UserTypeId, Username, EmailAddress, PasswordHash, PasswordSalt, ApiKey, UploadKey, Verified, CreatedByUserId, CreatedDatetime, LastModifiedByUserId, LastModifiedDatetime, DeletedByUserId, DeletedDatetime, IsDeleted) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) RETURNING Id;", [
|
|
user.UserType, user.Username, user.EmailAddress, user.PasswordHash, user.PasswordSalt, user.ApiKey, user.UploadKey, Number(user.Verified), user.CreatedByUserId, user.CreatedDatetime, user.LastModifiedByUserId ?? null, user.LastModifiedDatetime ?? null, user.DeletedByUserId ?? null, user.DeletedDatetime ?? null, Number(user.IsDeleted)
|
|
]))[0]["Id"];
|
|
} else {
|
|
await Database.Instance.query(`UPDATE User SET UserTypeId = ?, Username = ?, EmailAddress = ?, PasswordHash = ?, PasswordSalt = ?, ApiKey = ?, UploadKey = ?, Verified = ?, CreatedByUserId = ?, CreatedDatetime = ?, LastModifiedByUserId = ?, LastModifiedDatetime = ?, DeletedByUserId = ?, DeletedDatetime = ?, IsDeleted = ? WHERE Id = ?`, [
|
|
user.UserType, user.Username, user.EmailAddress, user.PasswordHash, user.PasswordSalt, user.ApiKey, user.UploadKey, Number(user.Verified), user.CreatedByUserId, user.CreatedDatetime, user.LastModifiedByUserId ?? null, user.LastModifiedDatetime ?? null, user.DeletedByUserId ?? null, user.DeletedDatetime ?? null, Number(user.IsDeleted), user.Id
|
|
]);
|
|
}
|
|
|
|
return user;
|
|
}
|
|
}
|
|
|
|
function PopulateUserFromDB(user: User, dbUser: any) {
|
|
user.Id = dbUser.Id;
|
|
user.UserType = dbUser.UserTypeId;
|
|
user.Username = dbUser.Username;
|
|
user.EmailAddress = dbUser.EmailAddress;
|
|
user.PasswordHash = dbUser.PasswordHash;
|
|
user.PasswordSalt = dbUser.PasswordSalt;
|
|
user.ApiKey = dbUser.ApiKey;
|
|
user.UploadKey = dbUser.UploadKey;
|
|
user.Verified = dbUser.Verified[0] === 1;
|
|
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[0] === 1;
|
|
} |