public async Task <IActionResult> Cancelled(Guid id, CancellationToken cancellationToken) { var command = new PaymentApprovalCommand <Guid, PaymentTransaction, PaymentReadModel>(id, User, (int)PaymentStatus.Canceled); var result = await Mediator.Send(command, cancellationToken).ConfigureAwait(false); return(ObjectResult(result, StatusCodes.Status200OK)); }
protected override async Task <PaymentReadModel> ProcessAsync(PaymentApprovalCommand <Guid, PaymentTransaction, PaymentReadModel> message, CancellationToken cancellationToken) { var command = new EntityIdentifierQuery <Guid, PaymentTransaction, PaymentReadModel>(message.Id, message.Principal); var result = await Mediator.Send(command, cancellationToken).ConfigureAwait(false); if (result == null) { throw new DomainException(422, $"Payment Id '{message.Id}' not found."); } if (result.Status != message.PaymentStatus) { var commandCourse = new EntityIdentifierQuery <Guid, TrainingCourse, CourseReadModel>(result.CourseId.Value, message.Principal); var resultCourse = await Mediator.Send(commandCourse, cancellationToken).ConfigureAwait(false); if (resultCourse == null) { throw new DomainException(422, $"Course Id '{result.CourseId}' not found."); } if (resultCourse.MaxAttendee == resultCourse.NoAttendee) { throw new DomainException(422, $"Course '{resultCourse.Title}' is already full."); } var mapCourse = _mapper.Map <CourseUpdateModel>(resultCourse); mapCourse.MaxAttendee = mapCourse.MaxAttendee - 1; mapCourse.NoAttendee = mapCourse.NoAttendee + 1; var updateCourse = new EntityUpdateCommand <Guid, TrainingCourse, CourseUpdateModel, CourseReadModel>(resultCourse.Id, mapCourse, message.Principal); var mediatorCourse = await Mediator.Send(updateCourse, cancellationToken) .ConfigureAwait(false); result.Status = message.PaymentStatus; var map = _mapper.Map <PaymentUpdateModel>(result); var update = new EntityUpdateCommand <Guid, PaymentTransaction, PaymentUpdateModel, PaymentReadModel>(message.Id, map, message.Principal); result = await Mediator.Send(update, cancellationToken).ConfigureAwait(false); var history = _mapper.Map <PaymentTransactionHistory>(result); var dbSetHistory = _dataContext.Set <Data.Entities.PaymentTransactionHistory>(); await dbSetHistory.AddAsync(history, cancellationToken).ConfigureAwait(false); await _dataContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); var mapAttendee = _mapper.Map <TrainingBuildCoursesAttendeeCreatedModel>(result); var courseAttendee = new EntityCreateCommand <Core.Data.Entities.TrainingBuildCourseAttendee, TrainingBuildCoursesAttendeeCreatedModel, TrainingBuildCoursesAttendeeReadModel>(mapAttendee, message.Principal); var resultCourseAttendee = await Mediator.Send(courseAttendee, cancellationToken).ConfigureAwait(false); } return(result); }