diff --git a/src/main/java/com/example/springtest/demo/resources/UserRest.java b/src/main/java/com/example/springtest/demo/resources/UserRest.java index e5ea139..3f9ad5c 100644 --- a/src/main/java/com/example/springtest/demo/resources/UserRest.java +++ b/src/main/java/com/example/springtest/demo/resources/UserRest.java @@ -14,7 +14,7 @@ @Path("/users") public class UserRest { - private Map users = new HashMap<>(); +// private Map users = new HashMap<>(); @Autowired // This means to get the bean called userRepository // Which is auto-generated by Spring, we will use it to handle the data @@ -57,7 +57,7 @@ public User createUser(@FormParam("name") String name, @FormParam("password") String password) { String uId = UUID.randomUUID().toString(); User user = new User(uId, name, password); - users.put(uId, user); +// users.put(uId, user); return userRepository.save(user); } @@ -65,11 +65,12 @@ @PUT @Produces(MediaType.APPLICATION_JSON) public User updateUser(@PathParam("uId") String uId, @Nullable @FormParam("name") String name, @Nullable @FormParam("password") String password) { - User user = users.get(uId); - if (user != null) { - user.setName(name); - user.setPassword(password); - return userRepository.save(user); // JSONが返る +// User user = users.get(uId); + Optional user = userRepository.findById(uId); + if (user.isPresent()) { + user.get().setName(name); + user.get().setPassword(password); + return userRepository.save(user.get()); // JSONが返る } else { // uIdが無ければ throw new WebApplicationException(404); // 404が返る } @@ -79,10 +80,10 @@ @DELETE @Produces(MediaType.APPLICATION_JSON) public User deleteUser(@PathParam("uId") String uId) { - User user = users.remove(uId); - if (user != null) { + Optional user = userRepository.findById(uId); + if (user.isPresent()) { userRepository.deleteById(uId); - return user; // JSONが返る + return user.get(); // JSONが返る } else { // uIdが無ければ throw new WebApplicationException(410); // 410が返る(GONE) }