public async Task <Result <VoidObject, ProblemDetails> > Cancel(int bookingId, ServiceAccount serviceAccount) { var(_, isGetBookingFailure, booking, getBookingError) = await _bookingRecordsManager.Get(bookingId); if (isGetBookingFailure) { return(ProblemDetailsBuilder.Fail <VoidObject>(getBookingError)); } return(await CancelBooking(booking, serviceAccount.ToUserInfo())); }
public Task <Result <BatchOperationResult> > Charge(List <int> bookingIds, ServiceAccount serviceAccount) { return(ExecuteBatchAction(bookingIds, IsBookingValidForChargePredicate, Charge, serviceAccount)); async Task <Result <string> > Charge(Booking booking, UserInfo serviceAcc) { var chargeResult = await _bookingPaymentService.Charge(booking, serviceAccount.ToUserInfo()); if (chargeResult.IsFailure) { await _bookingManagementService.Cancel(booking.Id, serviceAccount); } return(chargeResult); } }
public Task <Result <BatchOperationResult> > Capture(List <int> bookingIds, ServiceAccount serviceAccount) { return(ExecuteBatchAction(bookingIds, IsBookingValidForCapturePredicate, Capture, serviceAccount)); Task <Result <string> > Capture(Booking booking, UserInfo serviceAcc) => _bookingPaymentService.Capture(booking, serviceAccount.ToUserInfo()); }
private async Task <Result <BatchOperationResult> > ExecuteBatchAction(List <int> bookingIds, Expression <Func <Booking, bool> > predicate, Func <Booking, UserInfo, Task <Result <string> > > action, ServiceAccount serviceAccount) { var bookings = await GetBookings(); return(await ValidateCount() .Map(ProcessBookings)); Task <List <Booking> > GetBookings() => _context.Bookings .Where(booking => bookingIds.Contains(booking.Id)) .Where(predicate) .ToListAsync(); Result ValidateCount() => bookings.Count != bookingIds.Count ? Result.Failure("Invalid booking ids. Could not find some of requested bookings.") : Result.Success(); Task <BatchOperationResult> ProcessBookings() => Combine(bookings.Select(booking => action(booking, serviceAccount.ToUserInfo()))); async Task <BatchOperationResult> Combine(IEnumerable <Task <Result <string> > > results) { var builder = new StringBuilder(); bool hasErrors = false; foreach (var result in results) { var(_, isFailure, value, error) = await result; if (isFailure) { hasErrors = true; } builder.AppendLine(isFailure ? error : value); } return(new BatchOperationResult(builder.ToString(), hasErrors)); } }