public async Task Execution_should_fail_when_order_is_associated_with_items_that_have_been_shipped_state_based_async() { var order = new Order() { OrderDate = DateTime.Now }; var orderRepo = new OrderRepository(Mock.Of <ICustomerDataProxy>(), Mock.Of <IOrderItemDataProxy>()); orderRepo.Clear(); order = await orderRepo.InsertAsync(order); var orderItemRepo = new OrderItemRepository(); orderItemRepo.Clear(); await orderItemRepo.InsertAsync(new OrderItem() { OrderID = order.ID, OrderStatusID = OrderStatusConstants.PENDING_STATUS }); 2.Times(async() => await orderItemRepo.InsertAsync(new OrderItem() { OrderID = order.ID, OrderStatusID = OrderStatusConstants.SHIPPED_STATUS })); var orderItemService = new OrderItemService(orderItemRepo, Mock.Of <IProductDataProxy>(), Mock.Of <IInventoryItemDataProxy>(), Mock.Of <ITransactionContext>()); var command = new DeleteOrderCommand(order.ID, orderRepo, orderItemService, new TransactionContextStub()); var result = await command.ExecuteAsync(); result.Success.ShouldBe(false); result.Errors.Count().ShouldBe(2); }
public async Task Execution_deletes_order_and_associated_order_items_state_based_async() { var order = new Order() { OrderDate = DateTime.Now }; var orderRepo = new OrderRepository(Mock.Of <ICustomerDataProxy>(), Mock.Of <IOrderItemDataProxy>()); orderRepo.Clear(); order = orderRepo.Insert(order); var orderItemRepo = new OrderItemRepository(); orderItemRepo.Clear(); await orderItemRepo.InsertAsync(new OrderItem { OrderID = order.ID, OrderStatusID = OrderStatusConstants.PENDING_STATUS }); await orderItemRepo.InsertAsync(new OrderItem { OrderID = order.ID, OrderStatusID = OrderStatusConstants.SUBMITTED_STATUS }); await orderItemRepo.InsertAsync(new OrderItem { OrderID = order.ID, OrderStatusID = OrderStatusConstants.BACK_ORDERED_STATE }); await orderItemRepo.InsertAsync(new OrderItem { OrderID = 2, OrderStatusID = OrderStatusConstants.PENDING_STATUS }); var orderItemService = new OrderItemService(orderItemRepo, Mock.Of <IProductDataProxy>(), Mock.Of <IInventoryItemDataProxy>(), new TransactionContextStub()); var command = new DeleteOrderCommand(order.ID, orderRepo, orderItemService, new TransactionContextStub()); await command.ExecuteAsync(); orderRepo.GetAll().ShouldBeEmpty(); orderItemRepo.GetAll().Count().ShouldBe(1); }
public virtual async Task <OrderDto> CreateAsync(OrderCreateDto dto) { var userId = CurrentUser.Id; var addressE = (await AsyncExecuter .SingleOrDefaultAsync(from addrR in AddressRepository join cusR in CustomerRepository on addrR.CustomerID equals cusR.Id where cusR.UserID == userId && addrR.Id == dto.AddressID select addrR)) ?? throw new BusinessException(); var itemIds = dto.Items.Select(e => e.ItemID).ToArray(); var dsEArr = await DeliveryScheduleAppService.GetAllCompatiblesAsync(addressE.Id, itemIds); var dsE = dsEArr.SingleOrDefault(e => e.Id == dto.DeliveryScheduleID) ?? throw new BusinessException(); var promotionE = (dto.PromotionID != null) ? await GetPromotionEntity((int)dto.PromotionID) : null; var subtotal = 0.0; var orderItemEList = new List <OrderItemEntity>(); if (!(await ItemRepository.ValidateItemsWithinRadiusAsync(addressE.CityID, itemIds))) { throw new BusinessException(); } foreach (var cartItem in dto.Items) { var item = await ItemRepository.SingleOrDefaultAsync(e => e.Id == cartItem.ItemID) ?? throw new BusinessException(); subtotal += item.ItemPrice * cartItem.Qty; orderItemEList.Add(new OrderItemEntity(0, item.Id, (uint)cartItem.Qty, item.ItemPrice)); } var dcDto = await DeliveryChargeAppService.CalculateAsync(addressE.Id, dsE.Id, dto.Items.ToArray()); var orderE = new OrderEntity(addressE.Id, dsE.Id, promotionE?.Id); orderE = await OrderRepository.InsertAsync(orderE, autoSave : true); foreach (var oi in orderItemEList) { var item = new OrderItemEntity(orderE.Id, oi.ItemID, oi.Quantity, oi.ItemPrice); await OrderItemRepository.InsertAsync(item, autoSave : true); } var dCharge = dcDto.DistanceCharge + dcDto.SubtotalPercentage + dcDto.IncreasedCost; if (promotionE != null) { dCharge = dcDto.SubtotalPercentage; if (promotionE.IsOneTime || promotionE.NoOfTimes - 1 < 1) { await PromotionRepository.DeleteAsync(promotionE); } } var dcE = new DeliveryChargeEntity(dCharge, dcDto.DistanceChargeID, dcDto.SubtotalPercentageID, dcDto.DeliveryScheduleID); await DeliveryChargeRepository.InsertAsync(dcE, autoSave : true); var paymentE = new PaymentEntity(orderE.Id, dcE.Id, (float)(subtotal + dcE.Charge), (float)subtotal); await PaymentRepository.InsertAsync(paymentE, autoSave : true); return(ObjectMapper.Map <OrderEntity, OrderDto>(orderE)); }