public OrderProductResponse OrderProduct([FromBody]OrderProductRequest request)
        {
            //TODO: implement scenario ไม่มีข้อมูล หรือไม่เลือกสินค้าที่จะสั่ง ให้แจ้งกลับว่า 'ไม่พบเมนูที่จะสั่ง' และไม่บันทึกข้อมูล   
            if (request == null || request.OrderedProducts == null || !request.OrderedProducts.Any())
            {
                return new OrderProductResponse { Message = "ไม่พบเมนูที่จะสั่ง", };
            }

            var productIds = request.OrderedProducts.Select(p => p.Key);            
            var products = productRepo.GetAllProducts();
            var filteredProducts = products.Where(p => productIds.Contains(p.Id)).ToList();
        
            if (filteredProducts.Count() != productIds.Count())
            {
                return new OrderProductResponse { Message = "ไม่พบสินค้าบางรายการ กรุณาสั่งใหม่อีกครั้ง", };
            }

            if (filteredProducts.Any(p => p.HasStock && p.Stock < request.OrderedProducts.First(op => op.Key == p.Id).Value))
            {
                return new OrderProductResponse { Message = "สินค้าบางรายการมีไม่พอ กรุณาสั่งใหม่อีกครั้ง", };
            }
            var id = Guid.NewGuid().ToString();
            var order = new Order
            {
                Id = id,
                OrderedProducts = filteredProducts,
                ReferenceCode = id.Substring(0, 5),
                OrderDate = DateTime.UtcNow,
                Username = request.Username,
            };
            orderRepo.Create(order);
            
            return new OrderProductResponse { Message = "สั่งเมนูสำเร็จ กรุณาชำระเงินที่เค้าเตอร์", ReferenceCode = order.ReferenceCode, };
        }
        public void OrderNotexistingProduct(OrderProductRequest data, string expectedMessage)
        {
            var mock        = new MockRepository(MockBehavior.Default);
            var repoProduct = mock.Create <IProductRepository>();
            var repoOrder   = mock.Create <IOrderRepository>();
            var api         = new OrderController(repoProduct.Object, repoOrder.Object);

            repoProduct.Setup(it => it.GetAllProducts()).Returns(() => new List <Product>
            {
                new Product {
                    Id = 1, Stock = 0, HasStock = true
                },
                new Product {
                    Id = 2, Stock = 5, HasStock = true
                },
                new Product {
                    Id = 3, Stock = 0, HasStock = true
                },
            });
            repoOrder.Setup(it => it.Create(It.IsAny <Order>()));

            var response = api.OrderProduct(data);

            repoProduct.Verify(dac => dac.GetAllProducts(), Times.Once);
            repoProduct.VerifyNoOtherCalls();
            repoOrder.VerifyNoOtherCalls();
            response.ReferenceCode.Should().BeNullOrEmpty();
            response.Message.Should().Be(expectedMessage);
        }
示例#3
0
        public OrderProductResponse OrderProduct([FromBody] OrderProductRequest request)
        {
            if (request == null || request.OrderedProducts == null)
            {
                return new OrderProductResponse {
                           Message = "ไม่พบเมนูที่จะสั่ง"
                }
            }
            ;
            var hasOrder    = request.OrderedProducts.Any();
            var productIds  = request.OrderedProducts.Select(p => p.Key);
            var products    = productRepo.GetAllProducts();
            var hasProducts = products.Any();
            var canOrder    = hasOrder || hasProducts;

            if (canOrder)
            {
                var filteredProducts = products.Where(p => productIds.Contains(p.Id)).ToList();

                if (filteredProducts.Count() != productIds.Count())
                {
                    return(new OrderProductResponse {
                        Message = "ไม่พบสินค้าบางรายการ กรุณาสั่งใหม่อีกครั้ง",
                    });
                }

                if (filteredProducts.Any(p => p.HasStock && p.Stock < request.OrderedProducts.First(op => op.Key == p.Id).Value))
                {
                    return(new OrderProductResponse {
                        Message = "สินค้าบางรายการมีไม่พอ กรุณาสั่งใหม่อีกครั้ง",
                    });
                }
                var id    = Guid.NewGuid().ToString();
                var order = new Order
                {
                    Id = id,
                    OrderedProducts = filteredProducts,
                    ReferenceCode   = id.Substring(0, 5),
                    OrderDate       = DateTime.UtcNow,
                    Username        = request.Username,
                };
                orderRepo.Create(order);

                return(new OrderProductResponse {
                    Message = "สั่งเมนูสำเร็จ กรุณาชำระเงินที่เค้าเตอร์", ReferenceCode = order.ReferenceCode,
                });
            }
            else
            {
                return(new OrderProductResponse {
                    Message = "ไม่พบเมนูที่จะสั่ง"
                });
            }
        }
    }
        public async Task SubmitOrderReturnsOrderFromApi()
        {
            var repo          = ResolveType <OrderRepository>();
            var orderProducts = new OrderProductRequest[]
            {
                new OrderProductRequest
                {
                    Id       = 1,
                    Quantity = 1
                }
            };

            var order = await repo.SubmitOrder(orderProducts);

            Assert.NotNull(order);
        }
        public async Task SubmitOrderCallsOrderServiceSubmitOrderOnce()
        {
            var repo          = ResolveType <OrderRepository>();
            var orderProducts = new OrderProductRequest[]
            {
                new OrderProductRequest
                {
                    Id       = 1,
                    Quantity = 1
                }
            };

            var order = await repo.SubmitOrder(orderProducts);

            GetMock <IOrderService>().Verify(s => s.SubmitOrder(It.IsAny <OrderProductRequestDto[]>()), Times.Once);
        }
        public void OrderNoneProduct(OrderProductRequest data, string expectedMessage)
        {
            var mock        = new MockRepository(MockBehavior.Default);
            var repoProduct = mock.Create <IProductRepository>();
            var repoOrder   = mock.Create <IOrderRepository>();
            var api         = new OrderController(repoProduct.Object, repoOrder.Object);

            repoOrder.Setup(it => it.Create(It.IsAny <Order>()));

            var response = api.OrderProduct(data);

            repoProduct.VerifyNoOtherCalls();
            repoOrder.VerifyNoOtherCalls();
            response.ReferenceCode.Should().BeNullOrEmpty();
            response.Message.Should().Be(expectedMessage);
        }
        public void OrderProductsSuccess(OrderProductRequest data, string expectedMessage)
        {
            var mock        = new MockRepository(MockBehavior.Default);
            var repoProduct = mock.Create <IProductRepository>();
            var repoOrder   = mock.Create <IOrderRepository>();
            var api         = new OrderController(repoProduct.Object, repoOrder.Object);

            repoProduct.Setup(it => it.GetAllProducts()).Returns(() => new List <Product>
            {
                new Product {
                    Id = 1, Stock = 4, HasStock = true
                },
                new Product {
                    Id = 2, Stock = 5, HasStock = true
                },
                new Product {
                    Id = 3, Stock = 6, HasStock = true
                },
                new Product {
                    Id = 4, Stock = 0, HasStock = false
                },
            });
            repoOrder.Setup(it => it.Create(It.IsAny <Order>()));

            var response = api.OrderProduct(data);

            repoProduct.Verify(dac => dac.GetAllProducts(), Times.Once);
            repoProduct.VerifyNoOtherCalls();
            Func <Order, bool> func = o =>
            {
                o.Id.Should().NotBeNullOrEmpty();
                o.Username.Should().NotBeNullOrEmpty();
                o.OrderedProducts.Any();
                o.ReferenceCode.Should().NotBeNullOrEmpty();
                return(true);
            };

            repoOrder.Verify(dac => dac.Create(It.Is <Order>(o => func(o))), Times.Once);
            repoOrder.VerifyNoOtherCalls();
            response.ReferenceCode.Should().NotBeNull();
            response.Message.Should().Be(expectedMessage);
        }
示例#8
0
        public async Task <CommandResponse> Handle(OrderDto request, CancellationToken cancellationToken)
        {
            var productMap = _mapper.Map <List <Product> >(request.Products);

            productMap = CheckForProductInStock(productMap);

            var order = new Order();
            var createOrderRequest = _mapper.Map <CreateOrderRequest>(order);

            _orderPublishService.PublishOrder(createOrderRequest);

            foreach (var item in productMap)
            {
                var ProductOrder = new OrderProductRequest(order.Id, Guid.Parse(item.ProductId), item.Value, item.Quantity, item.ProductType);
                await _mediator.Send(ProductOrder);
            }

            var data = new CreateOrderResponse(createOrderRequest.Number, createOrderRequest.CreateDate, createOrderRequest.LastUpdateDate, request.Products, createOrderRequest.Total, createOrderRequest.Notes, createOrderRequest.Status);

            return(CreateResponse(data, "Order sent successfully"));
        }
        public async Task <IActionResult> RemoveProductAsync([FromRoute] Guid id, [FromBody] OrderProductRequest orderProductRequest)
        {
            await _orderService.RemoveProductAsync(id, orderProductRequest.ProductId, orderProductRequest.Quantity);

            return(Result());
        }