public async Task <IResult <IMessageOutput> > Handle(TreatmentSessionInput input, CancellationToken cancellationToken) { var validationResult = Validator.Validate(input); if (validationResult.Errors.Count > 0) { return(new Result <IMessageOutput>(validationResult.Errors)); } var patient = await PatientRepository.SingleOrDefaultAsync(p => p.ReferenceId == input.PatientReferenceId, cancellationToken); var dentalTeam = await DentalTeamRepository.SingleOrDefaultAsync(dt => dt.ReferenceId == input.DentalTeamReferenceId, cancellationToken); var treatment = await TreatmentRepository.SingleOrDefaultAsync(t => t.ReferenceId == input.TreatmentReferenceId, cancellationToken); var treatmentSession = new TreatmentSession() { PatientId = patient.Id, DentalTeamId = dentalTeam.Id, TreatmentId = treatment.Id, Start = input.Start.Value, End = input.End.Value }; await TreatmentSessionRepository.AddAsync(treatmentSession, cancellationToken); await UoW.SaveAsync(cancellationToken); return(new Result <IMessageOutput>( value: new MessageOutput("Treatment Session successfully created."), type: ResultType.Created )); }
public async Task <IResult <IMessageOutput> > ExecuteAsync(ITreatmentSessionInput input) { var validationResult = Validator.Validate(input); if (validationResult.Errors.Count > 0) { return(new Result <IMessageOutput>(validationResult.Errors)); } var treatmentSession = await TreatmentSessionRepository.Where( ts => ts.ReferenceId == input.ReferenceId ) .Include(ts => ts.Treatment) .Include(ts => ts.DentalTeam) .SingleOrDefaultAsync(); if (treatmentSession == null) { return(new Result <IMessageOutput>( new List <IError> { new Error( ErrorType.NotFound, $"Treatment session for the given Patient, Dental Team and period cannot be found." ) }, ResultType.NotFound )); } if (treatmentSession.Treatment.ReferenceId != input.TreatmentReferenceId) { var treatment = await TreatmentRepository.SingleOrDefaultAsync( t => t.ReferenceId == input.TreatmentReferenceId ); treatmentSession.TreatmentId = treatment.Id; } if (treatmentSession.DentalTeam.ReferenceId != input.DentalTeamReferenceId) { var dentalTeam = await DentalTeamRepository.SingleOrDefaultAsync( dt => dt.ReferenceId == input.DentalTeamReferenceId ); treatmentSession.DentalTeamId = dentalTeam.Id; } treatmentSession.Start = input.Start.Value; treatmentSession.End = input.End.Value; treatmentSession.Status = Enum.Parse <TreatmentSessionStatus>(input.Status); await UoW.SaveAsync(); return(new Result <IMessageOutput>( value: new MessageOutput("Treatment Session successfully updated."), type: ResultType.Updated )); }
public async Task <IResult <IMessageOutput> > Handle(UpdateTreatmentSessionInput input, CancellationToken cancellationToken) { var validationResult = Validator.Validate(input); if (validationResult.Errors.Count > 0) { return(new Result <IMessageOutput>(validationResult.Errors)); } var treatmentSession = await TreatmentSessionRepository.Where( ts => ts.ReferenceId == input.ReferenceId ) .Include(ts => ts.Treatment) .Include(ts => ts.DentalTeam) .SingleOrDefaultAsync(cancellationToken); if (treatmentSession.Treatment.ReferenceId != input.TreatmentReferenceId) { var treatment = await TreatmentRepository.SingleOrDefaultAsync( t => t.ReferenceId == input.TreatmentReferenceId, cancellationToken ); treatmentSession.TreatmentId = treatment.Id; } if (treatmentSession.DentalTeam.ReferenceId != input.DentalTeamReferenceId) { var dentalTeam = await DentalTeamRepository.SingleOrDefaultAsync( dt => dt.ReferenceId == input.DentalTeamReferenceId, cancellationToken ); treatmentSession.DentalTeamId = dentalTeam.Id; } treatmentSession.Start = input.Start.Value; treatmentSession.End = input.End.Value; treatmentSession.Status = Enum.Parse <TreatmentSessionStatus>(input.Status); await UoW.SaveAsync(cancellationToken); return(new Result <IMessageOutput>( value: new MessageOutput("Treatment Session successfully updated."), type: ResultType.Updated )); }