mirror of
https://github.com/azaion/admin.git
synced 2026-04-22 10:36:33 +00:00
structure app by rest api standards
add getusers tidy up BusinessException
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using Azaion.Common;
|
||||
using Azaion.Common.Database;
|
||||
using Azaion.Common.Entities;
|
||||
using Azaion.Common.Extensions;
|
||||
using Azaion.Common.Requests;
|
||||
using LinqToDB;
|
||||
|
||||
@@ -11,6 +12,7 @@ public interface IUserService
|
||||
Task RegisterUser(RegisterUserRequest request, CancellationToken cancellationToken = default);
|
||||
Task<User> ValidateUser(LoginRequest request, string? hardwareId = null, CancellationToken cancellationToken = default);
|
||||
Task UpdateHardwareId(string email, string hardwareId, CancellationToken cancellationToken = default);
|
||||
Task<IEnumerable<User>> GetUsers(string searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public class UserService(IDbFactory dbFactory) : IUserService
|
||||
@@ -21,7 +23,7 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
{
|
||||
var existingUser = await db.Users.FirstOrDefaultAsync(u => u.Email == request.Email, token: cancellationToken);
|
||||
if (existingUser != null)
|
||||
throw new BusinessException(ExceptionEnum.UserExists, "User already exists");
|
||||
throw new BusinessException(ExceptionEnum.EmailExists);
|
||||
|
||||
await db.InsertAsync(new User
|
||||
{
|
||||
@@ -38,21 +40,30 @@ public class UserService(IDbFactory dbFactory) : IUserService
|
||||
{
|
||||
var user = await db.Users.FirstOrDefaultAsync(x => x.Email == request.Email, token: cancellationToken);
|
||||
if (user == null)
|
||||
throw new BusinessException(ExceptionEnum.NoUserFound, "No user found");
|
||||
throw new BusinessException(ExceptionEnum.NoEmailFound);
|
||||
|
||||
if (request.Password.ToHash() != user.PasswordHash)
|
||||
throw new BusinessException(ExceptionEnum.PasswordIncorrect, "Passwords do not match");
|
||||
throw new BusinessException(ExceptionEnum.WrongPassword);
|
||||
|
||||
if (user.Role == RoleEnum.ApiAdmin)
|
||||
return user;
|
||||
|
||||
// For Non-API admins hardwareId should match if it was already set
|
||||
if (user.HardwareId != null && user.HardwareId != hardwareId)
|
||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch, "Hardware id mismatch");
|
||||
throw new BusinessException(ExceptionEnum.HardwareIdMismatch);
|
||||
return user;
|
||||
});
|
||||
|
||||
public async Task UpdateHardwareId(string email, string hardwareId, CancellationToken cancellationToken = default) =>
|
||||
await dbFactory.RunAdmin(async db =>
|
||||
await db.Users.UpdateAsync(x => x.Email == email, u => new User { HardwareId = hardwareId}, token: cancellationToken));
|
||||
|
||||
public async Task<IEnumerable<User>> GetUsers(string searchEmail, RoleEnum? searchRole, CancellationToken cancellationToken) =>
|
||||
await dbFactory.Run(async db =>
|
||||
await db.Users
|
||||
.WhereIf(!string.IsNullOrEmpty(searchEmail),
|
||||
u => u.Email.ToLower().Contains(searchEmail.ToLower()))
|
||||
.WhereIf(searchRole != null,
|
||||
u => u.Role == searchRole)
|
||||
.ToListAsync(token: cancellationToken));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user