public IActionResult CreateOrder([FromBody] OrderAddDTO order)
        {
            try
            {
                if (order == null)
                {
                    return(BadRequest("Order object is null"));
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }


                if (_repository.Product.CompareCant(order))
                {
                    return(BadRequest("Cannot create order. The cant of product is not enough."));
                }

                var orderEntity = _mapper.Map <Order>(order);
                orderEntity.UserId = new Guid(User.Identity.Name);

                _repository.Order.CreateOrder(orderEntity);
                _repository.Save();

                var createdOrder = _mapper.Map <OrderDTO>(orderEntity);

                return(CreatedAtRoute("OrderById", new { id = createdOrder.Id }, createdOrder));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, $"Something went wrong inside CreateOrder action: {ex.Message}"));
            }
        }
Exemplo n.º 2
0
        public JsonResult Delete([FromBody] OrderAddDTO body)
        {
            var newModel = model.Remove(new Order()
            {
                Id = body.Id,
                PersonResponsible = body.OrderPerson,
                CreateDate        = DateTime.UtcNow
            }).Entity;

            model.SaveChanges();
            return(Json(newModel));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Add([FromBody] OrderAddDTO addOrder)
        {
            try
            {
                var result = await _service.Add(addOrder);

                return(Ok(result));
            }
            catch (Exception ex)
            {
                return(Ok(ResponseResult.Failure <OrderDTO>(ex.Message)));
            }
        }
Exemplo n.º 4
0
        public int AddNewOrder([FromBody] OrderAddDTO body)
        {
            var order = model.Orders.Add(new Order()
            {
                PersonResponsible = body.OrderPerson,
                CreateDate        = DateTime.UtcNow
            }).Entity;

            model.SaveChanges();
            var listOfProducts = body.OrderProductIds
                                 .Select(p => new OrderProducts()
            {
                ProductId = p, Quantity = 0, OrderId = order.Id
            }).ToList();

            model.AddRange(listOfProducts);
            model.SaveChanges();
            return(order.Id);
        }
        public bool CompareCant(OrderAddDTO order)
        {
            var product = FindByCondition(product => product.Id.Equals(order.ProductId)).FirstOrDefault();

            return(product.Cant < order.Cant ? true : false);
        }