Skip to content
This repository was archived by the owner on May 26, 2024. It is now read-only.

Commit a077c8a

Browse files
committed
refactoring and fixing some issues
1 parent e32fbe2 commit a077c8a

File tree

8 files changed

+224
-140
lines changed

8 files changed

+224
-140
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Book</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.validation.validationbuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.m2e.core.maven2Builder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
</buildSpec>
34+
<natures>
35+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
36+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
37+
<nature>org.eclipse.jdt.core.javanature</nature>
38+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
39+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
40+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
41+
</natures>
42+
<filteredResources>
43+
<filter>
44+
<id>1683029717030</id>
45+
<name></name>
46+
<type>30</type>
47+
<matcher>
48+
<id>org.eclipse.core.resources.regexFilterMatcher</id>
49+
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
50+
</matcher>
51+
</filter>
52+
</filteredResources>
53+
</projectDescription>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Book</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.validation.validationbuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.springframework.ide.eclipse.boot.validation.springbootbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.m2e.core.maven2Builder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
</buildSpec>
34+
<natures>
35+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
36+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
37+
<nature>org.eclipse.jdt.core.javanature</nature>
38+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
39+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
40+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
41+
</natures>
42+
<filteredResources>
43+
<filter>
44+
<id>1683029717030</id>
45+
<name></name>
46+
<type>30</type>
47+
<matcher>
48+
<id>org.eclipse.core.resources.regexFilterMatcher</id>
49+
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
50+
</matcher>
51+
</filter>
52+
</filteredResources>
53+
</projectDescription>

spring adding one to many/Day 60/1-Products and categories/CategoriesandProducts/src/main/java/com/codingdojo/mvc/controllers/HomeController.java

Lines changed: 52 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
import java.util.List;
44

55
import 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+
97
import org.springframework.stereotype.Controller;
108
import org.springframework.ui.Model;
119
import org.springframework.validation.BindingResult;
1210
import org.springframework.web.bind.annotation.ModelAttribute;
1311
import org.springframework.web.bind.annotation.PathVariable;
1412
import org.springframework.web.bind.annotation.PostMapping;
15-
// import org.springframework.web.bind.annotation.ModelAttribute;
1613
import org.springframework.web.bind.annotation.RequestMapping;
1714
import org.springframework.web.bind.annotation.RequestMethod;
1815
import 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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.codingdojo.mvc.controllers;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.PathVariable;
8+
// import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import com.codingdojo.mvc.models.Category;
14+
import com.codingdojo.mvc.models.Product;
15+
import com.codingdojo.mvc.services.CatServices;
16+
17+
@RestController
18+
public class RestApi {
19+
20+
private final CatServices catServices;
21+
public RestApi(CatServices catServices){
22+
this.catServices = catServices;
23+
}
24+
25+
@RequestMapping("/test/cat/{id}")
26+
public Category getCategory(@PathVariable("id" ) long id){
27+
System.out.println("HI");
28+
return catServices.findCategory(id);
29+
}
30+
31+
32+
33+
}

spring adding one to many/Day 60/1-Products and categories/CategoriesandProducts/src/main/java/com/codingdojo/mvc/models/Category.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ protected void onUpdate(){
6767
private List<Product> products;
6868

6969

70+
7071
public List<Product> getProducts() {
7172
return products;
7273
}

spring adding one to many/Day 60/1-Products and categories/CategoriesandProducts/src/main/java/com/codingdojo/mvc/repository/categoryRepository.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ public interface categoryRepository extends CrudRepository<Category, Long>{
1313

1414
List<Category> findAll();
1515
// Retrieves a list of all categories for a particular product
16-
List<Category> findAllByProducts(Product product);
16+
List<Category> findAllByProducts(Product product);
1717

1818
// Retrieves a list of any categories a particular product
1919
// does not belong to.
20-
List<Category> findByProductsNotContains(Optional<Product> product);
20+
List<Category> findByProductsNotContains(Optional<Product> product);
2121
}
22+
// List<Category> findAllByProducts(Product product);
23+
// List<Category> findByProductsNotContains(Product product);

0 commit comments

Comments
 (0)