adding the start to a coupon service, pushing to test

This commit is contained in:
Chris Witte 2021-04-23 10:41:07 -05:00
parent 1cd2d41b43
commit 37715c3e27
7 changed files with 83 additions and 3 deletions

View File

@ -13,6 +13,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
{
private IOrderingService _orderSvc;
private IBasketService _basketSvc;
private ICouponService _couponSvc;
private readonly IIdentityParser<ApplicationUser> _appUserParser;
public OrderController(IOrderingService orderSvc, IBasketService basketSvc, IIdentityParser<ApplicationUser> appUserParser)
{
@ -41,7 +42,6 @@ namespace Microsoft.eShopOnContainers.WebMVC.Controllers
{
var user = _appUserParser.Parse(HttpContext.User);
var basket = _orderSvc.MapOrderToBasket(model);
await _basketSvc.Checkout(basket);
//Redirect to historic list.

View File

@ -18,12 +18,14 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
private readonly ILogger<BasketService> _logger;
private readonly string _basketByPassUrl;
private readonly string _purchaseUrl;
private ICouponService _couponSvc;
public BasketService(HttpClient httpClient, IOptions<AppSettings> settings, ILogger<BasketService> logger)
public BasketService(HttpClient httpClient, IOptions<AppSettings> settings, ILogger<BasketService> logger, ICouponService couponService)
{
_apiClient = httpClient;
_settings = settings;
_logger = logger;
_couponSvc = couponService;
_basketByPassUrl = $"{_settings.Value.PurchaseUrl}/b/api/v1/basket";
_purchaseUrl = $"{_settings.Value.PurchaseUrl}/api/v1";
@ -53,6 +55,33 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
return basket;
}
public async Task<Basket> ApplyCoupon(Basket basket)
{
var uri = API.Basket.UpdateBasket(_basketByPassUrl);
var basketUpdate = new
{
BuyerId = basket.BuyerId,
Items = basket.Items.Select(item => new
{
Id = item.Id,
ProductId = item.ProductId,
ProductName = item.ProductName,
UnitPrice = item.UnitPrice * (decimal)0.10,
OldUnitPrice = item.UnitPrice,
Quantity = item.Quantity,
PictureUrl = item.PictureUrl
}).ToArray()
};
var basketContent = new StringContent(JsonConvert.SerializeObject(basketUpdate), System.Text.Encoding.UTF8, "application/json");
var response = await _apiClient.PostAsync(uri, basketContent);
response.EnsureSuccessStatusCode();
return basket;
}
public async Task Checkout(BasketDTO basket)
{

View File

@ -0,0 +1,36 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using WebMVC.Infrastructure;
using WebMVC.Services.ModelDTOs;
namespace Microsoft.eShopOnContainers.WebMVC.Services
{
public class CouponService : ICouponService
{
public Basket Apply(Basket basket)
{
//Basket updatedBasket = new Basket();
//updatedBasket.BuyerId = basket.BuyerId;
//// Todo: Stub for now, should reach out to a coupon microservice
//if (basket.CouponCode == "SM360")
//{
// foreach (BasketItem item in basket.Items)
// {
// item.UnitPrice = item.UnitPrice - (item.UnitPrice * (decimal)0.1);
// }
//}
//else
//{
throw new System.NotImplementedException();
//}
//return basket;
}
}
}

View File

@ -11,6 +11,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.Services
Task AddItemToBasket(ApplicationUser user, int productId);
Task<Basket> UpdateBasket(Basket basket);
Task Checkout(BasketDTO basket);
Task<Basket> ApplyCoupon(Basket basket);
Task<Basket> SetQuantities(ApplicationUser user, Dictionary<string, int> quantities);
Task<Order> GetOrderDraft(string basketId);
}

View File

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
using System.Collections.Generic;
using System.Threading.Tasks;
using WebMVC.Services.ModelDTOs;
namespace Microsoft.eShopOnContainers.WebMVC.Services
{
public interface ICouponService
{
Basket Apply(Basket basket);
}
}

View File

@ -15,6 +15,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
public decimal Total()
{
// What is we apply the discount here
return Math.Round(Items.Sum(x => x.UnitPrice * x.Quantity), 2);
}
}

View File

@ -61,7 +61,7 @@ namespace Microsoft.eShopOnContainers.WebMVC.ViewModels
[Required]
public Guid RequestId { get; set; }
public string CouponCode { get; internal set; }
public void CardExpirationShortFormat()
{