Skip to content

Commit

Permalink
Refactor controllers and models for new dependencies
Browse files Browse the repository at this point in the history
* Added `IPractitionerService` to `PatientsController` and updated methods to use it.
* Added `IHospitalService` to `PractitionersController` and updated methods to use it.
* Updated `SearchPatientsAsync` and `SearchPractitionerAsync` to include practitioner's and hospital's names.
* Renamed properties in `PatientDto` and `PractitionerDto` to `AssignedPractitionerId` and `AssignedHospitalId`, and added new properties for names.
* Updated `PatientRepository`, `PractitionerRepository`, `Patient`, and `Practitioner` models to use new property names.
* Added new methods to `IHospitalService` and `IPractitionerService` interfaces and implemented them.
* Updated enums `PractitionerTitle` and `PractitionerSpecialty`.
* Added mapping in `PatientProfile` to ignore `AssignedPractitioner`.
  • Loading branch information
theEMA-dev committed Sep 5, 2024
1 parent eac7f82 commit 85dd03a
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 50 deletions.
17 changes: 15 additions & 2 deletions Carefusion.API/Controllers/PatientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ namespace Carefusion.Web.Controllers;
public class PatientsController : ControllerBase
{
private readonly IPatientService _patientService;
private readonly IPractitionerService _practitionerService;

/// <inheritdoc />
public PatientsController(IPatientService patientService)
public PatientsController(IPatientService patientService, IPractitionerService practitionerService)
{
_patientService = patientService;
_practitionerService = practitionerService;
}

/// <summary>
Expand All @@ -32,6 +34,8 @@ public async Task<IActionResult> GetPatient(int id)
try
{
var patient = await _patientService.GetPatientByIdAsync(id);
var assignedPractitioner = await _practitionerService.GetPractitionerNameById(patient.AssignedPractitionerId) ?? null;
patient.PractitionerName = assignedPractitioner;
return Ok(patient);
}
catch (InvalidOperationException)
Expand All @@ -57,6 +61,8 @@ public async Task<IActionResult> GetPatientByGovId(string govId)
try
{
var patient = await _patientService.GetPatientByGovId(govId);
var assignedPractitioner = await _practitionerService.GetPractitionerNameById(patient.AssignedPractitionerId) ?? null;
patient.PractitionerName = assignedPractitioner;
return Ok(patient);
}
catch (InvalidOperationException)
Expand Down Expand Up @@ -103,7 +109,14 @@ public async Task<IActionResult> SearchPatients(
var (patients, totalCount) = await _patientService.SearchPatientsAsync(q ?? " ", sort, gender, bloodType, birthStartDate, birthEndDate, pageNumber, pageSize, showDeceased, showInactive);
if (totalCount == 0) return NotFound();

return Ok(new { Patients = patients, TotalCount = totalCount });
var patientDtos = patients.ToList();
foreach (var patient in patientDtos)
{
var assignedPractitioner = await _practitionerService.GetPractitionerNameById(patient.AssignedPractitionerId) ?? null;
patient.PractitionerName = assignedPractitioner;
}

return Ok(new { Patients = patientDtos, TotalCount = totalCount });
}
catch (Exception)
{
Expand Down
28 changes: 20 additions & 8 deletions Carefusion.API/Controllers/PractitionersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Carefusion.Business.Interfaces;
using Carefusion.Core;
using Carefusion.Core.Utilities;

#pragma warning disable CS0168 // Variable is declared but never used

namespace Carefusion.Web.Controllers;
Expand All @@ -13,11 +12,13 @@ namespace Carefusion.Web.Controllers;
public class PractitionersController : ControllerBase
{
private readonly IPractitionerService _practitionerService;
private readonly IHospitalService _hospitalService;

/// <inheritdoc />
public PractitionersController(IPractitionerService practitionerService)
public PractitionersController(IPractitionerService practitionerService, IHospitalService hospitalService)
{
_practitionerService = practitionerService;
_hospitalService = hospitalService;
}

/// <summary>
Expand All @@ -32,8 +33,10 @@ public async Task<IActionResult> GetPractitioner(int id)
{
try
{
var patient = await _practitionerService.GetPractitionerByIdAsync(id);
return Ok(patient);
var practitioner = await _practitionerService.GetPractitionerByIdAsync(id);
var assignedHospital = await _hospitalService.GetHospitalNameById(practitioner.AssignedHospitalId) ?? null;
practitioner.HospitalName = assignedHospital;
return Ok(practitioner);
}
catch (InvalidOperationException)
{
Expand All @@ -57,8 +60,10 @@ public async Task<IActionResult> GetPractitionerByGovId(string govId)
{
try
{
var patient = await _practitionerService.GetPractitionerByGovId(govId);
return Ok(patient);
var practitioner = await _practitionerService.GetPractitionerByGovId(govId);
var assignedHospital = await _hospitalService.GetHospitalNameById(practitioner.AssignedHospitalId) ?? null;
practitioner.HospitalName = assignedHospital;
return Ok(practitioner);
}
catch (InvalidOperationException)
{
Expand Down Expand Up @@ -101,10 +106,17 @@ public async Task<IActionResult> SearchPatients(
{
try
{
var (patients, totalCount) = await _practitionerService.SearchPractitionerAsync(q ?? " ", sort, gender, title, specialty, birthStartDate, birthEndDate, pageNumber, pageSize, showInactive);
var (practitioners, totalCount) = await _practitionerService.SearchPractitionerAsync(q ?? " ", sort, gender, title, specialty, birthStartDate, birthEndDate, pageNumber, pageSize, showInactive);
if (totalCount == 0) return NotFound();

return Ok(new { Practitioners = patients, TotalCount = totalCount });
var practitionerDtos = practitioners.ToList();
foreach (var practitioner in practitionerDtos)
{
var assignedHospital = await _hospitalService.GetHospitalNameById(practitioner.AssignedHospitalId) ?? null;
practitioner.HospitalName = assignedHospital;
}

return Ok(new { Practitioners = practitionerDtos, TotalCount = totalCount });
}
catch (Exception)
{
Expand Down
4 changes: 4 additions & 0 deletions Carefusion.API/Mappings/PatientProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public PatientProfile()
.ReverseMap()
.ForMember(dest => dest.Identifier, opt => opt.Ignore())
.ForMember(dest => dest.RecordUpdated, opt => opt.Ignore());

CreateMap<PatientDto, PatientDto>()
.ForMember(dest => dest.AssignedPractitioner, opt => opt.Ignore());

CreateMap<Communication, CommunicationDto>().ReverseMap();
}
}
1 change: 1 addition & 0 deletions Carefusion.Business/Interfaces/IHospitalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface IHospitalService
Task AddHospitalAsync(HospitalDto hospitalDto);
Task UpdateHospitalAsync(int id, HospitalDto hospitalDto);
Task<bool> DeleteHospitalAsync(int id);
Task<string?> GetHospitalNameById(int? id);
}
1 change: 1 addition & 0 deletions Carefusion.Business/Interfaces/IPractitionerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public interface IPractitionerService
Task AddPractitionerAsync(PractitionerDto practitionerDto);
Task UpdatePractitionerAsync(int id, PractitionerDto practitionerDto);
Task<bool> DeletePractitionerAsync(int id);
Task<string?> GetPractitionerNameById(int? id);
}
12 changes: 12 additions & 0 deletions Carefusion.Business/Services/HospitalService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,16 @@ public async Task<bool> DeleteHospitalAsync(int id)
await _hospitalRepository.DeleteAsync(hospital);
return true;
}

public async Task<string?> GetHospitalNameById(int? id)
{
if (id == null)
{
return null;
}

var hospital = await _hospitalRepository.GetByIdAsync(id.Value);
Console.WriteLine(hospital.Name);
return hospital.Name;
}
}
11 changes: 11 additions & 0 deletions Carefusion.Business/Services/PractitionerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,15 @@ public async Task<bool> DeletePractitionerAsync(int id)
await _practitionerRepository.DeleteAsync(practitioner);
return true;
}

public async Task<string?> GetPractitionerNameById(int? id)
{
if (id == null)
{
return null;
}

var practitioner = await _practitionerRepository.GetByIdAsync(id.Value);
return practitioner.Title + " " + practitioner.Name;
}
}
48 changes: 23 additions & 25 deletions Carefusion.Core/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,32 +66,30 @@ public enum PractitionerTitle
deputyChiefPhysician,
[Description("Sr. Asst.")]
seniorAssistant,
[Description("MD")]
medicalDr
[Description("Dr.")]
dr
}

public enum PractitionerSpecialty
{
neurologist,
specialist,
radiologist,
cardiologist,
surgeon,
internist,
pathologist,
endocrinologist,
psychiatrist,
oncologist,
dermatologist,
orthopedic,
pulmonologist,
nephrologist,
hematologist,
obstetrician,
gynecologist,
urologist,
anesthesiologist,
rheumatologist,
allergist,
geriatrician
neurology,
specialty,
radiology,
cardiology,
surgery,
pathology,
endocrinology,
psychiatry,
oncology,
dermatology,
orthopedics,
pulmonology,
nephrology,
hematology,
obstetrics,
gynecology,
urology,
anesthesiology,
rheumatology,
allergy,
geriatrics
}
6 changes: 5 additions & 1 deletion Carefusion.Core/PatientDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public class PatientDto
[StringLength(1024)]
public string? Picture { get; init; }
[DefaultValue(null)]
public int? AssignedPractitioner { get; init; }
public int? AssignedPractitionerId { get; init; }
[DefaultValue(null)]
[JsonIgnore]
public string? PractitionerName { get; set; }
public string? AssignedPractitioner => PractitionerName;
[DefaultValue(null)]
[StringLength(50)]
public string? HealthcareProvider { get; init; }
Expand Down
16 changes: 10 additions & 6 deletions Carefusion.Core/PractitionerDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public class PractitionerDto
[Required]
[StringLength(25)]
public required string Gender { get; init; }
[Required]
[StringLength(25)]
[DefaultValue(null)]
public string? Specialty { get; init; }
public required string Specialty { get; init; }
[Required]
[StringLength(25)]
[DefaultValue(null)]
public string? Title { get; init; }
public required string Title { get; init; }
[StringLength(50)]
[DefaultValue(null)]
public string? Role { get; init; }
Expand All @@ -33,9 +33,13 @@ public class PractitionerDto
[DefaultValue(null)]
public string? Picture { get; init; }
[DefaultValue(null)]
public int? AssignedHospital { get; init; }
public int? AssignedHospitalId { get; init; }
[DefaultValue(null)]
[JsonIgnore]
public string? HospitalName { get; set; }
public string? AssignedHospital => HospitalName;
[DefaultValue(null)]
public int? AssignedDepartment { get; init; }
public int? AssignedDepartmentId { get; init; }
[Required]
[DefaultValue(true)]
public required bool Active { get; init; }
Expand Down
2 changes: 1 addition & 1 deletion Carefusion.Data/Repositories/PatientRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public IQueryable<Patient> GetQuery()
BloodType = p.BloodType,
GovernmentId = p.GovernmentId,
Picture = p.Picture,
AssignedPractitioner = p.AssignedPractitioner,
AssignedPractitionerId = p.AssignedPractitionerId,
HealthcareProvider = p.HealthcareProvider,
Active = p.Active,
Deceased = p.Deceased,
Expand Down
4 changes: 2 additions & 2 deletions Carefusion.Data/Repositories/PractitionerRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public IQueryable<Practitioner> GetQuery()
Role = p.Role,
GovernmentId = p.GovernmentId,
Picture = p.Picture,
AssignedHospital = p.AssignedHospital,
AssignedDepartment = p.AssignedDepartment,
AssignedHospitalId = p.AssignedHospitalId,
AssignedDepartmentId = p.AssignedDepartmentId,
Active = p.Active,
RecordUpdated = p.RecordUpdated,
Communication = context.Communications
Expand Down
2 changes: 1 addition & 1 deletion Carefusion.Entities/Patient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class Patient
public required string GovernmentId { get; init; }
[StringLength(1024)]
public string? Picture { get; init; }
public int? AssignedPractitioner { get; init; }
public int? AssignedPractitionerId { get; init; }
[StringLength(50)]
public string? HealthcareProvider { get; init; }
[Required]
Expand Down
10 changes: 6 additions & 4 deletions Carefusion.Entities/Practitioner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,21 @@ public class Practitioner
[Required]
[StringLength(25)]
public required string Gender { get; init; }
[Required]
[StringLength(25)]
public string? Specialty { get; init; }
public required string Specialty { get; init; }
[Required]
[StringLength(25)]
public string? Title { get; init; }
public required string Title { get; init; }
[StringLength(50)]
public string? Role { get; init; }
[Required]
[StringLength(11)]
public required string GovernmentId { get; init; }
[StringLength(1024)]
public string? Picture { get; init; }
public int? AssignedHospital { get; init; }
public int? AssignedDepartment { get; init; }
public int? AssignedHospitalId { get; init; }
public int? AssignedDepartmentId { get; init; }
[Required]
public required bool Active { get; init; }
public List<Communication>? Communication { get; set; } = [];
Expand Down

0 comments on commit 85dd03a

Please sign in to comment.