Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
javabyranjith committed Jul 17, 2020
1 parent b76d564 commit 70fed32
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 113 deletions.
9 changes: 6 additions & 3 deletions sboot-rest-swagger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ copy the response and paste @ https://editor.swagger.io/
### **View Swagger UI**
* http://localhost:6060/restapi-swagger/swagger-ui.html

### **RESTApi Response**
* http://localhost:6060/restapi-swagger/products
* http://localhost:6060/restapi-swagger/products/100
### **REST End-points**
* GET: http://localhost:6060/restapi-swagger/products
* GET: http://localhost:6060/restapi-swagger/products/{id}
* POST: http://localhost:6060/restapi-swagger/addProduct
* PUT: http://localhost:6060/restapi-swagger/updateProduct/{id}
* DELETE: http://localhost:6060/restapi-swagger/deleteProduct/{id}

### TEST CUSTOM EXCEPTIONS
#### CustomExceptionGlobalHandler.handleMethodArgumentNotValid(); - Choose POST method in POSTMAN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public ResponseEntity<Void> addProduct(@Valid @RequestBody Product product, UriC
try {
productService.addProduct(product);
HttpHeaders headers = new HttpHeaders();
headers.setLocation(builder.path("/addProduct")
.buildAndExpand(product.getId())
.toUri());
headers.setLocation(builder.path("/addProduct").buildAndExpand(product.getId()).toUri());
return new ResponseEntity<Void>(headers, HttpStatus.CREATED);
} catch (ProductExistsException ex) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, ex.getMessage());
Expand Down
15 changes: 11 additions & 4 deletions sboot-rest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
1. Eclipse/STS
2. SpringBoot
3. REST API
4. H2DB

### CONCEPTS/TOPICS COVERED
1. CRUD Operations with hard-coded values in Java HashMap
1. CRUD Operations with H2DB.

### HOW TO RUN? (use POSTMAN Client)
http://localhost:8080/ </br>
http://localhost:8080/products </br>
http://localhost:8080/products/100
## RESTApi End-points
* GET: http://localhost:6060/springboot-restapi/product/all
* GET: http://localhost:6060/springboot-restapi/product/{id}
* POST: http://localhost:6060/springboot-restapi/product/add
* PUT: http://localhost:6060/springboot-restapi/product/update/{id}
* DELETE: http://localhost:6060/springboot-restapi/product/delete/{id}

### H2 DB Console
http://localhost:6060/springboot-restapi/h2-console
20 changes: 14 additions & 6 deletions sboot-rest/build.gradle
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@


plugins {
id 'org.springframework.boot' version '2.2.4.RELEASE'
id 'java'
}

apply plugin: 'io.spring.dependency-management'

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

repositories {
jcenter()
mavenCentral()
}

dependencies {
compile group: 'org.springframework.boot', name: 'spring-boot-starter-parent', version: '2.0.0.RELEASE', ext: 'pom'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.0.0.RELEASE'
testImplementation 'junit:junit:4.12'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

runtime('com.h2database:h2')

implementation 'org.projectlombok:lombok:1.18.12'

testImplementation 'org.springframework.boot:spring-boot-starter-test'
runtime('org.springframework.boot:spring-boot-devtools')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,19 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import jbr.springboot.restapi.model.Product;
import jbr.springboot.restapi.model.ProductApiResponse;
import jbr.springboot.restapi.service.ProductService;

/**
Expand All @@ -19,33 +25,37 @@
* @since 2018, Jun 20
*/
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/product")
public class ProductController {

@Autowired
private ProductService productService;

@RequestMapping("/products")
public List<Product> getAllProducts() {
return productService.getAllProducts();
@GetMapping("/all")
public ProductApiResponse<List<Product>> getAllProducts() {
return new ProductApiResponse<>(HttpStatus.OK.value(), "All products are retrieved successfully.",
productService.getAllProducts());
}

@RequestMapping("/products/{id}")
public Product getProductById(@PathVariable String id) {
return productService.getProductById(id);
@GetMapping("/{id}")
public ProductApiResponse<Product> getProductById(@PathVariable String id) {
return new ProductApiResponse<Product>(HttpStatus.OK.value(), "Product retrieved successfully.",
productService.getProductById(id));
}

@RequestMapping(method = RequestMethod.POST, value = "/products")
@PostMapping("/add")
public void addProduct(@RequestBody Product product) {
productService.addProduct(product);
}

@RequestMapping(method = RequestMethod.PUT, value = "/products/{id}")
@PutMapping(value = "/update/{id}")
public void updateProduct(@RequestBody Product product, @PathVariable String id) {
productService.updateProduct(product, id);
}

@RequestMapping(method = RequestMethod.DELETE, value = "/products/{id}")
@DeleteMapping("/delete/{id}")
public void deleteProduct(@PathVariable String id) {
productService.delete(id);
productService.deleteProduct(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package jbr.springboot.restapi.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import jbr.springboot.restapi.model.Product;

@Repository
public interface ProductRepository extends JpaRepository<Product, String> {

Product findProductByName(String name);
}
68 changes: 16 additions & 52 deletions sboot-rest/src/main/java/jbr/springboot/restapi/model/Product.java
Original file line number Diff line number Diff line change
@@ -1,63 +1,27 @@
package jbr.springboot.restapi.model;

import javax.persistence.Entity;
import javax.persistence.Id;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
* Product Model.
*
* @author Ranjith Sekar
* @since 2018, Jun 20
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Product {

private String id;
private String name;
private String type;
private String price;

public Product() {
}

public Product(String id, String name, String type, String price) {
this.id = id;
this.name = name;
this.type = type;
this.price = price;
}

public String getName() {
return name;
}

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

@Override
public String toString() {
return "id:" + this.id + " | name: " + this.name + " | type: " + this.type + " | price: " + this.price;
}

@Id
private String id;
private String name;
private String category;
private String price;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package jbr.springboot.restapi.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class ProductApiResponse<T> {
private int status;
private String message;
private Object result;
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package jbr.springboot.restapi.service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.springframework.stereotype.Service;

import jbr.springboot.restapi.model.Product;

Expand All @@ -14,38 +10,14 @@
* @author Ranjith Sekar
* @since 2018, Jun 20
*/
@Service
public class ProductService {

List<Product> products = Stream.of(new Product("100", "Galaxy S8", "Mobile", "50000"),
new Product("200", "Honda Shine", "Vehicle", "60000"), new Product("300", "Dell Vostro", "Laptop", "75000"))
.collect(Collectors.toList());

public List<Product> getAllProducts() {
return products;
}

public Product getProductById(String id) {
return products.stream().filter(e -> e.getId().equals(id)).findFirst().get();
}
public interface ProductService {
List<Product> getAllProducts();

public void addProduct(Product product) {
products.add(product);
}
Product getProductById(String id);

public void updateProduct(Product product, String id) {
System.out.println(product.toString());
for (int i = 0; i < products.size(); i++) {
Product currentProduct = products.get(i);
void addProduct(Product product);

if (currentProduct.getId().equals(id)) {
products.set(i, product);
return;
}
}
}
void updateProduct(Product product, String id);

public void delete(String id) {
products.remove(getProductById(id));
}
void deleteProduct(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package jbr.springboot.restapi.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import jbr.springboot.restapi.dao.ProductRepository;
import jbr.springboot.restapi.model.Product;

@Service
public class ProductServiceImpl implements ProductService {

@Autowired
private ProductRepository productRepository;

@Override
public List<Product> getAllProducts() {
return productRepository.findAll();
}

@Override
public Product getProductById(String id) {
return productRepository.findById(id).get();
}

@Override
public void addProduct(Product product) {

}

@Override
public void updateProduct(Product product, String id) {

}

@Override
public void deleteProduct(String id) {

}

}
16 changes: 16 additions & 0 deletions sboot-rest/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
server:
servlet:
context-path: /springboot-restapi
port: 6060

spring:
datasource:
driver-class-name: org.h2.Driver
url: jdbc:h2:mem:testdb
username: ranjith
password: sekar

h2:
console:
path: /h2-console
enabled: true
5 changes: 5 additions & 0 deletions sboot-rest/src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
INSERT INTO product(id, name, category, price) VALUES('100', 'Samsung S8', 'Mobile', '75000');
INSERT INTO product(id, name, category, price) VALUES('200', 'Usha Fan', 'Fan', '6000');
INSERT INTO product(id, name, category, price) VALUES('300', 'Dell Vostro', 'Laptop', '79000');
INSERT INTO product(id, name, category, price) VALUES('400', 'IFB Washer', 'Washing Machine', '25000');
INSERT INTO product(id, name, category, price) VALUES('500', 'Redmi 8', 'Mobile', '5000');

0 comments on commit 70fed32

Please sign in to comment.