Beispiel #1
0
        public async Task <ActionResult <OrderAPIModel> > UpdateOrder(int id, OrderAPIModel order)
        {
            var validator        = new OrderValidator();
            var validationResult = await validator.ValidateAsync(order);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var orderToUpdate = await _orderService.GetOrderById(id);

            if (orderToUpdate == null)
            {
                return(NotFound());
            }

            var orderModel = _mapper.Map <OrderAPIModel, Orders>(order);

            await _orderService.UpdateOrder(orderToUpdate, orderModel);

            orderModel = await _orderService.GetOrderById(id);

            var orderAPIModel = _mapper.Map <Orders, OrderAPIModel>(orderModel);

            return(Ok(orderAPIModel));
        }
        public async Task <Guid> Handle(CreateProductOrderCommand request, CancellationToken cancellationToken)
        {
            var customer = await _context.Customers.FindAsync(request.CustomerId);

            var product = await _context.Products.FindAsync(request.ProductId);

            if (customer is null || product is null)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            var order = new Order
            {
                CustomerId  = customer.Id,
                OrderPlaced = DateTime.UtcNow,
            };

            await _context.Orders.AddAsync(order, cancellationToken);

            var orderValidator        = new OrderValidator();
            var orderValidationResult = await orderValidator.ValidateAsync(order, cancellationToken);

            if (!orderValidationResult.IsValid)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            var productOrder = new ProductOrder
            {
                Quantity  = request.Quantity,
                OrderId   = order.Id,
                ProductId = product.Id,
            };

            await _context.ProductOrders.AddAsync(productOrder, cancellationToken);

            var productOrderValidator        = new ProductOrderValidator();
            var productOrderValidationResult =
                await productOrderValidator.ValidateAsync(productOrder, cancellationToken);

            if (!productOrderValidationResult.IsValid)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(productOrder.Id));
        }
Beispiel #3
0
        public async Task <ActionResult <OrderAPIModel> > CreateOrder(OrderAPIModel order)
        {
            var validator        = new OrderValidator();
            var validationResult = await validator.ValidateAsync(order);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            var orderModel    = _mapper.Map <OrderAPIModel, Orders>(order);
            var orderAPIModel = await _orderService.CreateOrder(orderModel);

            return(Ok(orderAPIModel));
        }