public IActionResult GetShoppingCartItems(ShoppingCartItemsParametersModel 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 page parameter")); } IList <ShoppingCartItem> shoppingCartItems = _shoppingCartItemApiService.GetShoppingCartItems(customerId: null, createdAtMin: parameters.CreatedAtMin, createdAtMax: parameters.CreatedAtMax, updatedAtMin: parameters.UpdatedAtMin, updatedAtMax: parameters.UpdatedAtMax, limit: parameters.Limit, page: parameters.Page); var shoppingCartItemsDtos = shoppingCartItems.Select(shoppingCartItem => { return(_dtoHelper.PrepareShoppingCartItemDTO(shoppingCartItem)); }).ToList(); var shoppingCartsRootObject = new ShoppingCartItemsRootObject() { ShoppingCartItems = shoppingCartItemsDtos }; var json = JsonFieldsSerializer.Serialize(shoppingCartsRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public IActionResult UpdateShoppingCartItem([ModelBinder(typeof(JsonModelBinder <ShoppingCartItemDto>))] Delta <ShoppingCartItemDto> shoppingCartItemDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } // We kno that the id will be valid integer because the validation for this happens in the validator which is executed by the model binder. var shoppingCartItemForUpdate = _shoppingCartItemApiService.GetShoppingCartItem(shoppingCartItemDelta.Dto.Id); if (shoppingCartItemForUpdate == null) { return(Error(HttpStatusCode.NotFound, "shopping_cart_item", "not found")); } shoppingCartItemDelta.Merge(shoppingCartItemForUpdate); if (!shoppingCartItemForUpdate.Product.IsRental) { shoppingCartItemForUpdate.RentalStartDateUtc = null; shoppingCartItemForUpdate.RentalEndDateUtc = null; } if (shoppingCartItemDelta.Dto.Attributes != null) { shoppingCartItemForUpdate.AttributesXml = _productAttributeConverter.ConvertToXml(shoppingCartItemDelta.Dto.Attributes, shoppingCartItemForUpdate.Product.Id); } // The update time is set in the service. var warnings = _shoppingCartService.UpdateShoppingCartItem(shoppingCartItemForUpdate.Customer, shoppingCartItemForUpdate.Id, shoppingCartItemForUpdate.AttributesXml, shoppingCartItemForUpdate.CustomerEnteredPrice, shoppingCartItemForUpdate.RentalStartDateUtc, shoppingCartItemForUpdate.RentalEndDateUtc, shoppingCartItemForUpdate.Quantity); if (warnings.Count > 0) { foreach (var warning in warnings) { ModelState.AddModelError("shopping cart item", warning); } return(Error(HttpStatusCode.BadRequest)); } else { shoppingCartItemForUpdate = _shoppingCartItemApiService.GetShoppingCartItem(shoppingCartItemForUpdate.Id); } // Preparing the result dto of the new product category mapping var newShoppingCartItemDto = _dtoHelper.PrepareShoppingCartItemDTO(shoppingCartItemForUpdate); var shoppingCartsRootObject = new ShoppingCartItemsRootObject(); shoppingCartsRootObject.ShoppingCartItems.Add(newShoppingCartItemDto); var json = JsonFieldsSerializer.Serialize(shoppingCartsRootObject, string.Empty); return(new RawJsonActionResult(json)); }
public async Task <IActionResult> GetShoppingCartItems(ShoppingCartItemsParametersModel 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 page parameter")); } IList <ShoppingCartItem> shoppingCartItems = _shoppingCartItemApiService.GetShoppingCartItems(null, parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.UpdatedAtMin, parameters.UpdatedAtMax, parameters.Limit, parameters.Page); var shoppingCartItemsDtos = await shoppingCartItems.SelectAwait(async shoppingCartItem => await _dtoHelper.PrepareShoppingCartItemDTOAsync(shoppingCartItem)).ToListAsync(); var shoppingCartsRootObject = new ShoppingCartItemsRootObject { ShoppingCartItems = shoppingCartItemsDtos }; var json = JsonFieldsSerializer.Serialize(shoppingCartsRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public IHttpActionResult UpdateShoppingCartItem([ModelBinder(typeof(JsonModelBinder <ShoppingCartItemDto>))] Delta <ShoppingCartItemDto> shoppingCartItemDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } // We kno that the id will be valid integer because the validation for this happens in the validator which is executed by the model binder. ShoppingCartItem shoppingCartItemForUpdate = _shoppingCartItemApiService.GetShoppingCartItem(int.Parse(shoppingCartItemDelta.Dto.Id)); if (shoppingCartItemForUpdate == null) { return(Error(HttpStatusCode.NotFound, "shopping_cart_item", "not found")); } // Here we make sure that the product id and the customer id won't be modified. int productId = shoppingCartItemForUpdate.ProductId; int customerId = shoppingCartItemForUpdate.CustomerId; shoppingCartItemDelta.Merge(shoppingCartItemForUpdate); shoppingCartItemForUpdate.ProductId = productId; shoppingCartItemForUpdate.CustomerId = customerId; if (!shoppingCartItemForUpdate.Product.IsRental) { shoppingCartItemForUpdate.RentalStartDateUtc = null; shoppingCartItemForUpdate.RentalEndDateUtc = null; } if (!string.IsNullOrEmpty(shoppingCartItemDelta.Dto.ShoppingCartType)) { ShoppingCartType shoppingCartType = (ShoppingCartType)Enum.Parse(typeof(ShoppingCartType), shoppingCartItemDelta.Dto.ShoppingCartType); shoppingCartItemForUpdate.ShoppingCartType = shoppingCartType; } // The update time is set in the service. _shoppingCartService.UpdateShoppingCartItem(shoppingCartItemForUpdate.Customer, shoppingCartItemForUpdate.Id, shoppingCartItemForUpdate.AttributesXml, shoppingCartItemForUpdate.CustomerEnteredPrice, shoppingCartItemForUpdate.RentalStartDateUtc, shoppingCartItemForUpdate.RentalEndDateUtc, shoppingCartItemForUpdate.Quantity); // Preparing the result dto of the new product category mapping ShoppingCartItemDto newShoppingCartItemDto = shoppingCartItemForUpdate.ToDto(); newShoppingCartItemDto.ProductDto = shoppingCartItemForUpdate.Product.ToDto(); newShoppingCartItemDto.CustomerDto = shoppingCartItemForUpdate.Customer.ToCustomerForShoppingCartItemDto(); newShoppingCartItemDto.ShoppingCartType = shoppingCartItemForUpdate.ShoppingCartType.ToString(); var shoppingCartsRootObject = new ShoppingCartItemsRootObject(); shoppingCartsRootObject.ShoppingCartItems.Add(newShoppingCartItemDto); var json = _jsonFieldsSerializer.Serialize(shoppingCartsRootObject, string.Empty); return(new RawJsonActionResult(json)); }
public IHttpActionResult GetShoppingCartItemsByCustomerId(int customerId, ShoppingCartItemsForCustomerParametersModel parameters) { if (customerId <= Configurations.DefaultCustomerId) { return(Error(HttpStatusCode.BadRequest, "customer_id", "invalid customer_id")); } 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 page parameter")); } IList <ShoppingCartItem> shoppingCartItems = _shoppingCartItemApiService.GetShoppingCartItems(customerId, parameters.CreatedAtMin, parameters.CreatedAtMax, parameters.UpdatedAtMin, parameters.UpdatedAtMax, parameters.Limit, parameters.Page); if (shoppingCartItems == null) { return(Error(HttpStatusCode.NotFound, "shopping_cart_item", "not found")); } List <ShoppingCartItemDto> shoppingCartItemsDtos = shoppingCartItems.Select(shoppingCartItem => { return(_dtoHelper.PrepareShoppingCartItemDTO(shoppingCartItem)); }).ToList(); var shoppingCartsRootObject = new ShoppingCartItemsRootObject() { ShoppingCartItems = shoppingCartItemsDtos }; var json = _jsonFieldsSerializer.Serialize(shoppingCartsRootObject, parameters.Fields); return(new RawJsonActionResult(json)); }
public IHttpActionResult CreateShoppingCartItem([ModelBinder(typeof (JsonModelBinder<ShoppingCartItemDto>))] Delta<ShoppingCartItemDto> shoppingCartItemDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return Error(); } ShoppingCartItem newShoppingCartItem = _factory.Initialize(); shoppingCartItemDelta.Merge(newShoppingCartItem); // We know that the product id and customer id will be provided because they are required by the validator. // TODO: validate Product product = _productService.GetProductById(shoppingCartItemDelta.Dto.ProductId.Value); if (product == null) { return Error(HttpStatusCode.NotFound, "product", "not found"); } Customer customer = _customerService.GetCustomerById(shoppingCartItemDelta.Dto.CustomerId.Value); if (customer == null) { return Error(HttpStatusCode.NotFound, "customer", "not found"); } ShoppingCartType shoppingCartType = (ShoppingCartType)Enum.Parse(typeof(ShoppingCartType), shoppingCartItemDelta.Dto.ShoppingCartType); if (!product.IsRental) { newShoppingCartItem.RentalStartDateUtc = null; newShoppingCartItem.RentalEndDateUtc = null; } IList<string> warnings = _shoppingCartService.AddToCart(customer, product, shoppingCartType, 0, null, 0M, shoppingCartItemDelta.Dto.RentalStartDateUtc, shoppingCartItemDelta.Dto.RentalEndDateUtc, shoppingCartItemDelta.Dto.Quantity ?? 1); if (warnings.Count > 0) { foreach (var warning in warnings) { ModelState.AddModelError("shopping cart item", warning); } return Error(HttpStatusCode.BadRequest); } // Preparing the result dto of the new product category mapping ShoppingCartItemDto newShoppingCartItemDto = newShoppingCartItem.ToDto(); newShoppingCartItemDto.ProductDto = product.ToDto(); newShoppingCartItemDto.CustomerDto = customer.ToCustomerForShoppingCartItemDto(); newShoppingCartItemDto.ShoppingCartType = shoppingCartType.ToString(); var shoppingCartsRootObject = new ShoppingCartItemsRootObject(); shoppingCartsRootObject.ShoppingCartItems.Add(newShoppingCartItemDto); var json = _jsonFieldsSerializer.Serialize(shoppingCartsRootObject, string.Empty); return new RawJsonActionResult(json); }
public IActionResult CreateShoppingCartItem([ModelBinder(typeof(JsonModelBinder <ShoppingCartItemDto>))] Delta <ShoppingCartItemDto> shoppingCartItemDelta) { // Here we display the errors if the validation has failed at some point. if (!ModelState.IsValid) { return(Error()); } ShoppingCartItem newShoppingCartItem = _factory.Initialize(); shoppingCartItemDelta.Merge(newShoppingCartItem); // We know that the product id and customer id will be provided because they are required by the validator. // TODO: validate Product product = _productService.GetProductById(newShoppingCartItem.ProductId); if (product == null) { return(Error(HttpStatusCode.NotFound, "product", "not found")); } Customer customer = _customerService.GetCustomerById(newShoppingCartItem.CustomerId); if (customer == null) { return(Error(HttpStatusCode.NotFound, "customer", "not found")); } ShoppingCartType shoppingCartType = (ShoppingCartType)Enum.Parse(typeof(ShoppingCartType), shoppingCartItemDelta.Dto.ShoppingCartType); if (!product.IsRental) { newShoppingCartItem.RentalStartDateUtc = null; newShoppingCartItem.RentalEndDateUtc = null; } string attributesXml = _productAttributeConverter.ConvertToXml(shoppingCartItemDelta.Dto.Attributes, product.Id); int currentStoreId = _storeContext.CurrentStore.Id; IList <string> warnings = _shoppingCartService.AddToCart(customer, product, shoppingCartType, currentStoreId, attributesXml, 0M, newShoppingCartItem.RentalStartDateUtc, newShoppingCartItem.RentalEndDateUtc, shoppingCartItemDelta.Dto.Quantity ?? 1); if (warnings.Count > 0) { foreach (var warning in warnings) { ModelState.AddModelError("shopping cart item", warning); } return(Error(HttpStatusCode.BadRequest)); } else { // the newly added shopping cart item should be the last one newShoppingCartItem = customer.ShoppingCartItems.LastOrDefault(); } // Preparing the result dto of the new product category mapping ShoppingCartItemDto newShoppingCartItemDto = _dtoHelper.PrepareShoppingCartItemDTO(newShoppingCartItem); var shoppingCartsRootObject = new ShoppingCartItemsRootObject(); shoppingCartsRootObject.ShoppingCartItems.Add(newShoppingCartItemDto); var json = _jsonFieldsSerializer.Serialize(shoppingCartsRootObject, string.Empty); return(new RawJsonActionResult(json)); }