Commit 98a22859 authored by Almouhannad's avatar Almouhannad

(B) Add verification for unique username on register

parent 49d2f2e6
using Application.Abstractions.CQRS.Commands;
using Domain.Entities.Identity.Users;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
......@@ -30,7 +31,15 @@ public class RegisterDoctorHandler : CommandHandlerBase<RegisterDoctorCommand, R
return Result.Failure<RegisterDoctorResponse>(doctorUserResult.Error);
#endregion
#region 2. Register (save to DB)
#region 2. Verify unique username
var uniqueUserNameResult = await _userRepository.IsUserNameAvailableAsunc(request.UserName);
if (uniqueUserNameResult.IsFailure)
return Result.Failure<RegisterDoctorResponse>(uniqueUserNameResult.Error);
if (uniqueUserNameResult.Value == false)
return Result.Failure<RegisterDoctorResponse>(IdentityErrors.TakenUserName);
#endregion
#region 3. Register (save to DB)
var registerResult = await _userRepository.RegisterDoctorAsync(doctorUserResult.Value);
if (registerResult.IsFailure)
return Result.Failure<RegisterDoctorResponse>(registerResult.Error);
......
using Application.Abstractions.CQRS.Commands;
using Application.Users.Commands.RegisterDoctor;
using Domain.Entities.Identity.Users;
using Domain.Errors;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
......@@ -31,7 +32,15 @@ public class RegisterReceptionistCommandHandler : CommandHandlerBase<RegisterRec
return Result.Failure<RegisterReceptionistResponse>(receptionistUserResult.Error);
#endregion
#region 2. Register (save to DB)
#region 2. Verify unique username
var uniqueUserNameResult = await _userRepository.IsUserNameAvailableAsunc(request.UserName);
if (uniqueUserNameResult.IsFailure)
return Result.Failure<RegisterReceptionistResponse>(uniqueUserNameResult.Error);
if (uniqueUserNameResult.Value == false)
return Result.Failure<RegisterReceptionistResponse>(IdentityErrors.TakenUserName);
#endregion
#region 3. Register (save to DB)
var registerResult = await _userRepository.RegisterReceptionistAsync(receptionistUserResult.Value);
if (registerResult.IsFailure)
return Result.Failure<RegisterReceptionistResponse>(registerResult.Error);
......
......@@ -7,5 +7,6 @@ public static class IdentityErrors
public static Error InvalidRole => new("Identity.InvalidRole", "Role specified for user is invalid");
public static Error NotFound => new("Identity.NotFound", "المستخدم غير مسجّل في النظام");
public static Error PasswordMismatch => new("Identity.PasswordMismatch", "كلمة السر غير صحيحة");
public static Error TakenUserName => new("Identity.TakenUserName", "يوجد مستخدم آخر له نفس اسم المستخدم");
public static Error UnableToRegister => new("Identity.UnableToRegister", "تعذر تسجيل المستخدم");
}
......@@ -5,7 +5,7 @@ namespace Domain.Errors;
public static class PersistenceErrors
{
public static Error UnableToCompleteTransaction =>
new("Persistence.UnableToCompleteTransaction", "حدثت مشكلة عند الاتصال مع قاعدة البيانات");
new("Persistence.UnableToCompleteTransaction", "لم يتم تنفيذ العملية، حدثت مشكلة عند الاتصال مع قاعدة البيانات");
public static Error UnableToCreate =>
new("Persistence.UnableToCreate", "فشلت عملية الإضافة");
......@@ -20,5 +20,5 @@ public static class PersistenceErrors
new("Persistence.NotFound", "الغرض المطلوب غير موجود");
public static Error Unknown =>
new("Persistence.Unknown", "حدث خطأ غير متوقع");
new("Persistence.Unknown", "حدثت مشكلة عند الاتصال مع قاعدة البيانات");
}
......@@ -13,6 +13,10 @@ public interface IUserRepository : IRepository<User>
public Task<Result<User?>> VerifyPasswordAsync(string userName, string password);
#endregion
#region Available username
public Task<Result<bool>> IsUserNameAvailableAsunc(string userName);
#endregion
#region Doctor users
#region Get doctor user by user name full
......
......@@ -61,6 +61,21 @@ public class UserRepository : Repositroy<User>, IUserRepository
}
#endregion
#region Available username
public async Task<Result<bool>> IsUserNameAvailableAsunc(string userName)
{
try
{
var query = _context.Set<User>().Where(user => user.UserName == userName);
return (await query.ToListAsync()).Count == 0;
}
catch (Exception)
{
return Result.Failure<bool>(PersistenceErrors.Unknown);
}
}
#endregion
#region Doctor users
#region Get doctor user by user name full
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment