Esempio n. 1
0
        public async Task <ActionResult <OrderModel> > Get(string orderNumber)
        {
            try
            {
                var result = await _repository.GetOrderAsync(orderNumber);

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

                return(_mapper.Map <OrderModel>(result));
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Database Failure"));
            }
        }
        public async Task <ActionResult <OrderItemModel> > Post(string orderNumber, OrderItemModel model)
        {
            try
            {
                var order = await _repository.GetOrderAsync(orderNumber);

                if (order == null)
                {
                    return(BadRequest("Order does not exist"));
                }

                var orderItem = _mapper.Map <OrderItem>(model);
                orderItem.Order = order;

                if (model.Book == null)
                {
                    return(BadRequest("Book ID is required"));
                }

                var book = await _repository.GetBookAsync(model.Book.BookId);

                if (book == null)
                {
                    return(BadRequest("Book could not be found"));
                }
                orderItem.Book = book;

                _repository.Add(orderItem);

                if (await _repository.SaveChangesAsync())
                {
                    var url = _linkGenerator.GetPathByAction(HttpContext, "Get", values: new { orderNumber, id = orderItem.OrderItemId });

                    return(Created(url, _mapper.Map <OrderItemModel>(orderItem)));
                }
                else
                {
                    return(BadRequest("Failed to save new orderItem"));
                }
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to post OrderItem"));
            }
        }