From 5b6fe52a04b3f148898195ac0620de3367bd06c1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 24 Mar 2019 00:16:01 +0100 Subject: [PATCH 1/3] added a isPresent() check and unregistered users are able start a cache now --- .../bugageocaching/controller/Controller.java | 58 +++++++++++++------ 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index c524e99..25926d8 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -34,33 +34,57 @@ public class Controller { @RequestMapping("/allCaches") @ResponseBody - public String getAllCaches(){ + public String getAllCaches() { return new Gson().toJson(cacheRepository.findAll()); } @RequestMapping("/startCache") public @ResponseBody - Bearbeitet startCache(@RequestParam String userID, - @RequestParam String cacheID, - @RequestParam String cacheAccesDefinitionID) { + String startCache(@RequestParam(value = "userID", defaultValue = "-1") String userID, + @RequestParam String cacheID, + @RequestParam String cacheAccesDefinitionID) { - Optional userOptional = userRepository.findById(Integer.valueOf(userID)); - User user = userOptional.get(); + if (!userID.equals("-1")) { // ein angemeldeter User startet den Cache - Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); - Cache cache = cacheOptional.get(); + Bearbeitet bearbeitet = new Bearbeitet(); - Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(Integer.valueOf(cacheAccesDefinitionID)); - CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); + Optional userOptional = userRepository.findById(Integer.valueOf(userID)); + if (userOptional.isPresent()) { + User user = userOptional.get(); + bearbeitet.setUser(user); + } else { + return "There is no user with the ID " + userID; + } - Bearbeitet bearbeitet = new Bearbeitet(); - bearbeitet.setUser(user); - bearbeitet.setCache(cache); - bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); + if (cacheOptional.isPresent()) { + Cache cache = cacheOptional.get(); + bearbeitet.setCache(cache); + } else { + return "There is no cache with the ID " + cacheID; + } - bearbeitetRepository.save(bearbeitet); + Optional cacheAccesDefinitionOptional = + cacheAccesDefinitionRepository.findById(Integer.valueOf(cacheAccesDefinitionID)); + if (cacheAccesDefinitionOptional.isPresent()) { + CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); + bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); + } else { + return "There is no cacheAccesDefinition with the ID " + cacheAccesDefinitionID; + } - return bearbeitet; + bearbeitetRepository.save(bearbeitet); + + return new Gson().toJson(bearbeitet); + + } else { // kein angemeldeter User startet den Cache + Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); + if (cacheOptional.isPresent()) { + Cache cache = cacheOptional.get(); + return new Gson().toJson(cache); + } else { + return "There is no cache with the ID " + cacheID; + } + } } } From 920a34920e798a48fa60c9c28cfcc8e5d6f2554f Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 24 Mar 2019 00:24:04 +0100 Subject: [PATCH 2/3] removed cacheAccesDefinition as a parameter --- .../hhn/labsw/bugageocaching/controller/Controller.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 25926d8..285eaf6 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -41,8 +41,7 @@ public class Controller { @RequestMapping("/startCache") public @ResponseBody String startCache(@RequestParam(value = "userID", defaultValue = "-1") String userID, - @RequestParam String cacheID, - @RequestParam String cacheAccesDefinitionID) { + @RequestParam String cacheID) { if (!userID.equals("-1")) { // ein angemeldeter User startet den Cache @@ -65,12 +64,12 @@ public class Controller { } Optional cacheAccesDefinitionOptional = - cacheAccesDefinitionRepository.findById(Integer.valueOf(cacheAccesDefinitionID)); + cacheAccesDefinitionRepository.findById(1); // bearbeitet if (cacheAccesDefinitionOptional.isPresent()) { CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); } else { - return "There is no cacheAccesDefinition with the ID " + cacheAccesDefinitionID; + return "There is no cacheAccesDefinition with the ID " + 1; } bearbeitetRepository.save(bearbeitet); From 8803f4419a63a9a160cb46aa3f3a119554f771ab Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 24 Mar 2019 06:31:35 +0100 Subject: [PATCH 3/3] startCache throws an IllegalParameterExcpetion if the method gets called with illegal parameters --- .../bugageocaching/controller/Controller.java | 15 +++-- .../exceptions/IllegalParameterException.java | 65 +++++++++++++++++++ 2 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 src/main/java/hhn/labsw/bugageocaching/exceptions/IllegalParameterException.java diff --git a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java index 285eaf6..81e8242 100644 --- a/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java +++ b/src/main/java/hhn/labsw/bugageocaching/controller/Controller.java @@ -2,6 +2,7 @@ package hhn.labsw.bugageocaching.controller; import com.google.gson.Gson; import hhn.labsw.bugageocaching.entities.*; +import hhn.labsw.bugageocaching.exceptions.IllegalParameterException; import hhn.labsw.bugageocaching.repositories.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -41,9 +42,9 @@ public class Controller { @RequestMapping("/startCache") public @ResponseBody String startCache(@RequestParam(value = "userID", defaultValue = "-1") String userID, - @RequestParam String cacheID) { + @RequestParam String cacheID) throws IllegalParameterException { - if (!userID.equals("-1")) { // ein angemeldeter User startet den Cache + if (!userID.equals("-1")) { // ein angemeldeter user startet den cache(es werden zwei parameter übergeben) Bearbeitet bearbeitet = new Bearbeitet(); @@ -52,7 +53,7 @@ public class Controller { User user = userOptional.get(); bearbeitet.setUser(user); } else { - return "There is no user with the ID " + userID; + throw new IllegalParameterException("There is no user with the ID " + userID); } Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); @@ -60,7 +61,7 @@ public class Controller { Cache cache = cacheOptional.get(); bearbeitet.setCache(cache); } else { - return "There is no cache with the ID " + cacheID; + throw new IllegalParameterException( "There is no cache with the ID " + cacheID); } Optional cacheAccesDefinitionOptional = @@ -69,20 +70,20 @@ public class Controller { CacheAccesDefinition cacheAccesDefinition = cacheAccesDefinitionOptional.get(); bearbeitet.setCacheAccesDefinition(cacheAccesDefinition); } else { - return "There is no cacheAccesDefinition with the ID " + 1; + throw new IllegalParameterException("There is no cacheAccesDefinition with the ID " + 1); } bearbeitetRepository.save(bearbeitet); return new Gson().toJson(bearbeitet); - } else { // kein angemeldeter User startet den Cache + } else { // kein angemeldeter User startet den cache(es wird nur der cache als parameter übergeben) Optional cacheOptional = cacheRepository.findById(Integer.valueOf(cacheID)); if (cacheOptional.isPresent()) { Cache cache = cacheOptional.get(); return new Gson().toJson(cache); } else { - return "There is no cache with the ID " + cacheID; + throw new IllegalParameterException( "There is no cache with the ID " + cacheID); } } } diff --git a/src/main/java/hhn/labsw/bugageocaching/exceptions/IllegalParameterException.java b/src/main/java/hhn/labsw/bugageocaching/exceptions/IllegalParameterException.java new file mode 100644 index 0000000..df77bae --- /dev/null +++ b/src/main/java/hhn/labsw/bugageocaching/exceptions/IllegalParameterException.java @@ -0,0 +1,65 @@ +package hhn.labsw.bugageocaching.exceptions; + + +/** + * Thrown to indicate that a method has been passed an illegal or inappropriate argument. + * This matches the IllegalArgumentException, but is no RuntimeException. This forces the + * developer to catch it. + *

+ */ +public class IllegalParameterException extends Exception { + /** + * Constructs a new exception with {@code null} as its detail message. + * The cause is not initialized, and may subsequently be initialized by a + * call to {@link #initCause}. + */ + public IllegalParameterException() { + } + + /** + * Constructs a new exception with the specified detail message. The + * cause is not initialized, and may subsequently be initialized by + * a call to {@link #initCause}. + * + * @param message the detail message. The detail message is saved for + * later retrieval by the {@link #getMessage()} method. + */ + public IllegalParameterException(final String message) { + super(message); + } + + /** + * Constructs a new exception with the specified detail message and + * cause.

Note that the detail message associated with + * {@code cause} is not automatically incorporated in + * this exception's detail message. + * + * @param message the detail message (which is saved for later retrieval + * by the {@link #getMessage()} method). + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A null value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + * @since 1.4 + */ + public IllegalParameterException(final String message, final Throwable cause) { + super(message, cause); + } + + /** + * Constructs a new exception with the specified cause and a detail + * message of (cause==null ? null : cause.toString()) (which + * typically contains the class and detail message of cause). + * This constructor is useful for exceptions that are little more than + * wrappers for other throwables. + * + * @param cause the cause (which is saved for later retrieval by the + * {@link #getCause()} method). (A null value is + * permitted, and indicates that the cause is nonexistent or + * unknown.) + * @since 1.4 + */ + public IllegalParameterException(final Throwable cause) { + super(cause); + } +}