public async Task CheckoutBasket(BasketCheckoutModel model) { var response = await _client.PostAsJson($"/Basket/Checkout", model); if (!response.IsSuccessStatusCode) { throw new Exception("Something went wrong when calling api."); } }
public static BasketCheckoutEvent ToEvent( this BasketCheckoutModel source, ICollection <BasketLine> basketLines = null, CouponModel coupon = null) { if (source == null) { return(null); } var basketCheckoutEvent = new BasketCheckoutEvent { Address = source.Address, City = source.City, Country = source.Country, Email = source.Email, BasketId = source.BasketId, BasketTotal = 0, CardExpiration = source.CardExpiration, CardName = source.CardName, CardNumber = source.CardNumber, FirstName = source.FirstName, LastName = source.LastName, UserId = source.UserId, ZipCode = source.ZipCode }; if (basketLines == null || !basketLines.Any()) { return(basketCheckoutEvent); } basketCheckoutEvent.BasketLines = new List <BasketLineEvent>(); foreach (var basketLine in basketLines.ToList()) { var basketLineEvent = new BasketLineEvent { BasketLineId = basketLine.BasketLineId, Price = basketLine.Price, TicketAmount = basketLine.TicketAmount }; basketCheckoutEvent.BasketTotal += basketLine.Price * basketLine.TicketAmount; basketCheckoutEvent.BasketLines.Add(basketLineEvent); } if (coupon == null) { return(basketCheckoutEvent); } basketCheckoutEvent.BasketTotal -= coupon.Amount; return(basketCheckoutEvent); }
public async Task CheckoutBasket(BasketCheckoutModel model) { var message = new HttpRequestBuilder(settings.BaseAddress) .SetPath(settings.CatalogPath) .AddToPath("Checkout") .HttpMethod(HttpMethod.Post) .GetHttpMessage(); var json = JsonConvert.SerializeObject(model); message.Content = new StringContent(json, Encoding.UTF8, "application/json"); await SendRequest <BasketCheckoutModel>(message); }
public async Task <IActionResult> CheckoutBasketAsync([FromBody] BasketCheckoutModel basketCheckout) { try { var basket = await _mediator.Send(new GetBasketByIdQuery(basketCheckout.BasketId)); if (basket == null) { return(BadRequest()); } // TODO: Применить скидку. CouponModel coupon = null; if (basket.CouponId.HasValue) { coupon = (await _discountService.GetCoupon(basket.CouponId.Value))?.ToModel(); } // TODO: Discount with error for check policy. //if (basket.CouponId.HasValue) // coupon = await discountService.GetCouponWithError(basket.CouponId.Value); var basketCheckoutEvent = basketCheckout.ToEvent(basket.BasketLines, coupon); try { _eventBus.Publish(basketCheckoutEvent); } catch (Exception e) { Console.WriteLine(e); throw; } await _mediator.Send(new ClearBasketCommand(basketCheckout.BasketId)); return(Accepted(basketCheckoutEvent)); } catch (BrokenCircuitException ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.StackTrace)); } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.StackTrace)); } }