33import java .util .List ;
44
55import org .springframework .beans .factory .annotation .Autowired ;
6- // import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
7- // import org.hibernate.mapping.List;
8- // import org.springframework.beans.factory.annotation.Autowired;
6+
97import org .springframework .stereotype .Controller ;
108import org .springframework .ui .Model ;
119import org .springframework .validation .BindingResult ;
1210import org .springframework .web .bind .annotation .ModelAttribute ;
1311import org .springframework .web .bind .annotation .PathVariable ;
1412import org .springframework .web .bind .annotation .PostMapping ;
15- // import org.springframework.web.bind.annotation.ModelAttribute;
1613import org .springframework .web .bind .annotation .RequestMapping ;
1714import org .springframework .web .bind .annotation .RequestMethod ;
1815import org .springframework .web .bind .annotation .RequestParam ;
@@ -30,148 +27,87 @@ public class HomeController {
3027 CatServices catServices ;
3128
3229
33- //rendering all the categories and products
34- /**
35- * @param model
36- * @return
37- */
3830 @ RequestMapping ("/" )
39- public String showMain (Model model ){
40- List <Product > products = catServices .allProduct ();
41-
42- List <Category > categories =catServices .allCategories ();
43-
31+ public String showMain (Model model ) {
32+ List < Product > products = catServices .allProduct ();
33+ List < Category > categories = catServices .allCategories ();
4434 model .addAttribute ("categories" , categories );
4535 model .addAttribute ("products" , products );
36+ return "index.jsp" ;
37+ }
4638
4739
48- return "index.jsp" ;
40+ @ RequestMapping ("/product/new" )
41+ private String showForm (@ ModelAttribute ("product" ) Product product ) {
42+ return "newproduct.jsp" ;
4943 }
5044
51- //adding a new product
52- //rendering
5345
54- /**
55- * @param product
56- * @return
57- */
58- @ RequestMapping ("/product/new" )
59- public String showForm (@ ModelAttribute ("product" ) Product product ){
46+ @ PostMapping ("/product/new" )
47+ private String create (@ Valid @ ModelAttribute ("product" ) Product product , BindingResult result ) {
48+ if (result .hasErrors ()) {
6049 return "newproduct.jsp" ;
50+ } else {
51+ catServices .createProduct (product );
52+ return "redirect:/" ;
6153 }
62-
63- //handling the action of adding a new procuct
64- /**
65- * @param product
66- * @param result
67- * @return
68- */
69- @ PostMapping ("/product/new" )
70- public String create (@ Valid @ ModelAttribute ("product" ) Product product , BindingResult result ) {
71- if (result .hasErrors ()) {
72- return "newproduct.jsp" ;
73- } else {
74- catServices .createProduct (product );
75- return "redirect:/" ;
76- }
7754 }
7855
79- // /category/new
56+ @ RequestMapping ("/category/new" )
57+ private String showForm1 (@ ModelAttribute ("category" ) Category category ) {
58+ return "newcat.jsp" ;
59+ }
8060
8161
82- /**
83- * @param category
84- * @return
85- */
86- @ RequestMapping ("/category/new" )
87- public String showForm1 (@ ModelAttribute ("category" ) Category category ){
62+ @ PostMapping ("/category/new" )
63+ private String create1 (@ Valid @ ModelAttribute ("category" ) Category category , BindingResult result ) {
64+ if (result .hasErrors ()) {
8865 return "newcat.jsp" ;
66+ } else {
67+ catServices .createCategory (category );
68+ return "redirect:/" ;
8969 }
90-
91- //handling the action of adding a new procuct
92- /**
93- * @param category
94- * @param result
95- * @return
96- */
97- @ PostMapping ("/category/new" )
98- public String create1 (@ Valid @ ModelAttribute ("category" ) Category category , BindingResult result ) {
99- if (result .hasErrors ()) {
100- return "newcat.jsp" ;
101- } else {
102- catServices .createCategory (category );
103- return "redirect:/" ;
104- }
10570 }
10671
107- // Show Category page
108- // /category/${category.id}
109-
110- // add product to category
111- //I probably need a form here, but is it assigned to the third table as a modelattribute?
112- // Show all Catogegories and add poducct to category
113- /**
114- * @param id
115- * @param model
116- * @return
117- */
118- @ RequestMapping ("/category/{id}" )
119- public String showCat (@ PathVariable ("id" ) Long id , Model model ) {
72+
73+ @ RequestMapping ("/category/{id}" )
74+ private String showCat (@ PathVariable ("id" ) Long id , Model model ) {
12075 Category cat = catServices .findCategory (id );
12176 model .addAttribute ("cat" , cat );
122- List < Product > prod = catServices .allProduct ();
77+ List < Product > prod = catServices .allProduct ();
12378 model .addAttribute ("prod" , prod );
79+ //here we need to return the
80+
81+ // productRepository.class.getName()
12482 return "showcat.jsp" ;
12583 }
12684
127- // @ModelAttribute("categoryProductObj") CategoryProduct categoryProduct
128-
129- //add category to procuct
130- /**
131- * @param id
132- * @param model
133- * @param prodId
134- * @return
135- */
136- @ RequestMapping (value ="/category/{id}" , method = RequestMethod .POST )
137- public String addproductToCategory (@ PathVariable ("id" ) Long id , Model model , @ RequestParam (value = "product" ) Long prodId ) {
138- catServices .addProductToCategory (id , prodId );
139- return "redirect:/" ;
140- }
141-
142- //show all Products and add category to priduct
143- // /products/${product.id}
144- /**
145- * @param id
146- * @param model
147- * @return
148- */
149- @ RequestMapping ("/products/{id}" )
15085
151- public String showProd (@ PathVariable ("id" ) Long id , Model model ) {
152- Product prod =catServices .findProduct (id );
153- List <Category > categories =catServices .allCategories ();
86+ @ RequestMapping (value = "/category/{id}" , method = RequestMethod .POST )
87+ private String addproductToCategory (@ PathVariable ("id" ) Long id , Model model , @ RequestParam (value = "product" ) Long prodId ) {
88+ catServices .addProductToCategory (id , prodId );
89+ return "redirect:/" ;
90+ }
15491
155- model .addAttribute ("allcats" , categories );
156- model .addAttribute ("product" , prod );
92+ @ RequestMapping ("/products/{id}" )
93+ private String showProd (@ PathVariable ("id" ) Long id , Model model ) {
94+ Product prod = catServices .findProduct (id );
95+ List < Category > categories = catServices .allCategories ();
96+ model .addAttribute ("allcats" , categories );
97+ model .addAttribute ("product" , prod );
98+ return "showprod.jsp" ;
99+ }
100+
157101
158- return "showprod.jsp" ;
159- }
160102
161- /**
162- * @param id
163- * @param model
164- * @param catId
165- * @return
166- */
167- @ RequestMapping (value ="/products/{id}" , method = RequestMethod .POST )
168- public String addcatToprod (@ PathVariable ("id" ) Long id , Model model , @ RequestParam (value = "cat" ) Long catId ) {
169- catServices .addCategoryToProduct (id ,catId );
170- return "redirect:/" ;
171- }
103+ @ RequestMapping (value = "/products/{id}" , method = RequestMethod .POST )
104+ private String addcatToprod (@ PathVariable ("id" ) Long id , Model model , @ RequestParam (value = "cat" ) Long catId ) {
105+ catServices .addCategoryToProduct (id , catId );
106+ return "redirect:/" ;
107+ }
172108
173109
174110
175111
176112
177- }
113+ }
0 commit comments