diff options
Diffstat (limited to 'ufund-api/src/main')
6 files changed, 33 insertions, 22 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java index 15a741a..7773028 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/CupboardController.java @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -141,15 +142,17 @@ public class CupboardController { * @param need The need to update * @return OK response and the need if it was successful, or INTERNAL_SERVER_ERROR if there was an issue */ - @PutMapping("") - public ResponseEntity<Need> updateNeed(@RequestBody Need need) { + @PutMapping("/{id}") + public ResponseEntity<Need> updateNeed(@RequestBody Need need, @PathVariable int id) { try { - Need updatedNeed = cupboardService.updateNeed(need); + Need updatedNeed = cupboardService.updateNeed(need, id); if (updatedNeed != null) { return new ResponseEntity<>(need, HttpStatus.OK); } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } + } catch (InvalidParameterException ex) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java index 21cd1b3..0bb3fcf 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java @@ -1,6 +1,7 @@ package com.ufund.api.ufundapi.controller; import java.io.IOException; +import java.security.InvalidParameterException; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; @@ -97,7 +98,8 @@ public class UserController { } else { return new ResponseEntity<>(HttpStatus.NOT_FOUND); } - + } catch (InvalidParameterException ex) { + return new ResponseEntity<>(HttpStatus.BAD_REQUEST); } catch (IOException e) { return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); } catch (IllegalAccessException e) { diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java index 6558ce2..29d46cf 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserDAO.java @@ -49,15 +49,14 @@ public interface UserDAO { /** * Updates and saves a {@linkplain User user} * - * @param newUser {@link User user} object to be updated and saved - * @param name {@link String name} name of object to be updated + * @param user {@link User user} object to be updated and saved * * @return updated {@link User user} if successful, null if * {@link User user} could not be found * * @throws IOException if underlying storage cannot be accessed */ - User updateUser(User newUser, String name) throws IOException; + User updateUser(User user) throws IOException; /** * Deletes a {@linkplain User user} with the given id diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java index 97bc378..f17f8f2 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/persistence/UserFileDAO.java @@ -78,12 +78,12 @@ public class UserFileDAO implements UserDAO { } @Override - public User updateUser(User newUser, String name) throws IOException { + public User updateUser(User user) throws IOException { synchronized (users) { - if (users.containsKey(name)) { - users.put(name, newUser); + if (users.containsKey(user.getUsername())) { + users.put(user.getUsername(), user); save(); - return newUser; + return user; } else { return null; } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java index 15f8442..c8609ab 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/CupboardService.java @@ -78,13 +78,17 @@ public class CupboardService { } /** - * Modify a need - * // TODO - * @param need - * @return + * Updates a need + * + * @param id The ID of the need to update + * @param need The need object to set (note: the ID is ignored) + * @return The updated need object * @throws IOException Thrown if there was an issue saving the changes */ - public Need updateNeed(Need need) throws IOException { + public Need updateNeed(Need need, int id) throws IOException { + if (need.getId() != id) { + throw new IllegalArgumentException("ID in URL and body must match"); + } return cupboardDAO.updateNeed(need); } diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java index 776d09a..935ee72 100644 --- a/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java +++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/service/UserService.java @@ -45,14 +45,17 @@ public class UserService { /** * Updates a user - * // TODO - * @param user - * @param name - * @return + * + * @param user The ID of the user to update + * @param username The user object to set (note: the ID is ignored) + * @return The updated user object * @throws IOException Thrown if there was any issue saving the data */ - public User updateUser(User user, String name) throws IOException { - return userDAO.updateUser(user, name); + public User updateUser(User user, String username) throws IOException { + if (!user.getUsername().equals(username)) { + throw new IllegalArgumentException("ID in URL and body must match"); + } + return userDAO.updateUser(user); } /** |