Commit c43d428a authored by Almouhannad's avatar Almouhannad

(B) Add get doctors, receps users queries

parent f2ce0f95
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Users.Queries.GetAllDoctorUsers;
public class GetAllDoctorUsersHandler : IQueryHandler<GetAllDoctorUsersQuery, GetAllDoctorUsersResponse>
{
#region Ctor DI
private readonly IUserRepository _userRepository;
public GetAllDoctorUsersHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}
#endregion
public async Task<Result<GetAllDoctorUsersResponse>> Handle(GetAllDoctorUsersQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch users from DB
var doctorUsersFromDbResult = await _userRepository.GetAllDoctorUsersAsync();
if (doctorUsersFromDbResult.IsFailure)
return Result.Failure<GetAllDoctorUsersResponse>(doctorUsersFromDbResult.Error);
#endregion
#region 2. Generate response
var response = GetAllDoctorUsersResponse.GetResponse(doctorUsersFromDbResult.Value);
if (response.IsFailure)
return Result.Failure<GetAllDoctorUsersResponse>(response.Error);
#endregion
return Result.Success(response.Value);
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Users.Queries.GetAllDoctorUsers;
public class GetAllDoctorUsersQuery : IQuery<GetAllDoctorUsersResponse>
{
}
using Domain.Entities.Identity.Users;
using Domain.Errors;
using Domain.Shared;
namespace Application.Users.Queries.GetAllDoctorUsers;
public class GetAllDoctorUsersResponse
{
public class GetAllDoctorUsersResponseItem
{
public string UserName { get; set; } = null!;
public string FullName { get; set; } = null!;
}
public ICollection<GetAllDoctorUsersResponseItem> DoctorUsers { get; set; } = [];
public static Result<GetAllDoctorUsersResponse> GetResponse(ICollection<DoctorUser> doctorUsers)
{
List<GetAllDoctorUsersResponseItem> result = new();
foreach (var doctorUser in doctorUsers)
{
if (doctorUser.User is null || doctorUser.Doctor.PersonalInfo is null)
return Result.Failure<GetAllDoctorUsersResponse>(PersistenceErrors.NotFound);
var doctorUserItem = new GetAllDoctorUsersResponseItem
{
FullName = doctorUser.Doctor.PersonalInfo.FullName,
UserName = doctorUser.User.UserName
};
result.Add(doctorUserItem);
}
return new GetAllDoctorUsersResponse
{
DoctorUsers = result
};
}
}
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Users.Queries.GetAllReceptionistsUsers;
public class GetAllReceptionistUsersHandler : IQueryHandler<GetAllReceptionistUsersQuery, GetAllReceptionistUsersResponse>
{
#region CTOR DI
private readonly IUserRepository _userRepository;
public GetAllReceptionistUsersHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}
#endregion
public async Task<Result<GetAllReceptionistUsersResponse>> Handle(GetAllReceptionistUsersQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch users from DB
var receptionistUsersFromDbResult = await _userRepository.GetAllReceptionistUsersAsync();
if (receptionistUsersFromDbResult.IsFailure)
return Result.Failure<GetAllReceptionistUsersResponse>(receptionistUsersFromDbResult.Error);
#endregion
#region 2. Generate response
var response = GetAllReceptionistUsersResponse.GetResponse(receptionistUsersFromDbResult.Value);
if (response.IsFailure)
return Result.Failure<GetAllReceptionistUsersResponse>(response.Error);
#endregion
return Result.Success<GetAllReceptionistUsersResponse>(response.Value);
// Or just return response.Value
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Users.Queries.GetAllReceptionistsUsers;
public class GetAllReceptionistUsersQuery : IQuery<GetAllReceptionistUsersResponse>
{
}
using Domain.Entities.Identity.Users;
using Domain.Errors;
using Domain.Shared;
namespace Application.Users.Queries.GetAllReceptionistsUsers;
public class GetAllReceptionistUsersResponse
{
public class GetAllReceptionistUsersResponseItem
{
public string UserName { get; set; } = null!;
public string FullName { get; set; } = null!;
}
public ICollection<GetAllReceptionistUsersResponseItem> DoctorUsers { get; set; } = [];
public static Result<GetAllReceptionistUsersResponse> GetResponse(ICollection<ReceptionistUser> receptionistUsers)
{
List<GetAllReceptionistUsersResponseItem> result = new();
foreach (var receptionistUser in receptionistUsers)
{
if (receptionistUser.User is null || receptionistUser.PersonalInfo is null)
return Result.Failure<GetAllReceptionistUsersResponse>(PersistenceErrors.NotFound);
var receptionistUserItem = new GetAllReceptionistUsersResponseItem
{
UserName = receptionistUser.User.UserName,
FullName = receptionistUser.PersonalInfo.FullName
};
result.Add(receptionistUserItem);
}
return new GetAllReceptionistUsersResponse
{
DoctorUsers = result
};
}
}
......@@ -18,4 +18,7 @@ public static class PersistenceErrors
public static Error NotFound =>
new("Persistence.NotFound", "الغرض المطلوب غير موجود");
public static Error Unknown =>
new("Persistence.Unknown", "حدث خطأ غير متوقع");
}
......@@ -13,18 +13,32 @@ public interface IUserRepository : IRepository<User>
public Task<Result<User?>> VerifyPasswordAsync(string userName, string password);
#endregion
#region Doctor users
#region Get doctor user by user name full
public Task<Result<DoctorUser>> GetDoctorUserByUserNameFullAsync(string userName);
#endregion
#region Get all doctors
public Task<Result<ICollection<DoctorUser>>> GetAllDoctorUsersAsync();
#endregion
#region Register doctor
public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser);
#endregion
#endregion
#region Receptionists users
#region Get receptionist user by user name full
public Task<Result<ReceptionistUser>> GetReceptionistUserByUserNameFullAsync(string userName);
#endregion
#region Register doctor
public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser);
#region Get all Receptionist Users
public Task<Result<ICollection<ReceptionistUser>>> GetAllReceptionistUsersAsync();
#endregion
#region Register receptionist
......@@ -32,4 +46,7 @@ public interface IUserRepository : IRepository<User>
#endregion
#endregion
}
......@@ -61,6 +61,8 @@ public class UserRepository : Repositroy<User>, IUserRepository
}
#endregion
#region Doctor users
#region Get doctor user by user name full
public async Task<Result<DoctorUser>> GetDoctorUserByUserNameFullAsync(string username)
{
......@@ -81,6 +83,50 @@ public class UserRepository : Repositroy<User>, IUserRepository
}
#endregion
#region Get all doctors
public async Task<Result<ICollection<DoctorUser>>> GetAllDoctorUsersAsync()
{
try
{
var query = _context.Set<DoctorUser>()
.Include(doctroUser => doctroUser.User)
.Include(doctorUser => doctorUser.Doctor)
.ThenInclude(doctor => doctor.PersonalInfo);
return await query.ToListAsync();
}
catch (Exception)
{
return Result.Failure<ICollection<DoctorUser>>(PersistenceErrors.Unknown);
}
}
#endregion
#region Register doctor
public async Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser)
{
_context.Entry(doctorUser.User.Role).State = EntityState.Unchanged;
_context.Entry(doctorUser.Doctor.Status).State = EntityState.Unchanged;
var passwordResult = doctorUser.User.SetHashedPassword(_passwordHasher.Hash(doctorUser.User.HashedPassword));
if (passwordResult.IsFailure)
return Result.Failure<DoctorUser>(passwordResult.Error);
try
{
var createdDoctorUser = await _context.Set<DoctorUser>().AddAsync(doctorUser);
await _context.SaveChangesAsync();
return createdDoctorUser.Entity;
}
catch (Exception)
{
return Result.Failure<DoctorUser>(IdentityErrors.UnableToRegister);
}
}
#endregion
#endregion
#region Receptionist users
#region Get receptionist user by user name full
public async Task<Result<ReceptionistUser>> GetReceptionistUserByUserNameFullAsync(string username)
{
......@@ -100,24 +146,20 @@ public class UserRepository : Repositroy<User>, IUserRepository
}
#endregion
#region Register doctor
public async Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser)
#region GetAll
public async Task<Result<ICollection<ReceptionistUser>>> GetAllReceptionistUsersAsync()
{
_context.Entry(doctorUser.User.Role).State = EntityState.Unchanged;
_context.Entry(doctorUser.Doctor.Status).State = EntityState.Unchanged;
var passwordResult = doctorUser.User.SetHashedPassword(_passwordHasher.Hash(doctorUser.User.HashedPassword));
if (passwordResult.IsFailure)
return Result.Failure<DoctorUser>(passwordResult.Error);
// Tip: you can apply specification pattern here
try
{
var createdDoctorUser = await _context.Set<DoctorUser>().AddAsync(doctorUser);
await _context.SaveChangesAsync();
return createdDoctorUser.Entity;
var query = _context.Set<ReceptionistUser>()
.Include(receptionistUser => receptionistUser.User)
.Include(receptionistUser => receptionistUser.PersonalInfo);
return await query.ToListAsync();
}
catch (Exception)
{
return Result.Failure<DoctorUser>(IdentityErrors.UnableToRegister);
return Result.Failure<ICollection<ReceptionistUser>>(PersistenceErrors.Unknown);
}
}
#endregion
......@@ -144,4 +186,7 @@ public class UserRepository : Repositroy<User>, IUserRepository
}
#endregion
#endregion
}
using Application.Users.Commands.Login;
using Application.Users.Commands.RegisterDoctor;
using Application.Users.Commands.RegisterReceptionist;
using Application.Users.Queries.GetAllDoctorUsers;
using Application.Users.Queries.GetAllReceptionistsUsers;
using Domain.Entities.Identity.UserRoles;
using MediatR;
using Microsoft.AspNetCore.Authorization;
......@@ -45,11 +47,22 @@ public class UsersController : ApiController
return Ok(result.Value);
}
[Authorize(Roles = Roles.AdminName)]
[HttpGet("Doctors")]
public async Task<IActionResult> GetAllDoctorUsers()
{
var query = new GetAllDoctorUsersQuery();
var result = await _sender.Send(query);
if (result.IsFailure)
return HandleFailure(result);
return Ok(result.Value);
}
#endregion
#region Receptionist
[Authorize(Roles = Roles.AdminName)]
[HttpPost("Receptionist")]
[HttpPost("Receptionists")]
public async Task<IActionResult> RegisterReceptionist([FromBody] RegisterReceptionistCommand command)
{
var result = await _sender.Send(command);
......@@ -59,6 +72,16 @@ public class UsersController : ApiController
return Ok(result.Value);
}
[Authorize(Roles = Roles.AdminName)]
[HttpGet("Receptionists")]
public async Task<IActionResult> GetAllReceptionistUsers()
{
var query = new GetAllReceptionistUsersQuery();
var result = await _sender.Send(query);
if (result.IsFailure)
return HandleFailure(result);
return Ok(result.Value);
}
#endregion
......
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