From d3ffc0d51df39f7a7ae61e03735c5c3b700a4900 Mon Sep 17 00:00:00 2001 From: "p.hill" Date: Mon, 1 Jun 2015 18:56:58 -0700 Subject: [PATCH 1/3] added delete sprocket by SKU and cleaned warnings in controller --- pom.xml | 4 ++++ .../controller/SprocketController.java | 15 ++++++++++++--- .../com/dev9/dataexample/entity/Sprocket.java | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 06df9d4..35eff44 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,10 @@ guava 18.0 + + org.springframework.boot + spring-boot-starter-data-rest + diff --git a/src/main/java/com/dev9/dataexample/controller/SprocketController.java b/src/main/java/com/dev9/dataexample/controller/SprocketController.java index 1f87cc9..d45551f 100644 --- a/src/main/java/com/dev9/dataexample/controller/SprocketController.java +++ b/src/main/java/com/dev9/dataexample/controller/SprocketController.java @@ -4,14 +4,14 @@ import com.dev9.dataexample.entity.Sprocket; import com.dev9.dataexample.repo.SprocketRepository; import com.google.common.collect.Lists; + import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; 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.ResponseBody; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; @@ -43,5 +43,14 @@ public void newSprocket(@RequestBody Sprocket sprocket) { public Sprocket getSprocketBySku(@PathVariable("sku") String sku) { return sprocketRepository.findBySku(sku); } - + + @RequestMapping(value = "/sprockets/sku/{sku}", method = {RequestMethod.DELETE}) + public void deleteSprocketBySku(@PathVariable("sku") String sku) { + Sprocket sprocket = sprocketRepository.findBySku(sku); + if (sprocket == null) { + throw new ResourceNotFoundException("SKU " + sku + " not Found."); + } + long id = sprocket.getId(); + sprocketRepository.delete(id); + } } diff --git a/src/main/java/com/dev9/dataexample/entity/Sprocket.java b/src/main/java/com/dev9/dataexample/entity/Sprocket.java index 80717c0..ae8869c 100644 --- a/src/main/java/com/dev9/dataexample/entity/Sprocket.java +++ b/src/main/java/com/dev9/dataexample/entity/Sprocket.java @@ -22,6 +22,10 @@ public class Sprocket implements Serializable { private String sku; private String brand; + + public long getId() { + return id; + } public String getDescription() { return description; From 9a49f4d6db8599e5ddf8eb50351ad446fad37278 Mon Sep 17 00:00:00 2001 From: "p.hill" Date: Wed, 3 Jun 2015 15:09:48 -0700 Subject: [PATCH 2/3] added deleteeBySku with necessary @Transaction --- .../dev9/dataexample/controller/SprocketController.java | 8 +------- .../com/dev9/dataexample/repo/SprocketRepository.java | 6 +++++- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/dev9/dataexample/controller/SprocketController.java b/src/main/java/com/dev9/dataexample/controller/SprocketController.java index d45551f..8099f70 100644 --- a/src/main/java/com/dev9/dataexample/controller/SprocketController.java +++ b/src/main/java/com/dev9/dataexample/controller/SprocketController.java @@ -6,7 +6,6 @@ import com.google.common.collect.Lists; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.rest.webmvc.ResourceNotFoundException; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; @@ -46,11 +45,6 @@ public Sprocket getSprocketBySku(@PathVariable("sku") String sku) { @RequestMapping(value = "/sprockets/sku/{sku}", method = {RequestMethod.DELETE}) public void deleteSprocketBySku(@PathVariable("sku") String sku) { - Sprocket sprocket = sprocketRepository.findBySku(sku); - if (sprocket == null) { - throw new ResourceNotFoundException("SKU " + sku + " not Found."); - } - long id = sprocket.getId(); - sprocketRepository.delete(id); + sprocketRepository.deleteBySku(sku); } } diff --git a/src/main/java/com/dev9/dataexample/repo/SprocketRepository.java b/src/main/java/com/dev9/dataexample/repo/SprocketRepository.java index e61cb54..3c56b99 100644 --- a/src/main/java/com/dev9/dataexample/repo/SprocketRepository.java +++ b/src/main/java/com/dev9/dataexample/repo/SprocketRepository.java @@ -2,15 +2,19 @@ import com.dev9.dataexample.entity.Sprocket; + import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @Repository +@Transactional public interface SprocketRepository extends CrudRepository { Sprocket findBySku(String sku); List findByBrand(String brand); - + + void deleteBySku(String sku); } From a05f9b372135ca17fcce6b05f3585133b01e4827 Mon Sep 17 00:00:00 2001 From: "p.hill" Date: Wed, 3 Jun 2015 15:46:44 -0700 Subject: [PATCH 3/3] cleanup of unnecessary dependency in POM --- pom.xml | 4 ---- src/main/java/com/dev9/dataexample/entity/Sprocket.java | 4 ---- 2 files changed, 8 deletions(-) diff --git a/pom.xml b/pom.xml index 35eff44..06df9d4 100644 --- a/pom.xml +++ b/pom.xml @@ -31,10 +31,6 @@ guava 18.0 - - org.springframework.boot - spring-boot-starter-data-rest - diff --git a/src/main/java/com/dev9/dataexample/entity/Sprocket.java b/src/main/java/com/dev9/dataexample/entity/Sprocket.java index ae8869c..e8c4739 100644 --- a/src/main/java/com/dev9/dataexample/entity/Sprocket.java +++ b/src/main/java/com/dev9/dataexample/entity/Sprocket.java @@ -23,10 +23,6 @@ public class Sprocket implements Serializable { private String brand; - public long getId() { - return id; - } - public String getDescription() { return description; }