public void Check_GetShoppingTrolley_WithTestData() { List <Product> productList = new() { new Product { Id = 1, Name = It.IsAny <string>(), Brand = It.IsAny <string>(), }, new Product { Id = 2, Name = It.IsAny <string>(), Brand = It.IsAny <string>(), }, new Product { Id = 3, Name = It.IsAny <string>(), Brand = It.IsAny <string>(), } }; _mockProductService.Setup(prod => prod.GetAllProducts()).Returns(productList); ShoppingTrolley shoppingTrolley = _sutShoppingTrolleyService.GetShoppingTrolley(); Assert.Equal(3, shoppingTrolley.ShoppingTrolleyItems.Count); }
public ShoppingTrolley GetShoppingTrolley() { shoppingTrolleyItems = new List <ShoppingItem>(); productList = _productService.GetAllProducts(); #region Business validation if (productList == null) { throw new NotFoundException(CommonConstants.NO_PRODUCTS_FOUND_EXCEPTION); } #endregion productList.ForEach((product) => { shoppingTrolleyItems.Add(new ShoppingItem(product, 0)); }); shoppingTrolley = new() { ShoppingTrolleyItems = shoppingTrolleyItems, TotalDiscount = 0, TotalPrice = 0, TotalPriceWithoutDiscount = 0, PromotionOffer = CommonConstants.TROLLEY_PROMOTIONAL_OFFER }; return(shoppingTrolley); } }
public IActionResult Post([FromBody] ShoppingTrolley trolley) { if (!_trolleyValidator.Validate(trolley)) { return(BadRequest()); } return(Ok(trolley.CalculateTotal())); }
public IActionResult TrolleyCalculator(ShoppingTrolley shoppingTrolley) { try { var total = _shoppingTrolleyService.GetLowestTrolleyTotal(shoppingTrolley); return(Ok(total)); } catch (Exception) { return(StatusCode(StatusCodes.Status500InternalServerError, "Application Failure")); } }
public void Check_GetShoppingTrolley() { ShoppingTrolley shoppingTrolley = _shoppingTrolleyController.GetShoppingTrolley(); Assert.IsType <ShoppingTrolley>(shoppingTrolley); Assert.Equal(4, shoppingTrolley.ShoppingTrolleyItems.Count); Assert.Equal(CommonConstants.TROLLEY_PROMOTIONAL_OFFER, shoppingTrolley.PromotionOffer); List <ShoppingItem> shoppingItemList = new() { new ShoppingItem(new Product() { Brand = CommonConstants.VB_BRAND }, 0) }; Assert.Equal(shoppingItemList.First().Product.Brand, shoppingTrolley.ShoppingTrolleyItems.Where(prod => prod.Product.Brand == CommonConstants.VB_BRAND).Select(prod => prod.Product.Brand).FirstOrDefault()); }
public async Task <ShoppingTrolley> AddAsync(ShoppingTrolleyDto dto, CancellationToken token = default) { using (var db = new GuoGuoCommunityContext()) { //if (!Guid.TryParse(dto.OwnerCertificationRecordId, out var ocrId)) //{ // throw new NotImplementedException("业主ID无效!"); //} if (!Guid.TryParse(dto.ShopCommodityId, out var scId)) { throw new NotImplementedException("店铺商品ID无效!"); } var user = await db.Users.Where(x => x.Id.ToString() == dto.OperationUserId && x.IsDeleted == false).FirstOrDefaultAsync(token); if (user == null) { throw new NotImplementedException("创建人信息不存在!"); } //ShoppingTrolley model = new ShoppingTrolley(); ShoppingTrolley model = await db.ShoppingTrolleys.Where(item => item.ShopCommodityId == scId && item.CreateOperationUserId == user.Id).FirstOrDefaultAsync(token); if (model != null) { model.CommodityCount += dto.CommodityCount; } else { model = db.ShoppingTrolleys.Add(new ShoppingTrolley { //OwnerCertificationRecordId = ocrId, ShopCommodityId = scId, CommodityCount = dto.CommodityCount, CreateOperationTime = dto.OperationTime, CreateOperationUserId = user.Id, LastOperationTime = dto.OperationTime, LastOperationUserId = dto.OperationUserId }); } await db.SaveChangesAsync(token); return(model); } throw new NotImplementedException(); }
public decimal GetLowestTrolleyTotal(ShoppingTrolley request) { var trolleyTotalList = new List <decimal>(); decimal total = 0; total = CalculateTotalPrice(request.Products, request.Quantities); trolleyTotalList.Add(total); foreach (var specialItem in request.Specials) { if (IsSepcialApplicable(specialItem, request.Quantities)) { var specialItemTotal = specialItem.Total; var remainingItemsList = GetRemainingProductQuantity(specialItem, request.Quantities); remainingItemsList = ApplySpecials(request.Specials, remainingItemsList, ref specialItemTotal); specialItemTotal = CalculateTotalPrice(request.Products, remainingItemsList); trolleyTotalList.Add(specialItemTotal); } } return(trolleyTotalList.OrderBy(c => c).FirstOrDefault()); }
public void Check_GetShoppingTrolley_ReturnsNull() { ShoppingTrolley shoppingTrolley = _sutController.GetShoppingTrolley(); Assert.Null(shoppingTrolley); }
public bool Validate(ShoppingTrolley trolley) { // Todo: Validate products, specials and quantities return(trolley != null); }