@RequestMapping(value="/categories", method=RequestMethod.POST) public String createCategory(@Valid @ModelAttribute("category") Category category, BindingResult result, Model model, RedirectAttributes redirectAttributes) { categoryValidator.validate(category, result); if(result.hasErrors()){ return viewPrefix+"create_category"; } Category persistedCategory = catalogService.createCategory(category); logger.debug("Created new category with id : {} and name : {}", persistedCategory.getId(), persistedCategory.getName()); redirectAttributes.addFlashAttribute("info", "Category created successfully"); return "redirect:/categories"; }
@RequestMapping(value="/categories/{id}", method=RequestMethod.POST) public String updateCategory(Category category, Model model, RedirectAttributes redirectAttributes) { Category persistedCategory = catalogService.updateCategory(category); logger.debug("Updated category with id : {} and name : {}", persistedCategory.getId(), persistedCategory.getName()); redirectAttributes.addFlashAttribute("info", "Category updated successfully"); return "redirect:/categories"; }
public Category updateCategory(Category category) { Category persistedCategory = getCategoryById(category.getId()); if(persistedCategory == null){ throw new JCartException("Category "+category.getId()+" doesn't exist"); } persistedCategory.setDescription(category.getDescription()); persistedCategory.setDisplayOrder(category.getDisplayOrder()); persistedCategory.setDisabled(category.isDisabled()); return categoryRepository.save(persistedCategory); }
public Product updateProduct(Product product) { Product persistedProduct = getProductById(product.getId()); if(persistedProduct == null){ throw new JCartException("Product "+product.getId()+" doesn't exist"); } persistedProduct.setDescription(product.getDescription()); persistedProduct.setDisabled(product.isDisabled()); persistedProduct.setPrice(product.getPrice()); persistedProduct.setCategory(getCategoryById(product.getCategory().getId())); return productRepository.save(persistedProduct); }
public static ProductForm fromProduct(Product product) { ProductForm p = new ProductForm(); p.setId(product.getId()); p.setName(product.getName()); p.setDescription(product.getDescription()); p.setDisabled(product.isDisabled()); p.setPrice(product.getPrice()); p.setSku(product.getSku()); p.setCategoryId(product.getCategory().getId()); //p.setImageUrl(WebUtils.IMAGES_PREFIX+product.getId()+".jpg"); return p; } }