Skip to content

Commit 4b0c8de

Browse files
initial project commit
1 parent 627c8f8 commit 4b0c8de

File tree

12 files changed

+508
-37
lines changed

12 files changed

+508
-37
lines changed

pom.xml

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<parent>
1515
<groupId>org.springframework.boot</groupId>
1616
<artifactId>spring-boot-starter-parent</artifactId>
17-
<version>1.5.0.RC1</version>
17+
<version>1.5.1.RELEASE</version>
1818
<relativePath/> <!-- lookup parent from repository -->
1919
</parent>
2020

@@ -54,41 +54,5 @@
5454
</plugins>
5555
</build>
5656

57-
<repositories>
58-
<repository>
59-
<id>spring-snapshots</id>
60-
<name>Spring Snapshots</name>
61-
<url>https://repo.spring.io/snapshot</url>
62-
<snapshots>
63-
<enabled>true</enabled>
64-
</snapshots>
65-
</repository>
66-
<repository>
67-
<id>spring-milestones</id>
68-
<name>Spring Milestones</name>
69-
<url>https://repo.spring.io/milestone</url>
70-
<snapshots>
71-
<enabled>false</enabled>
72-
</snapshots>
73-
</repository>
74-
</repositories>
75-
<pluginRepositories>
76-
<pluginRepository>
77-
<id>spring-snapshots</id>
78-
<name>Spring Snapshots</name>
79-
<url>https://repo.spring.io/snapshot</url>
80-
<snapshots>
81-
<enabled>true</enabled>
82-
</snapshots>
83-
</pluginRepository>
84-
<pluginRepository>
85-
<id>spring-milestones</id>
86-
<name>Spring Milestones</name>
87-
<url>https://repo.spring.io/milestone</url>
88-
<snapshots>
89-
<enabled>false</enabled>
90-
</snapshots>
91-
</pluginRepository>
92-
</pluginRepositories>
9357

9458
</project>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package guru.springframework.commands;
2+
3+
4+
import java.math.BigDecimal;
5+
6+
/**
7+
* Created by jt on 1/10/17.
8+
*/
9+
public class ProductForm {
10+
private String id;
11+
private String description;
12+
private BigDecimal price;
13+
private String imageUrl;
14+
15+
public String getId() {
16+
return id;
17+
}
18+
19+
public void setId(String id) {
20+
this.id = id;
21+
}
22+
23+
public String getDescription() {
24+
return description;
25+
}
26+
27+
public void setDescription(String description) {
28+
this.description = description;
29+
}
30+
31+
public BigDecimal getPrice() {
32+
return price;
33+
}
34+
35+
public void setPrice(BigDecimal price) {
36+
this.price = price;
37+
}
38+
39+
public String getImageUrl() {
40+
return imageUrl;
41+
}
42+
43+
public void setImageUrl(String imageUrl) {
44+
this.imageUrl = imageUrl;
45+
}
46+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package guru.springframework.controllers;
2+
3+
import guru.springframework.commands.ProductForm;
4+
import guru.springframework.converters.ProductToProductForm;
5+
import guru.springframework.domain.Product;
6+
import guru.springframework.services.ProductService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
10+
import org.springframework.validation.BindingResult;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
15+
import javax.validation.Valid;
16+
17+
/**
18+
* Created by jt on 1/10/17.
19+
*/
20+
@Controller
21+
public class ProductController {
22+
private ProductService productService;
23+
24+
private ProductToProductForm productToProductForm;
25+
26+
@Autowired
27+
public void setProductToProductForm(ProductToProductForm productToProductForm) {
28+
this.productToProductForm = productToProductForm;
29+
}
30+
31+
@Autowired
32+
public void setProductService(ProductService productService) {
33+
this.productService = productService;
34+
}
35+
36+
@RequestMapping("/")
37+
public String redirToList(){
38+
return "redirect:/product/list";
39+
}
40+
41+
@RequestMapping({"/product/list", "/product"})
42+
public String listProducts(Model model){
43+
model.addAttribute("products", productService.listAll());
44+
return "product/list";
45+
}
46+
47+
@RequestMapping("/product/show/{id}")
48+
public String getProduct(@PathVariable String id, Model model){
49+
model.addAttribute("product", productService.getById(id));
50+
return "product/show";
51+
}
52+
53+
@RequestMapping("product/edit/{id}")
54+
public String edit(@PathVariable String id, Model model){
55+
Product product = productService.getById(id);
56+
ProductForm productForm = productToProductForm.convert(product);
57+
58+
model.addAttribute("productForm", productForm);
59+
return "product/productform";
60+
}
61+
62+
@RequestMapping("/product/new")
63+
public String newProduct(Model model){
64+
model.addAttribute("productForm", new ProductForm());
65+
return "product/productform";
66+
}
67+
68+
@RequestMapping(value = "/product", method = RequestMethod.POST)
69+
public String saveOrUpdateProduct(@Valid ProductForm productForm, BindingResult bindingResult){
70+
71+
if(bindingResult.hasErrors()){
72+
return "product/productform";
73+
}
74+
75+
Product savedProduct = productService.saveOrUpdateProductForm(productForm);
76+
77+
return "redirect:/product/show/" + savedProduct.getId();
78+
}
79+
80+
@RequestMapping("/product/delete/{id}")
81+
public String delete(@PathVariable String id){
82+
productService.delete(id);
83+
return "redirect:/product/list";
84+
}
85+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package guru.springframework.converters;
2+
3+
import guru.springframework.commands.ProductForm;
4+
import guru.springframework.domain.Product;
5+
import org.bson.types.ObjectId;
6+
import org.springframework.core.convert.converter.Converter;
7+
import org.springframework.stereotype.Component;
8+
import org.springframework.util.StringUtils;
9+
10+
/**
11+
* Created by jt on 1/10/17.
12+
*/
13+
@Component
14+
public class ProductFormToProduct implements Converter<ProductForm, Product> {
15+
16+
@Override
17+
public Product convert(ProductForm productForm) {
18+
Product product = new Product();
19+
if (productForm.getId() != null && !StringUtils.isEmpty(productForm.getId())) {
20+
product.setId(new ObjectId(productForm.getId()));
21+
}
22+
product.setDescription(productForm.getDescription());
23+
product.setPrice(productForm.getPrice());
24+
product.setImageUrl(productForm.getImageUrl());
25+
return product;
26+
}
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package guru.springframework.converters;
2+
3+
import guru.springframework.commands.ProductForm;
4+
import guru.springframework.domain.Product;
5+
import org.springframework.core.convert.converter.Converter;
6+
import org.springframework.stereotype.Component;
7+
8+
/**
9+
* Created by jt on 1/10/17.
10+
*/
11+
@Component
12+
public class ProductToProductForm implements Converter<Product, ProductForm> {
13+
@Override
14+
public ProductForm convert(Product product) {
15+
ProductForm productForm = new ProductForm();
16+
productForm.setId(product.getId().toHexString());
17+
productForm.setDescription(product.getDescription());
18+
productForm.setPrice(product.getPrice());
19+
productForm.setImageUrl(product.getImageUrl());
20+
return productForm;
21+
}
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package guru.springframework.domain;
2+
3+
import org.bson.types.ObjectId;
4+
import org.springframework.data.annotation.Id;
5+
import org.springframework.data.mongodb.core.mapping.Document;
6+
7+
import java.math.BigDecimal;
8+
9+
/**
10+
* Created by jt on 1/10/17.
11+
*/
12+
@Document
13+
public class Product {
14+
@Id
15+
private ObjectId _id;
16+
private String description;
17+
private BigDecimal price;
18+
private String imageUrl;
19+
20+
public ObjectId getId() {
21+
return _id;
22+
}
23+
24+
public void setId(ObjectId id) {
25+
this._id = id;
26+
}
27+
28+
public String getDescription() {
29+
return description;
30+
}
31+
32+
public void setDescription(String description) {
33+
this.description = description;
34+
}
35+
36+
public BigDecimal getPrice() {
37+
return price;
38+
}
39+
40+
public void setPrice(BigDecimal price) {
41+
this.price = price;
42+
}
43+
44+
public String getImageUrl() {
45+
return imageUrl;
46+
}
47+
48+
public void setImageUrl(String imageUrl) {
49+
this.imageUrl = imageUrl;
50+
}
51+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package guru.springframework.repositories;
2+
3+
import guru.springframework.domain.Product;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
/**
7+
* Created by jt on 1/10/17.
8+
*/
9+
public interface ProductRepository extends CrudRepository<Product, String> {
10+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.commands.ProductForm;
4+
import guru.springframework.domain.Product;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Created by jt on 1/10/17.
10+
*/
11+
public interface ProductService {
12+
13+
List<Product> listAll();
14+
15+
Product getById(String id);
16+
17+
Product saveOrUpdate(Product product);
18+
19+
void delete(String id);
20+
21+
Product saveOrUpdateProductForm(ProductForm productForm);
22+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package guru.springframework.services;
2+
3+
import guru.springframework.commands.ProductForm;
4+
import guru.springframework.converters.ProductFormToProduct;
5+
import guru.springframework.domain.Product;
6+
import guru.springframework.repositories.ProductRepository;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Service;
9+
10+
import java.util.ArrayList;
11+
import java.util.List;
12+
13+
/**
14+
* Created by jt on 1/10/17.
15+
*/
16+
@Service
17+
public class ProductServiceImpl implements ProductService {
18+
19+
private ProductRepository productRepository;
20+
private ProductFormToProduct productFormToProduct;
21+
22+
@Autowired
23+
public ProductServiceImpl(ProductRepository productRepository, ProductFormToProduct productFormToProduct) {
24+
this.productRepository = productRepository;
25+
this.productFormToProduct = productFormToProduct;
26+
}
27+
28+
29+
@Override
30+
public List<Product> listAll() {
31+
List<Product> products = new ArrayList<>();
32+
productRepository.findAll().forEach(products::add); //fun with Java 8
33+
return products;
34+
}
35+
36+
@Override
37+
public Product getById(String id) {
38+
return productRepository.findOne(id);
39+
}
40+
41+
@Override
42+
public Product saveOrUpdate(Product product) {
43+
productRepository.save(product);
44+
return product;
45+
}
46+
47+
@Override
48+
public void delete(String id) {
49+
productRepository.delete(id);
50+
51+
}
52+
53+
@Override
54+
public Product saveOrUpdateProductForm(ProductForm productForm) {
55+
Product savedProduct = saveOrUpdate(productFormToProduct.convert(productForm));
56+
57+
System.out.println("Saved Product Id: " + savedProduct.getId());
58+
return savedProduct;
59+
}
60+
}

0 commit comments

Comments
 (0)