Skip to content

Commit 9c5e90d

Browse files
author
mfarid
committed
Add cassandra related config
1 parent 3a177c1 commit 9c5e90d

File tree

17 files changed

+661
-0
lines changed

17 files changed

+661
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@
1010

1111
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
1212
hs_err_pid*
13+
14+
target
15+
16+
# IDE files
17+
.idea
18+
*.iml

pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>guru.springframework</groupId>
7+
<artifactId>spring-boot-cassandra</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-cassandra</name>
12+
<description>Demo project for Spring Boot and cassandra</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.1.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-cassandra</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-test</artifactId>
44+
<scope>test</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
<build>
49+
<plugins>
50+
<plugin>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-maven-plugin</artifactId>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
57+
58+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package guru.springframework;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootCassandraApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootCassandraApplication.class, args);
11+
}
12+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package guru.springframework.commands;
2+
3+
4+
import java.math.BigDecimal;
5+
import java.util.UUID;
6+
7+
/**
8+
* Created by jt on 1/10/17.
9+
*/
10+
public class ProductForm {
11+
private UUID id;
12+
private String description;
13+
private BigDecimal price;
14+
private String imageUrl;
15+
16+
public UUID getId() {
17+
return id;
18+
}
19+
20+
public void setId(UUID id) {
21+
this.id = id;
22+
}
23+
24+
public String getDescription() {
25+
return description;
26+
}
27+
28+
public void setDescription(String description) {
29+
this.description = description;
30+
}
31+
32+
public BigDecimal getPrice() {
33+
return price;
34+
}
35+
36+
public void setPrice(BigDecimal price) {
37+
this.price = price;
38+
}
39+
40+
public String getImageUrl() {
41+
return imageUrl;
42+
}
43+
44+
public void setImageUrl(String imageUrl) {
45+
this.imageUrl = imageUrl;
46+
}
47+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
import java.util.UUID;
17+
18+
/**
19+
* Created by jt on 1/10/17.
20+
*/
21+
@Controller
22+
public class ProductController {
23+
private ProductService productService;
24+
25+
private ProductToProductForm productToProductForm;
26+
27+
@Autowired
28+
public void setProductToProductForm(ProductToProductForm productToProductForm) {
29+
this.productToProductForm = productToProductForm;
30+
}
31+
32+
@Autowired
33+
public void setProductService(ProductService productService) {
34+
this.productService = productService;
35+
}
36+
37+
@RequestMapping("/")
38+
public String redirToList(){
39+
return "redirect:/product/list";
40+
}
41+
42+
@RequestMapping({"/product/list", "/product"})
43+
public String listProducts(Model model){
44+
model.addAttribute("products", productService.listAll());
45+
return "product/list";
46+
}
47+
48+
@RequestMapping("/product/show/{id}")
49+
public String getProduct(@PathVariable String id, Model model){
50+
model.addAttribute("product", productService.getById(UUID.fromString(id)));
51+
return "product/show";
52+
}
53+
54+
@RequestMapping("product/edit/{id}")
55+
public String edit(@PathVariable String id, Model model){
56+
Product product = productService.getById(UUID.fromString(id));
57+
ProductForm productForm = productToProductForm.convert(product);
58+
59+
model.addAttribute("productForm", productForm);
60+
return "product/productform";
61+
}
62+
63+
@RequestMapping("/product/new")
64+
public String newProduct(Model model){
65+
model.addAttribute("productForm", new ProductForm());
66+
return "product/productform";
67+
}
68+
69+
@RequestMapping(value = "/product", method = RequestMethod.POST)
70+
public String saveOrUpdateProduct(@Valid ProductForm productForm, BindingResult bindingResult){
71+
72+
if(bindingResult.hasErrors()){
73+
return "product/productform";
74+
}
75+
76+
Product savedProduct = productService.saveOrUpdateProductForm(productForm);
77+
78+
return "redirect:/product/show/" + savedProduct.getId();
79+
}
80+
81+
@RequestMapping("/product/delete/{id}")
82+
public String delete(@PathVariable String id){
83+
productService.delete(UUID.fromString(id));
84+
return "redirect:/product/list";
85+
}
86+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
import org.springframework.util.StringUtils;
8+
9+
/**
10+
* Created by jt on 1/10/17.
11+
*/
12+
@Component
13+
public class ProductFormToProduct implements Converter<ProductForm, Product> {
14+
15+
@Override
16+
public Product convert(ProductForm productForm) {
17+
Product product = new Product();
18+
if (productForm.getId() != null && !StringUtils.isEmpty(productForm.getId())) {
19+
product.setId(productForm.getId());
20+
}
21+
product.setDescription(productForm.getDescription());
22+
product.setPrice(productForm.getPrice());
23+
product.setImageUrl(productForm.getImageUrl());
24+
return product;
25+
}
26+
}
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());
17+
productForm.setDescription(product.getDescription());
18+
productForm.setPrice(product.getPrice());
19+
productForm.setImageUrl(product.getImageUrl());
20+
return productForm;
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package guru.springframework.domain;
2+
3+
import org.springframework.data.cassandra.mapping.PrimaryKey;
4+
import org.springframework.data.cassandra.mapping.Table;
5+
6+
import java.io.Serializable;
7+
import java.math.BigDecimal;
8+
import java.util.UUID;
9+
10+
/**
11+
* Created by jt on 1/10/17.
12+
*/
13+
@Table("products")
14+
public class Product implements Serializable{
15+
16+
@PrimaryKey
17+
private UUID id;
18+
private String description;
19+
private BigDecimal price;
20+
private String imageUrl;
21+
22+
public UUID getId() {
23+
return id;
24+
}
25+
26+
public void setId(UUID id) {
27+
this.id = id;
28+
}
29+
30+
public String getDescription() {
31+
return description;
32+
}
33+
34+
public void setDescription(String description) {
35+
this.description = description;
36+
}
37+
38+
public BigDecimal getPrice() {
39+
return price;
40+
}
41+
42+
public void setPrice(BigDecimal price) {
43+
this.price = price;
44+
}
45+
46+
public String getImageUrl() {
47+
return imageUrl;
48+
}
49+
50+
public void setImageUrl(String imageUrl) {
51+
this.imageUrl = imageUrl;
52+
}
53+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package guru.springframework.repositories;
2+
3+
import guru.springframework.domain.Product;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
import java.util.UUID;
7+
8+
/**
9+
* Created by jt on 1/10/17.
10+
*/
11+
public interface ProductRepository extends CrudRepository<Product, UUID> {
12+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
import java.util.UUID;
8+
9+
/**
10+
* Created by jt on 1/10/17.
11+
*/
12+
public interface ProductService {
13+
14+
List<Product> listAll();
15+
16+
Product getById(UUID id);
17+
18+
Product saveOrUpdate(Product product);
19+
20+
void delete(UUID id);
21+
22+
Product saveOrUpdateProductForm(ProductForm productForm);
23+
}

0 commit comments

Comments
 (0)