Exemplo n.º 1
0
        public async Task <IActionResult> GetOrderItems(int orderId, OrderItemsParametersModel parameters)
        {
            if (parameters.Limit < Constants.Configurations.MinLimit || parameters.Limit > Constants.Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter"));
            }

            if (parameters.Page < Constants.Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters"));
            }

            var order = _orderApiService.GetOrderById(orderId);

            if (order == null)
            {
                return(Error(HttpStatusCode.NotFound, "order", "not found"));
            }

            var allOrderItemsForOrder = await _orderItemApiService.GetOrderItemsForOrderAsync(order, parameters.Limit, parameters.Page,
                                                                                              parameters.SinceId);

            var orderItemsRootObject = new OrderItemsRootObject
            {
                OrderItems = await allOrderItemsForOrder.SelectAwait(async item => await _dtoHelper.PrepareOrderItemDTOAsync(item)).ToListAsync()
            };

            var json = JsonFieldsSerializer.Serialize(orderItemsRootObject, parameters.Fields);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> GetOrderItemByIdForOrder(int orderId, int orderItemId, string fields = "")
        {
            var order = _orderApiService.GetOrderById(orderId);

            if (order == null)
            {
                return(Error(HttpStatusCode.NotFound, "order", "not found"));
            }

            var orderItem = await _orderService.GetOrderItemByIdAsync(orderItemId);

            if (orderItem == null)
            {
                return(Error(HttpStatusCode.NotFound, "order_item", "not found"));
            }

            var orderItemDtos = new List <OrderItemDto>
            {
                await _dtoHelper.PrepareOrderItemDTOAsync(orderItem)
            };

            var orderItemsRootObject = new OrderItemsRootObject
            {
                OrderItems = orderItemDtos
            };

            var json = JsonFieldsSerializer.Serialize(orderItemsRootObject, fields);

            return(new RawJsonActionResult(json));
        }
        public IHttpActionResult GetOrderItems(int orderId, OrderItemsParametersModel parameters)
        {
            if (parameters.Limit < Configurations.MinLimit || parameters.Limit > Configurations.MaxLimit)
            {
                return(Error(HttpStatusCode.BadRequest, "limit", "Invalid limit parameter"));
            }

            if (parameters.Page < Configurations.DefaultPageValue)
            {
                return(Error(HttpStatusCode.BadRequest, "page", "Invalid request parameters"));
            }

            Order order = _orderApiService.GetOrderById(orderId);

            if (order == null)
            {
                return(Error(HttpStatusCode.NotFound, "order", "not found"));
            }

            IList <OrderItem> allOrderItemsForOrder = _orderItemApiService.GetOrderItemsForOrder(order, parameters.Limit, parameters.Page, parameters.SinceId);

            var orderItemsRootObject = new OrderItemsRootObject()
            {
                OrderItems = allOrderItemsForOrder.Select(item => _dtoHelper.PrepareOrderItemDTO(item)).ToList()
            };

            var json = _jsonFieldsSerializer.Serialize(orderItemsRootObject, parameters.Fields);

            return(new RawJsonActionResult(json));
        }
        public IHttpActionResult GetOrderItemByIdForOrder(int orderId, int orderItemId, string fields = "")
        {
            Order order = _orderApiService.GetOrderById(orderId);

            if (order == null)
            {
                return(Error(HttpStatusCode.NotFound, "order", "not found"));
            }

            OrderItem orderItem = _orderService.GetOrderItemById(orderItemId);

            if (orderItem == null)
            {
                return(Error(HttpStatusCode.NotFound, "order_item", "not found"));
            }

            var orderItemDtos = new List <OrderItemDto>();

            orderItemDtos.Add(_dtoHelper.PrepareOrderItemDTO(orderItem));

            var orderItemsRootObject = new OrderItemsRootObject()
            {
                OrderItems = orderItemDtos
            };

            var json = _jsonFieldsSerializer.Serialize(orderItemsRootObject, fields);

            return(new RawJsonActionResult(json));
        }
        public IActionResult UpdateOrderItem(
            int orderId, int orderItemId,
            [ModelBinder(typeof(JsonModelBinder <OrderItemDto>))]
            Delta <OrderItemDto> orderItemDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            var orderItemToUpdate = _orderService.GetOrderItemById(orderItemId);

            if (orderItemToUpdate == null)
            {
                return(Error(HttpStatusCode.NotFound, "order_item", "not found"));
            }

            var order = _orderApiService.GetOrderById(orderId);

            if (order == null)
            {
                return(Error(HttpStatusCode.NotFound, "order", "not found"));
            }

            // This is needed because those fields shouldn't be updatable. That is why we save them and after the merge set them back.
            int?productId       = orderItemToUpdate.ProductId;
            var rentalStartDate = orderItemToUpdate.RentalStartDateUtc;
            var rentalEndDate   = orderItemToUpdate.RentalEndDateUtc;

            orderItemDelta.Merge(orderItemToUpdate);

            orderItemToUpdate.ProductId          = (int)productId;
            orderItemToUpdate.RentalStartDateUtc = rentalStartDate;
            orderItemToUpdate.RentalEndDateUtc   = rentalEndDate;

            _orderService.UpdateOrder(order);

            CustomerActivityService.InsertActivity("UpdateOrderItem",
                                                   LocalizationService.GetResource("ActivityLog.UpdateOrderItem"), orderItemToUpdate);

            var orderItemsRootObject = new OrderItemsRootObject();

            orderItemsRootObject.OrderItems.Add(orderItemToUpdate.ToDto());

            var json = JsonFieldsSerializer.Serialize(orderItemsRootObject, string.Empty);

            return(new RawJsonActionResult(json));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> CreateOrderItem(
            int orderId,
            [ModelBinder(typeof(JsonModelBinder <OrderItemDto>))]
            Delta <OrderItemDto> orderItemDelta)
        {
            // Here we display the errors if the validation has failed at some point.
            if (!ModelState.IsValid)
            {
                return(Error());
            }

            var order = _orderApiService.GetOrderById(orderId);

            if (order == null)
            {
                return(Error(HttpStatusCode.NotFound, "order", "not found"));
            }

            var product = GetProduct(orderItemDelta.Dto.ProductId);

            if (product == null)
            {
                return(Error(HttpStatusCode.NotFound, "product", "not found"));
            }

            if (product.IsRental)
            {
                if (orderItemDelta.Dto.RentalStartDateUtc == null)
                {
                    return(Error(HttpStatusCode.BadRequest, "rental_start_date_utc", "required"));
                }

                if (orderItemDelta.Dto.RentalEndDateUtc == null)
                {
                    return(Error(HttpStatusCode.BadRequest, "rental_end_date_utc", "required"));
                }

                if (orderItemDelta.Dto.RentalStartDateUtc > orderItemDelta.Dto.RentalEndDateUtc)
                {
                    return(Error(HttpStatusCode.BadRequest, "rental_start_date_utc",
                                 "should be before rental_end_date_utc"));
                }

                if (orderItemDelta.Dto.RentalStartDateUtc < DateTime.UtcNow)
                {
                    return(Error(HttpStatusCode.BadRequest, "rental_start_date_utc", "should be a future date"));
                }
            }

            var newOrderItem = await PrepareDefaultOrderItemFromProductAsync(order, product);

            orderItemDelta.Merge(newOrderItem);
            await _orderService.InsertOrderItemAsync(newOrderItem);

            await _orderService.UpdateOrderAsync(order);

            await CustomerActivityService.InsertActivityAsync("AddNewOrderItem", await LocalizationService.GetResourceAsync("ActivityLog.AddNewOrderItem"), newOrderItem);

            var orderItemsRootObject = new OrderItemsRootObject();

            orderItemsRootObject.OrderItems.Add(newOrderItem.ToDto());

            var json = JsonFieldsSerializer.Serialize(orderItemsRootObject, string.Empty);

            return(new RawJsonActionResult(json));
        }