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); }
@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"; }
@RequestMapping(value="/categories/new", method=RequestMethod.GET) public String createCategoryForm(Model model) { Category category = new Category(); model.addAttribute("category",category); return viewPrefix+"create_category"; }
@RequestMapping("/home") public String home(Model model) { List<Category> previewCategories = new ArrayList<>(); List<Category> categories = catalogService.getAllCategories(); for (Category category : categories) { Set<Product> products = category.getProducts(); Set<Product> previewProducts = new HashSet<>(); int noOfProductsToDisplay = 4; if(products.size() > noOfProductsToDisplay){ Iterator<Product> iterator = products.iterator(); for (int i = 0; i < noOfProductsToDisplay; i++) { previewProducts.add(iterator.next()); } } else { previewProducts.addAll(products); } category.setProducts(previewProducts); previewCategories.add(category); } model.addAttribute("categories", previewCategories); return "home"; }
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); }
@Override public void validate(Object target, Errors errors) { Category category = (Category) target; String name = category.getName(); Category categoryByName = catalogService.getCategoryByName(name); if(categoryByName != null){ errors.rejectValue("name", "error.exists", new Object[]{name}, "Category "+category.getName()+" already exists"); } }
public Product toProduct() { Product p = new Product(); p.setId(id); p.setName(name); p.setDescription(description); p.setDisabled(disabled); p.setPrice(price); p.setSku(sku); Category category = new Category(); category.setId(categoryId); p.setCategory(category ); //p.setImageUrl(WebUtils.IMAGES_PREFIX+id+".jpg"); return p; }
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; } }
@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"; }