-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCategoryController.java
More file actions
84 lines (64 loc) · 2.54 KB
/
CategoryController.java
File metadata and controls
84 lines (64 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.mehmetpekdemir.librarymanagementsystem.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.mehmetpekdemir.librarymanagementsystem.entity.Category;
import com.mehmetpekdemir.librarymanagementsystem.service.CategoryService;
@Controller
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}
@RequestMapping("/categories")
public String findAllCategories(Model model) {
final List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories", categories);
return "list-categories";
}
@RequestMapping("/category/{id}")
public String findCategoryById(@PathVariable("id") Long id, Model model) {
final Category category = categoryService.findCategoryById(id);
model.addAttribute("category", category);
return "list-category";
}
@GetMapping("/addCategory")
public String showCreateForm(Category category) {
return "add-category";
}
@RequestMapping("/add-category")
public String createCategory(Category category, BindingResult result, Model model) {
if (result.hasErrors()) {
return "add-category";
}
categoryService.createCategory(category);
model.addAttribute("category", categoryService.findAllCategories());
return "redirect:/categories";
}
@GetMapping("/updateCategory/{id}")
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
final Category category = categoryService.findCategoryById(id);
model.addAttribute("category", category);
return "update-category";
}
@RequestMapping("/update-category/{id}")
public String updateCategory(@PathVariable("id") Long id, Category category, BindingResult result, Model model) {
if (result.hasErrors()) {
category.setId(id);
return "update-category";
}
categoryService.updateCategory(category);
model.addAttribute("category", categoryService.findAllCategories());
return "redirect:/categories";
}
@RequestMapping("/remove-category/{id}")
public String deleteCategory(@PathVariable("id") Long id, Model model) {
categoryService.deleteCategory(id);
model.addAttribute("category", categoryService.findAllCategories());
return "redirect:/categories";
}
}