public void ProduceOrder() { OrderService orderService = new OrderService(); BasketService basketService = new BasketService(); BasketModel basketModel = basketService.CreateNewBasket("C123"); BasketItemModel basketItemModel = new BasketItemModel(); basketItemModel.Id = "111"; basketItemModel.Name = "Car"; basketItemModel.Quantity = 1; basketItemModel.UnitPrice = (float)5.50; bool isAdded = basketService.AddBasketItem("C123", basketModel.Id, basketItemModel); CustomerModel customerModel = new CustomerModel(); customerModel.Id = ""; customerModel.ContactNumber = "0777777777"; customerModel.DeliveryAddress = "5th Street Somewhere"; customerModel.Name = "John Doe"; var obj = orderService.ProduceOrder(basketModel, customerModel, 0, 0); //Check the API does not produce an order if mandatory fields are missing for customer. Assert.Null(obj); }
public void TestIfMultiplePromotionsAddedForSameItemOnlyFirstIsTaken() { List <PromotionModel> promotions = new List <PromotionModel> { _appleDiscount, _appleGroupDiscount }; BasketItemModel appleBasketItem = new BasketItemModel(_apple); BasketModel basket = new BasketModel(); basket.AddOrUpdateBasket(appleBasketItem); basket.AddOrUpdateBasket(appleBasketItem); IPromotionService promotionService = new PromotionService(); promotionService.ApplyPromotionsToBasket(basket, promotions); Assert.AreEqual(1, basket.Basket.Count); Assert.AreEqual(2, basket.Total); Assert.IsTrue(basket.Basket[0].Discount != null); Assert.AreEqual("Apple", basket.Basket[0].ItemName); Assert.AreEqual(2, basket.Basket[0].ItemQuantity); Assert.AreEqual(1, basket.Basket[0].Discount.ThresholdQuantity); Assert.AreEqual(.5M, basket.Basket[0].Discount.DiscountAmount); Assert.AreEqual(1, basket.Basket[0].Discount.TotalDiscountAmount); }
public async Task <BasketItemModel> SetItem(Guid basketId, Guid productId, int quantity) { var basket = await Get(basketId); var item = basket.Items.SingleOrDefault(x => x.ProductId == productId); if (quantity == 0 && item != null) { basket.Items.Remove(item); } else if (item == null) { item = new BasketItemModel { Id = Guid.NewGuid(), ProductId = productId, Quantity = quantity }; basket.Items.Add(item); } else { item.Quantity = quantity; } if (!baskets.TryUpdate(basket.Id, basket, basket)) { throw new InvalidOperationException(); } return(item); }
public async Task <ActionResult <BasketItemModel> > Create(BasketItemModel basketItemModel) { var basketItem = new BasketItem { Name = basketItemModel.Name, Amount = basketItemModel.Amount, Hint = basketItemModel.Hint, Description = basketItemModel.Description, UserId = User.GetLoggedInUserId(), DateCreated = basketItemModel.DateCreated, ItemType = basketItemModel.ItemType }; await basketItemRepository.Create(basketItem); var result = MapToModel(basketItem); manager.Publish( message: result, exchangeName: "base.exchange.topic", exchangeType: ExchangeType.Topic, routeKey: "notifications.basket.add" ); return(new ObjectResult(result)); }
private async Task CreatePurchaseOrder(int buyerId, int[] catalogItemIds) { var basketModel = new CreateBasketModel { BuyerId = buyerId }; var createNewBasketCommand = _autoMapper.Map <CreateBasketModel, CreateBasketForUserCommand>(basketModel); await _commandBus.SendAsync(createNewBasketCommand); var basket = await _queryBus.SendAsync <GetBasketByBuyerId, Basket>(new GetBasketByBuyerId { BuyerId = buyerId }); foreach (var catalogItemId in catalogItemIds) { var basketItemModel = new BasketItemModel { BasketId = basket.BasketId, Price = 10M, Quantity = 1, CatalogItemId = catalogItemId }; var addItemToBasketCommand = _autoMapper.Map <BasketItemModel, AddItemToBasketCommand>(basketItemModel); await _commandBus.SendAsync(addItemToBasketCommand); } basket = await _queryBus.SendAsync <GetBasketByBuyerId, Basket>(new GetBasketByBuyerId { BuyerId = buyerId }); var createPurchaseOrderModel = new CreatePurchaseOrderModel(); createPurchaseOrderModel.BasketId = basket.BasketId.ToString(); var shippingAddress = new Address("126 BowhillWay", "Harlow", "Essex", "United Kingdom", "CM20 2FH"); var crateOrderCommand = new CreatePurchaseOrderCommand(Guid.Parse(createPurchaseOrderModel.BasketId), shippingAddress); await _commandBus.SendAsync(crateOrderCommand); }
public async Task AddItemAsync(string userId, BasketItemModel model) { foreach (var dish in model.Dishes) { var entity = await _context.Dishes.FirstOrDefaultAsync(x => x.Id == dish.Id); if (entity.IsBlock) { throw new ApiException("Блюдо заблокировано", 400); } var bd = await _context.BasketDishes.FirstOrDefaultAsync(x => x.UserId == userId); if (bd != null && bd.RestaurantId != entity.RestaurantId) { throw new ApiException("Невозможно добавить блюдо. В корзине уже есть блюда из другого ресторана.", 403); } var basketDish = await _context.BasketDishes.FirstOrDefaultAsync(x => x.UserId == userId && x.DishId == dish.Id); if (basketDish == null) { basketDish = new BasketDishEntity(userId, dish.Id, entity.MenuCategory.RestaurantId); await _context.BasketDishes.AddAsync(basketDish); } basketDish.Count += dish.Count; } await _context.SaveChangesAsync(); }
public ServiceResponse UpdateBasketItem(UserSessionModel user, BasketItemModel item) { this.Response = new ServiceResponse(); var basket = GetUserBasketModel(user); if (item == null) { this.Response.Model = basket; return(this.Response); } var basketItem = Db.UserBasketItems.Where(i => i.UserId == user.UserId && i.ItemId == item.ItemId).FirstOrDefault(); if (basketItem == null) { basketItem = Db.BasketItemCreate(user.UserId, item.ItemId, "", item.Quantity); } else { basketItem.Quantity += item.Quantity; } if (basketItem.Quantity <= 0) { Db.Context.UserBasketItems.Remove(basketItem); } Db.SaveChanges(); return(GetUserBasketModel(user)); }
public async Task <PSPMessage> ClearCustomerBasketAsync(BasketItemModel basketItemModel) { var response = await client.PostAsJsonAsync <BasketItemModel>("api/v1/clearCustomerBasket", basketItemModel); var result = response.Content.ReadAsStringAsync().Result; return(JsonConvert.DeserializeObject <PSPMessage>(result)); }
public async Task Put([FromBody] BasketItemModel basketItemModel) { //Assumption: We will call the Product Api to get the product //pricing and other details, So that user cannot tamper the price var addItemToBasketCommand = _autoMapper.Map <BasketItemModel, AddItemToBasketCommand>(basketItemModel); await _commandBus.SendAsync(addItemToBasketCommand); }
public async Task SaveOrUpdate(BasketItemModel basketModel) { DataStore.RemoveAll(x => x.ProductId == basketModel.ProductId && x.SessionId == basketModel.SessionId); if (basketModel.Quantity != 0) { DataStore.Add(basketModel); } }
public ActionResult BasketUpdateItem(BasketItemModel basketItem) { this.ServiceResponse = basketService.UpdateBasketItem(this.CurrentUser, basketItem); ProcessServiceResponse(this.ServiceResponse); return(Basket()); }
public void TestAddNewItemToBasket() { BasketItemModel baskteItem = new BasketItemModel(_apple); BasketModel basket = new BasketModel(); basket.AddOrUpdateBasket(baskteItem); Assert.AreEqual(1, basket.Basket.Count); Assert.AreEqual(1, basket.Total); }
/// <summary> /// Add the <paramref name="itemModel"/> to the basket. /// </summary> /// <param name="itemModel">The <see cref="BasketItemModel"/> to add to the basket.</param> /// <returns>The updated <see cref="BasketModel"/></returns> public BasketModel AddBasketItem(BasketItemModel itemModel) { if (itemModel == null) throw new ArgumentNullException(nameof(itemModel)); _basket.BasketItems.Add(itemModel); return _basket; }
public async Task <IActionResult> AddBasketItem([FromBody] BasketItemModel basketItemModel) { var basketItem = new BasketItemDTO(); basketItem.InjectFrom(basketItemModel); var result = await _redisBasketService.AddItemToBasket(basketItem); return(Ok(result)); }
public async Task SetItem(Guid basketId, Guid productId, int quantity) { var model = new BasketItemModel { ProductId = productId, Quantity = quantity }; var result = await client.PutAsJsonAsync($"api/basket/{basketId}/item/{productId}", model); CheckSuccess(result); }
public ActionResult BasketRemoveItem(BasketItemModel basketItem) { basketItem.Quantity = 0; this.ServiceResponse = basketService.RemoveItem(this.CurrentUser, basketItem); ProcessServiceResponse(this.ServiceResponse); return(Basket()); }
public void RemoveFromBasket(HttpContextBase httpContext, string itemId) { BasketModel basket = GetBasket(httpContext, true); BasketItemModel item = basket.BasketItems.FirstOrDefault(i => i.Id == itemId); if (item != null) { basket.BasketItems.Remove(item); basketContext.Commit(); } }
public void BasketItemRemove(UserSessionModel user, BasketItemModel item) { var entity = this.UserBasketItems.Where(u => u.UserId == user.UserId && u.ItemId == item.ItemId).FirstOrDefault(); if (entity != null) { this.Context.UserBasketItems.Remove(entity); } return; }
public void TestBasketSubtractFromTotalLessThanZero() { BasketItemModel basketItemApple = new BasketItemModel(_apple); BasketModel basket = new BasketModel(); basket.AddOrUpdateBasket(basketItemApple); basket.AddOrUpdateBasket(basketItemApple); Assert.AreEqual(2, basket.Total); basket.SubtractFromTotal(3); Assert.AreEqual(0, basket.Total); }
public void AddBasketItem(BasketItemModel basketItemModel) { var basketItem = Mapper.Map <BasketItem>(basketItemModel); if (basketItemModel.Game != null) { basketItem.Game = _unitOfWork.GameRepository.GetById(basketItemModel.GameId); } _unitOfWork.BasketItemRepository.Insert(basketItem); _unitOfWork.SaveChanges(); }
public void TestAddThreeUniqueItemsToBasket() { BasketItemModel basketItemApple = new BasketItemModel(_apple); BasketItemModel basketItemBanana = new BasketItemModel(_banana); BasketItemModel basketItemOrange = new BasketItemModel(_orange); BasketModel basket = new BasketModel(); basket.AddOrUpdateBasket(basketItemApple); basket.AddOrUpdateBasket(basketItemBanana); basket.AddOrUpdateBasket(basketItemOrange); Assert.AreEqual(3, basket.Basket.Count); Assert.AreEqual(6, basket.Total); }
private static BasketItemModel GetBasketItemModel() { var basketItemModel = new BasketItemModel { BasketItemId = 1, GameId = 1, BasketId = 1, Price = 5, Quantity = 10, }; return(basketItemModel); }
public void MapTest() { // Arrange var source = new BasketItemModel { Id = 1 }; // Act var destination = Mapper.Map <BasketItem>(source); // Assert Assert.AreEqual(1, destination.Id); }
public void UpdateBasketItem(BasketItemModel basketItemModel) { BasketItem basketItem = _unitOfWork.BasketItemRepository.GetById(basketItemModel.BasketItemId); Mapper.Map(basketItemModel, basketItem); if (basketItemModel.Game != null) { basketItem.Game = _unitOfWork.GameRepository.GetById(basketItemModel.GameId); } _unitOfWork.BasketItemRepository.Update(basketItem); _unitOfWork.SaveChanges(); }
public IHttpActionResult AddItemToBasket(BasketItemModel item) { try { var sessionBasket = BasketInMemorySorage.GetBasket(item.BasketId); var basket = MapBasketFromModel(sessionBasket); basket.AddItem(MapBasketItemModel(item)); sessionBasket = BasketInMemorySorage.UpdateBasket(item.BasketId, sessionBasket); return(Ok(sessionBasket)); } catch (Exception e) { return(InternalServerError(new Exception("there was an error mapping the basket item"))); } }
public void TestApplyingMultiplePromotionsToMultipleBasketItems() { List <PromotionModel> promotions = new List <PromotionModel> { _appleDiscount, _orangeBuyOneGetOneHalfOff, _bananaGroupDiscountOnThree }; BasketItemModel orangeBasketItem = new BasketItemModel(_orange); BasketItemModel appleBasketItem = new BasketItemModel(_apple); BasketItemModel bananaBasketItem = new BasketItemModel(_banana); BasketModel basket = new BasketModel(); basket.AddOrUpdateBasket(appleBasketItem); basket.AddOrUpdateBasket(orangeBasketItem); basket.AddOrUpdateBasket(bananaBasketItem); basket.AddOrUpdateBasket(orangeBasketItem); basket.AddOrUpdateBasket(bananaBasketItem); basket.AddOrUpdateBasket(bananaBasketItem); basket.AddOrUpdateBasket(appleBasketItem); IPromotionService promotionService = new PromotionService(); promotionService.ApplyPromotionsToBasket(basket, promotions); Assert.AreEqual(3, basket.Basket.Count); Assert.AreEqual(7, basket.Total); Assert.IsTrue(basket.Basket[0].Discount != null); Assert.AreEqual("Apple", basket.Basket[0].ItemName); Assert.AreEqual(2, basket.Basket[0].ItemQuantity); Assert.AreEqual(1, basket.Basket[0].Discount.ThresholdQuantity); Assert.AreEqual(.5M, basket.Basket[0].Discount.DiscountAmount); Assert.AreEqual(1, basket.Basket[0].Discount.TotalDiscountAmount); Assert.IsTrue(basket.Basket[1].Discount != null); Assert.AreEqual("Orange", basket.Basket[1].ItemName); Assert.AreEqual(2, basket.Basket[1].ItemQuantity); Assert.AreEqual(2, basket.Basket[1].Discount.ThresholdQuantity); Assert.AreEqual(1, basket.Basket[1].Discount.DiscountAmount); Assert.AreEqual(1, basket.Basket[1].Discount.TotalDiscountAmount); Assert.IsTrue(basket.Basket[2].Discount != null); Assert.AreEqual("Banana", basket.Basket[2].ItemName); Assert.AreEqual(3, basket.Basket[2].ItemQuantity); Assert.AreEqual(3, basket.Basket[2].Discount.ThresholdQuantity); Assert.AreEqual(1, basket.Basket[2].Discount.DiscountAmount); Assert.AreEqual(1, basket.Basket[2].Discount.TotalDiscountAmount); }
public void TestBaskteItemModelCreation() { ItemModel itemModel = new ItemModel { ItemName = "Apple", ItemId = 1, ItemPrice = 1 }; var basketItem = new BasketItemModel(itemModel); Assert.AreEqual(itemModel.ItemName, basketItem.ItemName); Assert.AreEqual(itemModel.ItemId, basketItem.ItemId); Assert.AreEqual(itemModel.ItemPrice, basketItem.ItemPrice); Assert.AreEqual(1, basketItem.ItemQuantity); Assert.IsTrue(basketItem.Discount == null); }
private static decimal ApplyDiscountToBasketItems(BasketItemModel basketItem, DiscountModel discount) { int basketQuantity = basketItem.ItemQuantity; if (basketQuantity >= discount.ThresholdQuantity) { basketItem.Discount = discount; while (basketQuantity >= discount.ThresholdQuantity) { discount.TotalDiscountAmount += discount.DiscountAmount; basketQuantity -= discount.ThresholdQuantity; } } return(discount.TotalDiscountAmount); }
public void UpdateQuantityWithZero() { BasketService basketService = new BasketService(); BasketModel basketModel = basketService.CreateNewBasket("C123"); BasketItemModel basketItemModel = new BasketItemModel(); basketItemModel.Id = "111"; basketItemModel.Name = "Car"; basketItemModel.Quantity = 1; basketItemModel.UnitPrice = (float)5.00; bool isAdded = basketService.AddBasketItem("C123", basketModel.Id, basketItemModel); bool isUpdated = basketService.UpdateBasketItemQuantity("C123", basketModel.Id, "111", 0); //Check if API does not update an item if quantity is 0, rather the item should be removed Assert.False(isUpdated); }
public void AddNewItemMissingMandatoryFields() { BasketService basketService = new BasketService(); BasketModel basketModel = basketService.CreateNewBasket("C123"); BasketItemModel basketItemModel = new BasketItemModel(); basketItemModel.Id = ""; basketItemModel.Name = ""; basketItemModel.Quantity = 0; basketItemModel.UnitPrice = -1; bool isAdded = basketService.AddBasketItem("C123", basketModel.Id, basketItemModel); //Check if API does not add new item if mandatory fields are missing Assert.False(isAdded); }
public void RemoveItem() { BasketService basketService = new BasketService(); BasketModel basketModel = basketService.CreateNewBasket("C123"); BasketItemModel basketItemModel = new BasketItemModel(); basketItemModel.Id = "111"; basketItemModel.Name = "Car"; basketItemModel.Quantity = 1; basketItemModel.UnitPrice = (float)5.00; bool isAdded = basketService.AddBasketItem("C123", basketModel.Id, basketItemModel); bool isRemoved = basketService.RemoveBasketItem("C123", basketModel.Id, "112"); //Check if API does not remove an item that does not exist in basket Assert.False(isRemoved); }