Advertisement
Tark_Wight

PatientService

Jan 7th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.39 KB | None | 0 0
  1. using WebNET.Source.DTO;
  2. using WebNET.Source.Model;
  3. using WebNET.Data;
  4. using WebNET.Source.Service.Interface;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.EntityFrameworkCore;
  7. using AutoMapper;
  8. using System.Diagnostics;
  9. using Microsoft.EntityFrameworkCore;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Threading.Tasks;
  14.  
  15. namespace WebNET.Source.Service
  16. {
  17. public class PatientService : IPatient
  18. {
  19. private readonly ApplicationDBContext dbContext;
  20. //private readonly IMapper mapper;
  21.  
  22. public PatientService(ApplicationDBContext dbContext/*, IMapper mapper*/)
  23. {
  24. this.dbContext = dbContext;
  25. //this.mapper = mapper;
  26. }
  27.  
  28. public async Task<Guid> CreatePatient(PatientDTO patient)
  29. {
  30. Guid patientId = Guid.NewGuid();
  31. _ = new PatientModel();
  32.  
  33. if (patient.Birthday >= DateTime.Now)
  34. {
  35. Exception exception = new();
  36. exception.Data.Add(StatusCodes.Status400BadRequest.ToString(), $"Birthdate cannot be later than {DateTime.Now}");
  37. throw exception;
  38. }
  39. if (!Enum.IsDefined(typeof(GenderEnum), patient.Gender))
  40. {
  41. Exception exception = new();
  42. exception.Data.Add(StatusCodes.Status400BadRequest.ToString(), $"Gender can only be {GenderEnum.Male} or {GenderEnum.Female}");
  43. throw exception;
  44. }
  45.  
  46. if (patient != null)
  47. {
  48. PatientModel newPatient = new()
  49. {
  50. Id = patientId,
  51. Birthday = patient.Birthday,
  52. CreateTime = DateTime.UtcNow,
  53. Name = patient.Name,
  54. Gender = patient.Gender
  55. };
  56. _ = dbContext.PatientModels.Add(newPatient);
  57. _ = await dbContext.SaveChangesAsync();
  58. return patientId;
  59. }
  60. else
  61. {
  62. Exception exception = new();
  63. exception.Data.Add(StatusCodes.Status400BadRequest.ToString(), "Error during registration :(");
  64. throw exception;
  65. }
  66. }
  67.  
  68. public async Task<Guid> CreateInspection(CreateInspectionDTO createInspectionDTO, Guid patientId, Guid doctorId)
  69. {
  70. var inspectionId = Guid.NewGuid();
  71.  
  72. // Preliminary checks
  73. await PerformPreInspectionChecks(createInspectionDTO, patientId, doctorId);
  74.  
  75. //Create a new inspection
  76. var commentsList = CreateComments(createInspectionDTO.Consultations, doctorId);
  77.  
  78. var diagnosesList = await CreateDiagnoses(createInspectionDTO.Diagnoses, patientId, doctorId);
  79.  
  80. var consultationsList = await CreateConsultations(createInspectionDTO.Consultations, inspectionId, doctorId);
  81. //var newInspection = mapper.Map<InspectionModel>(createInspectionDTO);
  82. //newInspection.Id = inspectionId;
  83. //newInspection.PatientId = patientId;
  84. //newInspection.DoctorId = doctorId;
  85.  
  86. // Additional logic...
  87.  
  88. try
  89. {
  90. dbContext.InspectionModels.AddRange();
  91. await dbContext.SaveChangesAsync();
  92. }
  93. catch (DbUpdateException ex)
  94. {
  95. Console.WriteLine("Error while saving a new inspection: " + ex.InnerException?.Message);
  96. throw;
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine("Unknown error occurred: " + ex.Message);
  101. throw;
  102. }
  103.  
  104. return inspectionId;
  105. }
  106.  
  107. private async Task PerformPreInspectionChecks(CreateInspectionDTO createInspectionDTO, Guid patientId, Guid doctorId)
  108. {
  109. ValidateInspectionDate(createInspectionDTO.Date);
  110.  
  111. ValidateNextVisitDate(createInspectionDTO.NextVisitDate, createInspectionDTO.DeathDate);
  112.  
  113. CheckIfPatientIsDeceased(createInspectionDTO.PreviousInspectionId, patientId);
  114.  
  115. CheckIfInspectionExists(createInspectionDTO.PreviousInspectionId, patientId);
  116.  
  117. CheckIfNextVisitDateIsCorrect(createInspectionDTO.NextVisitDate, createInspectionDTO.PreviousInspectionId, patientId);
  118.  
  119. CheckForDuplicateConsultationSpecialties(createInspectionDTO.Consultations);
  120.  
  121. CheckForSingleMainDiagnosis(createInspectionDTO.Diagnoses);
  122.  
  123. CheckIfPatientHasRecovered(createInspectionDTO.PreviousInspectionId, patientId);
  124.  
  125. CheckIfPreviousInspectionHasChild(createInspectionDTO.PreviousInspectionId);
  126. }
  127.  
  128. private void ValidateInspectionDate(DateTime inspectionDate)
  129. {
  130. if (inspectionDate > DateTime.UtcNow)
  131. {
  132. throw new CustomException(StatusCodes.Status400BadRequest, "Inspection date and time cannot be later than the current date and time");
  133. }
  134. }
  135.  
  136. private void ValidateNextVisitDate(DateTime? nextVisitDate, DateTime? deathDate)
  137. {
  138. if (nextVisitDate != null)
  139. {
  140. if (nextVisitDate < DateTime.UtcNow)
  141. {
  142. throw new CustomException(StatusCodes.Status400BadRequest, "Date and time of the next visit cannot be earlier than the current date and time");
  143. }
  144.  
  145. if (deathDate != null)
  146. {
  147. throw new CustomException(StatusCodes.Status400BadRequest, "The patient has passed away, so there should be no next visit date. Please remove it.");
  148. }
  149. }
  150. }
  151.  
  152. private void CheckIfPatientIsDeceased(Guid? previousInspectionId, Guid patientId)
  153. {
  154. var patientDeathCheck = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.DeathDate != null && x.PatientId == patientId);
  155. if (patientDeathCheck != null)
  156. {
  157. throw new CustomException(StatusCodes.Status400BadRequest, "The patient has passed away");
  158. }
  159. }
  160.  
  161. private void CheckIfInspectionExists(Guid? previousInspectionId, Guid patientId)
  162. {
  163. if (previousInspectionId != null && previousInspectionId != Guid.Empty)
  164. {
  165. var baseInspection = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.PatientId == patientId);
  166. if (baseInspection == null)
  167. {
  168. throw new CustomException(StatusCodes.Status404NotFound, "Inspection not found");
  169. }
  170. }
  171. }
  172.  
  173. private void CheckIfNextVisitDateIsCorrect(DateTime? nextVisitDate, Guid? previousInspectionId, Guid patientId)
  174. {
  175. if (nextVisitDate != null)
  176. {
  177. var checkTime = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.PatientId == patientId && x.NextVisitDate > nextVisitDate);
  178. if (checkTime != null)
  179. {
  180. throw new CustomException(StatusCodes.Status400BadRequest, "Inspection date and time cannot be earlier than the date and time of the previous inspection");
  181. }
  182. }
  183. }
  184.  
  185. private void CheckForDuplicateConsultationSpecialties(List<ConsultationDTO> consultations)
  186. {
  187. if (consultations != null && consultations.Select(x => x.SpecialityId).Distinct().Count() != consultations.Count)
  188. {
  189. throw new CustomException(StatusCodes.Status400BadRequest, "An inspection cannot have multiple consultations with the same doctor's specialty");
  190. }
  191. }
  192.  
  193. private void CheckForSingleMainDiagnosis(List<DiagnosesDTO> diagnoses)
  194. {
  195. if (diagnoses != null && diagnoses.Count(x => x.Type == DiagnosesType.Main) != 1)
  196. {
  197. throw new CustomException(StatusCodes.Status400BadRequest, "An inspection must have exactly one diagnosis with the diagnosis type 'Main'");
  198. }
  199. }
  200.  
  201. private void CheckIfPatientHasRecovered(Guid? previousInspectionId, Guid patientId)
  202. {
  203. var checkOnConclusion = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.PatientId == patientId && x.Conclusion == Conclusion.Recovery);
  204. if (checkOnConclusion != null)
  205. {
  206. throw new CustomException(StatusCodes.Status400BadRequest, "The patient has already recovered");
  207. }
  208. }
  209.  
  210. private void CheckIfPreviousInspectionHasChild(Guid? previousInspectionId)
  211. {
  212. var childInspection = dbContext.InspectionModels.FirstOrDefault(x => x.PreviousInspectionId == previousInspectionId);
  213. if (childInspection != null)
  214. {
  215. throw new CustomException(StatusCodes.Status400BadRequest, "This inspection already has a child inspection");
  216. }
  217. }
  218.  
  219. public class CustomException : Exception
  220. {
  221. public int StatusCode { get; }
  222.  
  223. public CustomException(int statusCode, string message) : base(message)
  224. {
  225. StatusCode = statusCode;
  226. }
  227. }
  228.  
  229. private List<CommentModel> CreateComments(List<ConsultationDTO> consultationsDto, Guid doctorId)
  230. {
  231. var commentsList = new List<CommentModel>();
  232. var author = dbContext.Doctors.FirstOrDefault(x => x.Id == doctorId);
  233. foreach (var consultationDto in consultationsDto)
  234. {
  235. var comment = new CommentModel
  236. {
  237. Id = Guid.NewGuid(),
  238. CreateTime = DateTime.UtcNow,
  239. Content = consultationDto.Comment.Content,
  240. AuthorId = doctorId,
  241. Author = author.Name,
  242. ModifiedDate = null,
  243. ParentId = null
  244.  
  245. };
  246. commentsList.Add(comment);
  247. }
  248. return commentsList;
  249. }
  250.  
  251. private async Task<List<DiagnosesModel>> CreateDiagnoses(List<DiagnosesDTO> diagnosesDto, Guid patientId, Guid doctorId)
  252. {
  253. var diagnosesList = new List<DiagnosesModel>();
  254. foreach (var diagnosisDto in diagnosesDto)
  255. {
  256. var diagnoseCode = await dbContext.ICD_10.FirstOrDefaultAsync(x => x.ID == diagnosisDto.IcdDiagnosisId);
  257. if (diagnoseCode != null)
  258. {
  259. var diagnosis = new DiagnosesModel
  260. {
  261. Code = diagnoseCode.REC_CODE,
  262. CreateTime = DateTime.UtcNow,
  263. Description = diagnosisDto.Description,
  264. Id = Guid.NewGuid(),
  265. Name = diagnoseCode.MKB_NAME,
  266. Type = diagnosisDto.Type
  267.  
  268. };
  269. diagnosesList.Add(diagnosis);
  270. }
  271. else
  272. {
  273. throw new CustomException(StatusCodes.Status404NotFound, "DiagnosisId not found");
  274. }
  275. }
  276. return diagnosesList;
  277. }
  278.  
  279. private async Task<List<ConsultationModel>> CreateConsultations(List<ConsultationDTO> consultationsDto, Guid inspectionId, Guid doctorId)
  280. {
  281. var consultationsList = new List<ConsultationModel>();
  282. foreach (var consultationDto in consultationsDto)
  283. {
  284. var specialityExists = await dbContext.SpecialityModels.AnyAsync(x => x.Id == consultationDto.SpecialityId);
  285. if (!specialityExists)
  286. {
  287. throw new CustomException(StatusCodes.Status404NotFound, "specialityId not found");
  288. }
  289.  
  290. var consultation = new ConsultationModel
  291. {
  292. Id = Guid.NewGuid(),
  293. CreateTime = DateTime.UtcNow,
  294. InspectionId = inspectionId,
  295. SpecialityId = consultationDto.SpecialityId,
  296. CommentId = Guid.NewGuid()
  297. };
  298.  
  299. consultationsList.Add(consultation);
  300. }
  301. return consultationsList;
  302. }
  303. }
  304. }
  305.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement