Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using WebNET.Source.DTO;
- using WebNET.Source.Model;
- using WebNET.Data;
- using WebNET.Source.Service.Interface;
- using Microsoft.AspNetCore.Http;
- using Microsoft.EntityFrameworkCore;
- using AutoMapper;
- using System.Diagnostics;
- using Microsoft.EntityFrameworkCore;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- namespace WebNET.Source.Service
- {
- public class PatientService : IPatient
- {
- private readonly ApplicationDBContext dbContext;
- //private readonly IMapper mapper;
- public PatientService(ApplicationDBContext dbContext/*, IMapper mapper*/)
- {
- this.dbContext = dbContext;
- //this.mapper = mapper;
- }
- public async Task<Guid> CreatePatient(PatientDTO patient)
- {
- Guid patientId = Guid.NewGuid();
- _ = new PatientModel();
- if (patient.Birthday >= DateTime.Now)
- {
- Exception exception = new();
- exception.Data.Add(StatusCodes.Status400BadRequest.ToString(), $"Birthdate cannot be later than {DateTime.Now}");
- throw exception;
- }
- if (!Enum.IsDefined(typeof(GenderEnum), patient.Gender))
- {
- Exception exception = new();
- exception.Data.Add(StatusCodes.Status400BadRequest.ToString(), $"Gender can only be {GenderEnum.Male} or {GenderEnum.Female}");
- throw exception;
- }
- if (patient != null)
- {
- PatientModel newPatient = new()
- {
- Id = patientId,
- Birthday = patient.Birthday,
- CreateTime = DateTime.UtcNow,
- Name = patient.Name,
- Gender = patient.Gender
- };
- _ = dbContext.PatientModels.Add(newPatient);
- _ = await dbContext.SaveChangesAsync();
- return patientId;
- }
- else
- {
- Exception exception = new();
- exception.Data.Add(StatusCodes.Status400BadRequest.ToString(), "Error during registration :(");
- throw exception;
- }
- }
- public async Task<Guid> CreateInspection(CreateInspectionDTO createInspectionDTO, Guid patientId, Guid doctorId)
- {
- var inspectionId = Guid.NewGuid();
- // Preliminary checks
- await PerformPreInspectionChecks(createInspectionDTO, patientId, doctorId);
- //Create a new inspection
- var commentsList = CreateComments(createInspectionDTO.Consultations, doctorId);
- var diagnosesList = await CreateDiagnoses(createInspectionDTO.Diagnoses, patientId, doctorId);
- var consultationsList = await CreateConsultations(createInspectionDTO.Consultations, inspectionId, doctorId);
- //var newInspection = mapper.Map<InspectionModel>(createInspectionDTO);
- //newInspection.Id = inspectionId;
- //newInspection.PatientId = patientId;
- //newInspection.DoctorId = doctorId;
- // Additional logic...
- try
- {
- dbContext.InspectionModels.AddRange();
- await dbContext.SaveChangesAsync();
- }
- catch (DbUpdateException ex)
- {
- Console.WriteLine("Error while saving a new inspection: " + ex.InnerException?.Message);
- throw;
- }
- catch (Exception ex)
- {
- Console.WriteLine("Unknown error occurred: " + ex.Message);
- throw;
- }
- return inspectionId;
- }
- private async Task PerformPreInspectionChecks(CreateInspectionDTO createInspectionDTO, Guid patientId, Guid doctorId)
- {
- ValidateInspectionDate(createInspectionDTO.Date);
- ValidateNextVisitDate(createInspectionDTO.NextVisitDate, createInspectionDTO.DeathDate);
- CheckIfPatientIsDeceased(createInspectionDTO.PreviousInspectionId, patientId);
- CheckIfInspectionExists(createInspectionDTO.PreviousInspectionId, patientId);
- CheckIfNextVisitDateIsCorrect(createInspectionDTO.NextVisitDate, createInspectionDTO.PreviousInspectionId, patientId);
- CheckForDuplicateConsultationSpecialties(createInspectionDTO.Consultations);
- CheckForSingleMainDiagnosis(createInspectionDTO.Diagnoses);
- CheckIfPatientHasRecovered(createInspectionDTO.PreviousInspectionId, patientId);
- CheckIfPreviousInspectionHasChild(createInspectionDTO.PreviousInspectionId);
- }
- private void ValidateInspectionDate(DateTime inspectionDate)
- {
- if (inspectionDate > DateTime.UtcNow)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "Inspection date and time cannot be later than the current date and time");
- }
- }
- private void ValidateNextVisitDate(DateTime? nextVisitDate, DateTime? deathDate)
- {
- if (nextVisitDate != null)
- {
- if (nextVisitDate < DateTime.UtcNow)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "Date and time of the next visit cannot be earlier than the current date and time");
- }
- if (deathDate != null)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "The patient has passed away, so there should be no next visit date. Please remove it.");
- }
- }
- }
- private void CheckIfPatientIsDeceased(Guid? previousInspectionId, Guid patientId)
- {
- var patientDeathCheck = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.DeathDate != null && x.PatientId == patientId);
- if (patientDeathCheck != null)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "The patient has passed away");
- }
- }
- private void CheckIfInspectionExists(Guid? previousInspectionId, Guid patientId)
- {
- if (previousInspectionId != null && previousInspectionId != Guid.Empty)
- {
- var baseInspection = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.PatientId == patientId);
- if (baseInspection == null)
- {
- throw new CustomException(StatusCodes.Status404NotFound, "Inspection not found");
- }
- }
- }
- private void CheckIfNextVisitDateIsCorrect(DateTime? nextVisitDate, Guid? previousInspectionId, Guid patientId)
- {
- if (nextVisitDate != null)
- {
- var checkTime = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.PatientId == patientId && x.NextVisitDate > nextVisitDate);
- if (checkTime != null)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "Inspection date and time cannot be earlier than the date and time of the previous inspection");
- }
- }
- }
- private void CheckForDuplicateConsultationSpecialties(List<ConsultationDTO> consultations)
- {
- if (consultations != null && consultations.Select(x => x.SpecialityId).Distinct().Count() != consultations.Count)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "An inspection cannot have multiple consultations with the same doctor's specialty");
- }
- }
- private void CheckForSingleMainDiagnosis(List<DiagnosesDTO> diagnoses)
- {
- if (diagnoses != null && diagnoses.Count(x => x.Type == DiagnosesType.Main) != 1)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "An inspection must have exactly one diagnosis with the diagnosis type 'Main'");
- }
- }
- private void CheckIfPatientHasRecovered(Guid? previousInspectionId, Guid patientId)
- {
- var checkOnConclusion = dbContext.InspectionModels.FirstOrDefault(x => x.Id == previousInspectionId && x.PatientId == patientId && x.Conclusion == Conclusion.Recovery);
- if (checkOnConclusion != null)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "The patient has already recovered");
- }
- }
- private void CheckIfPreviousInspectionHasChild(Guid? previousInspectionId)
- {
- var childInspection = dbContext.InspectionModels.FirstOrDefault(x => x.PreviousInspectionId == previousInspectionId);
- if (childInspection != null)
- {
- throw new CustomException(StatusCodes.Status400BadRequest, "This inspection already has a child inspection");
- }
- }
- public class CustomException : Exception
- {
- public int StatusCode { get; }
- public CustomException(int statusCode, string message) : base(message)
- {
- StatusCode = statusCode;
- }
- }
- private List<CommentModel> CreateComments(List<ConsultationDTO> consultationsDto, Guid doctorId)
- {
- var commentsList = new List<CommentModel>();
- var author = dbContext.Doctors.FirstOrDefault(x => x.Id == doctorId);
- foreach (var consultationDto in consultationsDto)
- {
- var comment = new CommentModel
- {
- Id = Guid.NewGuid(),
- CreateTime = DateTime.UtcNow,
- Content = consultationDto.Comment.Content,
- AuthorId = doctorId,
- Author = author.Name,
- ModifiedDate = null,
- ParentId = null
- };
- commentsList.Add(comment);
- }
- return commentsList;
- }
- private async Task<List<DiagnosesModel>> CreateDiagnoses(List<DiagnosesDTO> diagnosesDto, Guid patientId, Guid doctorId)
- {
- var diagnosesList = new List<DiagnosesModel>();
- foreach (var diagnosisDto in diagnosesDto)
- {
- var diagnoseCode = await dbContext.ICD_10.FirstOrDefaultAsync(x => x.ID == diagnosisDto.IcdDiagnosisId);
- if (diagnoseCode != null)
- {
- var diagnosis = new DiagnosesModel
- {
- Code = diagnoseCode.REC_CODE,
- CreateTime = DateTime.UtcNow,
- Description = diagnosisDto.Description,
- Id = Guid.NewGuid(),
- Name = diagnoseCode.MKB_NAME,
- Type = diagnosisDto.Type
- };
- diagnosesList.Add(diagnosis);
- }
- else
- {
- throw new CustomException(StatusCodes.Status404NotFound, "DiagnosisId not found");
- }
- }
- return diagnosesList;
- }
- private async Task<List<ConsultationModel>> CreateConsultations(List<ConsultationDTO> consultationsDto, Guid inspectionId, Guid doctorId)
- {
- var consultationsList = new List<ConsultationModel>();
- foreach (var consultationDto in consultationsDto)
- {
- var specialityExists = await dbContext.SpecialityModels.AnyAsync(x => x.Id == consultationDto.SpecialityId);
- if (!specialityExists)
- {
- throw new CustomException(StatusCodes.Status404NotFound, "specialityId not found");
- }
- var consultation = new ConsultationModel
- {
- Id = Guid.NewGuid(),
- CreateTime = DateTime.UtcNow,
- InspectionId = inspectionId,
- SpecialityId = consultationDto.SpecialityId,
- CommentId = Guid.NewGuid()
- };
- consultationsList.Add(consultation);
- }
- return consultationsList;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement