예제 #1
0
        public async Task <ActionResult <Order> > CreateOrder(
            [FromBody] OrderForCreation orderForCreation)
        {
            // model validation
            if (orderForCreation == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                // return 422 - Unprocessable Entity when validation fails
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            // map model to entity
            var orderEntity = _mapper.Map <Core.Entities.Order>(orderForCreation);

            // get product details
            foreach (var orderItem in orderEntity.Items)
            {
                var productDetail = await _productService.GetProductDetailAsync(orderItem.ProductId);

                if (productDetail == null)
                {
                    // return not found if no details were found
                    return(NotFound());
                }
                orderItem.Name  = productDetail.name;
                orderItem.Price = productDetail.price;
            }

            // add the entity to the repository
            _ordersRepository.AddOrder(orderEntity);

            // save the changes
            await _ordersRepository.SaveChangesAsync();

            // Fetch the order from the data store to include items
            await _ordersRepository.GetOrderAsync(orderEntity.Id);

            // return CreatedAtRoute to include location in header
            return(CreatedAtRoute("GetOrder",
                                  new { id = orderEntity.Id },
                                  _mapper.Map <Order>(orderEntity)));
        }
예제 #2
0
        public async Task <IActionResult> CreateOreder(string cartId,
                                                       OrderForCreation orderforCreationDto)
        {
            var mapOrder = _mapper.Map <Order> (orderforCreationDto);
            var getUser  = _repo.getUser(orderforCreationDto.UserId);

            mapOrder.User = await getUser;

            await _repo.CreateNewOrder(mapOrder);

            await _repo.SaveChanges();

            int orderId = mapOrder.OrderId;

            var getCartFormRepo = await _repo.GetCart(cartId);

            var getOrderFromRepo = await _repo.GetOrder(orderId);

            if (getOrderFromRepo != null)
            {
                var mapOrderDeta = _mapper.Map <ICollection <OrderDetail> > (getCartFormRepo.Items);

                foreach (var item in mapOrderDeta)
                {
                    item.OrderId = orderId;
                    getOrderFromRepo.OrderDetails.Add(item);
                }

                mapOrder.OrderDetails = getOrderFromRepo.OrderDetails;

                if (await _repo.SaveChanges())
                {
                    return(Ok(new {
                        orderId,
                    }));
                }
            }

            return(BadRequest());
        }