public async Task <IActionResult> ApplyVouchers([FromBody] ApplyVouchers body) { var fetchItemsTask = new List <Task <Item> >(); body.ItemIdQuantities.ForEach(i => fetchItemsTask.Add(_itemsRepository.GetById(i.ItemId))); var items = await Task.WhenAll(fetchItemsTask).ConfigureAwait(false); if (items.Length != body.ItemIdQuantities.Count) { return(BadRequest("This basket contains invalid items")); } var vouchers = new List <Voucher>(); foreach (var code in body.Codes) { var voucher = await _voucherRepository.GetByCode(code).ConfigureAwait(false); if (voucher == null) { return(BadRequest($"Voucher Code '{code}' is not valid")); } vouchers.Add(voucher); } var applicationResponse = await FormatVouchers(vouchers, ApplyQuantities(items, body.ItemIdQuantities)).ConfigureAwait(false); if (applicationResponse.Item2 != null) { return(BadRequest(applicationResponse.Item2)); } return(Ok(applicationResponse.Item1)); }
async Task <Voucher> FindVoucher(string userId, string code) { var voucher = await VoucherRepository.GetByCode(code); ValidateVoucher(voucher, userId); return(voucher); }
public async Task <SubscriptionInfo> GetSubscriptionInfo(SubscriptionInfoArgs args) { var result = await Repository.GetByCode(args.PurchaseToken); if (result is null) { return(SubscriptionInfo.NotFound); } return(CreateSubscription(args.UserId, result)); }
public CustomerMakeBookingParameterSet Map(CustomerMakeBookingRequest request) { var parameterSet = PropertyMapper.MapMatchingProperties <CustomerMakeBookingRequest, CustomerMakeBookingParameterSet>(request); parameterSet.CreatedOn = DateTime.Now; parameterSet.Bus = _busRepository.GetById(request.BusId); parameterSet.Voucher = !string.IsNullOrEmpty(request.VoucherCode) ? _voucherRepository.GetByCode(request.VoucherCode) : null; parameterSet.CurrentUser = _userRepository.GetByUsername("Application"); return(parameterSet); }
public async Task <string> Generate(TimeSpan duration, string productId, string comments) { const string UNAMBIGUOUS_LETTERS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"; var code = Enumerable.Range(0, 12).Select(x => UNAMBIGUOUS_LETTERS.PickRandom()).ToString(""); if (await VoucherRepository.GetByCode(code) is not null) { return(await Generate(duration, productId, comments)); } await VoucherRepository.Add(new Voucher { Id = Guid.NewGuid().ToString(), Code = code, Duration = duration, ProductId = productId, Comments = comments }); return(code); }