Commit 10de6317 authored by Almouhannad's avatar Almouhannad

(B) Add get phones, add phone by user id for doctor

parent 5c9ea6fe
using Application.Abstractions.CQRS.Commands;
namespace Application.Doctors.Commands.AddPhoneNumberByUserId;
public class AddPhoneNumberByUserIdCommand : ICommand
{
public int DoctorUserId { get; set; }
public string Name { get; set; } = null!;
public string Phone { get; set; } = null!;
}
using Application.Abstractions.CQRS.Commands;
using Domain.Repositories;
using Domain.Shared;
using Domain.UnitOfWork;
namespace Application.Doctors.Commands.AddPhoneNumberByUserId;
public class AddPhoneNumberByUserIdHandler : CommandHandlerBase<AddPhoneNumberByUserIdCommand>
{
#region CTOR DI
private readonly IUserRepository _userRepository;
private readonly IDoctorsRepository _doctorsRepository;
public AddPhoneNumberByUserIdHandler(IUnitOfWork unitOfWork, IUserRepository userRepository, IDoctorsRepository doctorsRepository) : base(unitOfWork)
{
_userRepository = userRepository;
_doctorsRepository = doctorsRepository;
}
#endregion
public override async Task<Result> HandleHelper(AddPhoneNumberByUserIdCommand request, CancellationToken cancellationToken)
{
#region 1. Fetch Doctor from persistence
var doctorFromPersistenceResult = await _userRepository
.GetDoctorByDoctorUserIdAsync(request.DoctorUserId);
if (doctorFromPersistenceResult.IsFailure)
return Result.Failure(doctorFromPersistenceResult.Error);
var doctor = doctorFromPersistenceResult.Value;
#endregion
#region 2. Add phone number
var addPhoneNumberResult = doctor.AddPhone(request.Phone, request.Name);
if (addPhoneNumberResult.IsFailure)
return Result.Failure(addPhoneNumberResult.Error);
#endregion
#region 3. save updates
var saveUpdatesResult = await _doctorsRepository.UpdateAsync(doctor);
if (saveUpdatesResult.IsFailure)
return Result.Failure(saveUpdatesResult.Error);
#endregion
return Result.Success();
}
}
using Application.Abstractions.CQRS.Queries;
namespace Application.Doctors.Queries.GetPhonesByUserId;
public class GetDoctorPhonesByUserIdQuery : IQuery<GetDoctorPhonesByUserIdResponse>
{
public int DoctorUserId { get; set; }
}
using Application.Abstractions.CQRS.Queries;
using Domain.Repositories;
using Domain.Shared;
namespace Application.Doctors.Queries.GetPhonesByUserId;
public class GetDoctorPhonesByUserIdQueryHandler : IQueryHandler<GetDoctorPhonesByUserIdQuery, GetDoctorPhonesByUserIdResponse>
{
#region CTOR DI
private readonly IUserRepository _userRepository;
public GetDoctorPhonesByUserIdQueryHandler(IUserRepository userRepository)
{
_userRepository = userRepository;
}
#endregion
public async Task<Result<GetDoctorPhonesByUserIdResponse>> Handle(GetDoctorPhonesByUserIdQuery request, CancellationToken cancellationToken)
{
#region 1. Fetch doctor from persistence
var doctorFromPersistenceResult = await _userRepository
.GetDoctorByDoctorUserIdAsync(request.DoctorUserId);
if (doctorFromPersistenceResult.IsFailure)
return Result.Failure<GetDoctorPhonesByUserIdResponse>(doctorFromPersistenceResult.Error);
var doctor = doctorFromPersistenceResult.Value;
#endregion
// Generate response
return Result.Success<GetDoctorPhonesByUserIdResponse>(
GetDoctorPhonesByUserIdResponse.GetResponse(doctor));
}
}
using Domain.Entities.People.Doctors;
namespace Application.Doctors.Queries.GetPhonesByUserId;
public class GetDoctorPhonesByUserIdResponse
{
public class GetDoctorPhonesByUserIdResponseItem
{
public string Name { get; set; } = null!;
public string Phone { get; set; } = null!;
}
public ICollection<GetDoctorPhonesByUserIdResponseItem> Phones { get; set; } = null!;
public static GetDoctorPhonesByUserIdResponse GetResponse(Doctor doctor)
{
List<GetDoctorPhonesByUserIdResponseItem> response = new();
foreach (var doctorPhone in doctor.Phones)
{
response.Add(new GetDoctorPhonesByUserIdResponseItem
{
Name = doctorPhone.Name!,
Phone = doctorPhone.Phone,
});
}
return new GetDoctorPhonesByUserIdResponse
{
Phones = response
};
}
}
...@@ -8,7 +8,6 @@ using Microsoft.EntityFrameworkCore; ...@@ -8,7 +8,6 @@ using Microsoft.EntityFrameworkCore;
using Persistence.Context; using Persistence.Context;
using Persistence.Repositories.Base; using Persistence.Repositories.Base;
using Persistence.Repositories.Doctors.Specifications; using Persistence.Repositories.Doctors.Specifications;
using System.Collections.Generic;
namespace Persistence.Repositories.Doctors; namespace Persistence.Repositories.Doctors;
...@@ -38,7 +37,8 @@ public class DoctorsRepository : Repositroy<Doctor>, IDoctorsRepository ...@@ -38,7 +37,8 @@ public class DoctorsRepository : Repositroy<Doctor>, IDoctorsRepository
#region Update method #region Update method
public override Task<Result> UpdateAsync(Doctor entity) public override Task<Result> UpdateAsync(Doctor entity)
{ {
_context.Entry(entity.Status).State = EntityState.Unchanged; if (entity.Status is not null)
_context.Entry(entity.Status).State = EntityState.Unchanged;
return base.UpdateAsync(entity); return base.UpdateAsync(entity);
} }
#endregion #endregion
......
...@@ -216,14 +216,15 @@ public class UserRepository : Repositroy<User>, IUserRepository ...@@ -216,14 +216,15 @@ public class UserRepository : Repositroy<User>, IUserRepository
} }
#endregion #endregion
#region Get Doctor by doctor user #region Get Doctor by doctor user id
public async Task<Result<Doctor>> GetDoctorByDoctorUserIdAsync(int doctorUserId) public async Task<Result<Doctor>> GetDoctorByDoctorUserIdAsync(int doctorUserId)
{ {
try try
{ {
var query = _context.Set<DoctorUser>() var query = _context.Set<DoctorUser>()
.Where(doctorUser => doctorUser.Id == doctorUserId) .Where(doctorUser => doctorUser.Id == doctorUserId)
.Include(doctorUser => doctorUser.Doctor); .Include(doctorUser => doctorUser.Doctor)
.ThenInclude(doctor => doctor.Phones);
var result = await query.FirstAsync(); var result = await query.FirstAsync();
return result.Doctor; return result.Doctor;
} }
......
using Application.Doctors.Commands.ChangeStatusByUserId; using Application.Doctors.Commands.AddPhoneNumberByUserId;
using Application.Doctors.Commands.ChangeStatusByUserId;
using Application.Doctors.Queries.GetAllDoctors; using Application.Doctors.Queries.GetAllDoctors;
using Application.Doctors.Queries.GetPhonesByUserId;
using Application.Doctors.Queries.GetStatusByUserId; using Application.Doctors.Queries.GetStatusByUserId;
using Application.Employees.Queries.GetAvailable; using Application.Employees.Queries.GetAvailable;
using Domain.Entities.Identity.UserRoles; using Domain.Entities.Identity.UserRoles;
...@@ -62,4 +64,25 @@ public class DoctorsController : ApiController ...@@ -62,4 +64,25 @@ public class DoctorsController : ApiController
return HandleFailure(result); return HandleFailure(result);
return Ok(); return Ok();
} }
//[Authorize(Roles = Roles.DoctorName)]
[HttpGet("Phones/{id:int}")]
public async Task<IActionResult> GetPhonesByUserId([FromRoute(Name = "id")] int doctorUserId)
{
var query = new GetDoctorPhonesByUserIdQuery { DoctorUserId = doctorUserId };
var result = await _sender.Send(query);
if (result.IsFailure)
return HandleFailure(result);
return Ok(result.Value);
}
//[Authorize(Roles = Roles.DoctorName)]
[HttpPost("Phones")]
public async Task<IActionResult> AddPhoneByUserId([FromBody] AddPhoneNumberByUserIdCommand command)
{
var result = await _sender.Send(command);
if (result.IsFailure)
return HandleFailure(result);
return Ok();
}
} }
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