Commit 123aa6d2 authored by Almouhannad's avatar Almouhannad

(B) Add get doctor user by id

parent e29d8df0
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Users.Queries.GetDoctorUserById;
public class GetDoctorUserByIdHandler : IQueryHandler<GetDoctorUserByIdQuery, GetDoctorUserByIdResponse>
{
#region CTOD DI
private readonly IUserRepository _userRepository;
public GetDoctorUserByIdHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}
#endregion
public async Task<Result<GetDoctorUserByIdResponse>> Handle(GetDoctorUserByIdQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch doctor user from DB
var doctorUserFromDbResult = await _userRepository.GetDoctorUserByIdAsync(request.Id);
if (doctorUserFromDbResult.IsFailure)
return Result.Failure<GetDoctorUserByIdResponse>(doctorUserFromDbResult.Error);
#endregion
#region 2. Generate response
var response = GetDoctorUserByIdResponse.GetResponse(doctorUserFromDbResult.Value);
if (response.IsFailure)
return Result.Failure<GetDoctorUserByIdResponse>(response.Error);
#endregion
return Result.Success<GetDoctorUserByIdResponse>(response.Value);
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Users.Queries.GetDoctorUserById;
public class GetDoctorUserByIdQuery : IQuery<GetDoctorUserByIdResponse>
{
public int Id { get; set; }
}
using Domain.Entities.Identity.Users;
using Domain.Errors;
using Domain.Shared;
namespace Application.Users.Queries.GetDoctorUserById;
public class GetDoctorUserByIdResponse
{
public int Id { get; set; }
public string UserName { get; set; } = null!;
public string FirstName { get; set; } = null!;
public string MiddleName { get; set; } = null!;
public string LastName { get; set; } = null!;
public static Result<GetDoctorUserByIdResponse> GetResponse (DoctorUser doctorUser)
{
if (doctorUser.Id <= 0 || doctorUser.User is null || doctorUser.Doctor is null)
return Result.Failure<GetDoctorUserByIdResponse>(PersistenceErrors.NotFound);
if (doctorUser.Doctor.PersonalInfo is null)
return Result.Failure<GetDoctorUserByIdResponse>(PersistenceErrors.NotFound);
return new GetDoctorUserByIdResponse
{
Id = doctorUser.Id,
UserName = doctorUser.User.UserName,
FirstName = doctorUser.Doctor.PersonalInfo.FirstName,
MiddleName = doctorUser.Doctor.PersonalInfo.MiddleName,
LastName = doctorUser.Doctor.PersonalInfo.LastName,
};
}
}
...@@ -28,6 +28,10 @@ public interface IUserRepository : IRepository<User> ...@@ -28,6 +28,10 @@ public interface IUserRepository : IRepository<User>
public Task<Result<ICollection<DoctorUser>>> GetAllDoctorUsersAsync(); public Task<Result<ICollection<DoctorUser>>> GetAllDoctorUsersAsync();
#endregion #endregion
#region Get doctor user by id
public Task<Result<DoctorUser>> GetDoctorUserByIdAsync(int id);
#endregion
#region Register doctor #region Register doctor
public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser); public Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser);
#endregion #endregion
......
...@@ -116,6 +116,26 @@ public class UserRepository : Repositroy<User>, IUserRepository ...@@ -116,6 +116,26 @@ public class UserRepository : Repositroy<User>, IUserRepository
} }
#endregion #endregion
#region Get doctor user by id
public async Task<Result<DoctorUser>> GetDoctorUserByIdAsync(int id)
{
try
{
var query = _context.Set<DoctorUser>()
.Where(doctorUser => doctorUser.Id == id)
.Include(doctorUser => doctorUser.User)
.Include(doctorUser => doctorUser.Doctor)
.ThenInclude(doctor => doctor.PersonalInfo);
return await query.FirstAsync();
}
catch (Exception)
{
return Result.Failure<DoctorUser>(PersistenceErrors.NotFound);
}
}
#endregion
#region Register doctor #region Register doctor
public async Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser) public async Task<Result<DoctorUser>> RegisterDoctorAsync(DoctorUser doctorUser)
{ {
......
...@@ -3,6 +3,7 @@ using Application.Users.Commands.RegisterDoctor; ...@@ -3,6 +3,7 @@ using Application.Users.Commands.RegisterDoctor;
using Application.Users.Commands.RegisterReceptionist; using Application.Users.Commands.RegisterReceptionist;
using Application.Users.Queries.GetAllDoctorUsers; using Application.Users.Queries.GetAllDoctorUsers;
using Application.Users.Queries.GetAllReceptionistsUsers; using Application.Users.Queries.GetAllReceptionistsUsers;
using Application.Users.Queries.GetDoctorUserById;
using Domain.Entities.Identity.UserRoles; using Domain.Entities.Identity.UserRoles;
using MediatR; using MediatR;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
...@@ -58,6 +59,17 @@ public class UsersController : ApiController ...@@ -58,6 +59,17 @@ public class UsersController : ApiController
return HandleFailure(result); return HandleFailure(result);
return Ok(result.Value); return Ok(result.Value);
} }
[Authorize(Roles = Roles.AdminName)]
[HttpGet("Doctors/{id:int}")]
public async Task<IActionResult> GetDoctorUserById([FromRoute(Name ="id")] int id )
{
var query = new GetDoctorUserByIdQuery { Id = id };
var result = await _sender.Send(query);
if (result.IsFailure)
return HandleFailure(result);
return Ok(result.Value);
}
#endregion #endregion
#region Receptionist #region Receptionist
......
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