diff --git a/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java b/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java index ddea638..0f585e3 100644 --- a/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java +++ b/src/main/java/org/ntlab/tampoposerver/resources/UsersResource.java @@ -12,6 +12,8 @@ import java.util.ArrayList; import java.util.List; + +import static io.micrometer.common.util.StringUtils.isBlank; /* //6/10報告:全部できた(friendまで)、UserRepositoryとの連携ができていない(updateあります?)、responseがない //6/17報告:すべてのユーザーの情報を持ってくるところ、フレンドペアの自分じゃない方のuidを返すところ、フレンドペアの削除はもらったものを連携できていない @@ -73,13 +75,21 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response createUser(@FormParam("user-id") String userId, @FormParam("password") String password) { + if (isBlank(userId) || isBlank(password)) { + throw new WebApplicationException( + Response.status(Response.Status.BAD_REQUEST) + .entity("ユーザーIDおよびパスワードを入力してください") + .build()); + } + //ユーザーがいるか調べる - User user = userRepository.getUser(userId); - if (userRepository.getUser(userId) != null) { + User existingUser = userRepository.getUser(userId); + if (existingUser != null) { return Response.status(Response.Status.CONFLICT) .entity("すでにユーザーが存在しています") .build(); } + User newUser = userRepository.addUser(userId, password); //6/12ここはswaggerではなくコードを仕様にすると決定しました。 @@ -143,6 +153,22 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public Response login(@PathParam("user-id") String userId, @FormParam("password") String password) { + + + if (userId == null || userId.isBlank()) { + throw new WebApplicationException( + Response.status(Response.Status.BAD_REQUEST) + .entity("user-id は必須です") + .build()); + } + + if (password == null || password.isBlank()) { + throw new WebApplicationException( + Response.status(Response.Status.BAD_REQUEST) + .entity("password は必須です") + .build()); + } + //存在チェック User user = userRepository.getUser(userId); if (user == null) { @@ -224,6 +250,15 @@ if (user == null) { throw new WebApplicationException(Response.Status.NOT_FOUND); } + + if (token == null || !token.equals(user.getToken())) { + throw new WebApplicationException( + Response.status(Response.Status.FORBIDDEN) + .entity("認証失敗") + .build() + ); + } + return Response.ok(user.getPassword(), MediaType.APPLICATION_JSON).build(); } @@ -231,43 +266,12 @@ @PUT @Path("/{user-id}/password") @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public Response updatePassword(@PathParam("user-id") String userId, @FormParam("new-password") String newPassword) { + public Response updatePassword(@PathParam("user-id") String userId, @FormParam("new-password") String newPassword, @FormParam("token") String token) { - User user = userRepository.getUser(userId); - if (user == null) { - throw new WebApplicationException( - Response.status(Response.Status.NOT_FOUND) - .entity("ユーザーが存在しません") - .build() - ); + if (newPassword == null || newPassword.isBlank()) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); } - - //パスワードのアップデート - user.setPassword(newPassword); - return Response.ok().build(); - - } - - - //単一アカウントのemailの取得 - @GET - @Path("/{user-id}/email") - @Produces(MediaType.APPLICATION_JSON) - public Response getEmail(@PathParam("user-id") String userId) { - User user = userRepository.getUser(userId); - if (user == null) { - throw new WebApplicationException(Response.Status.NOT_FOUND); - } - return Response.ok(user.getEmail(), MediaType.APPLICATION_JSON).build(); - } - - //指定されたIDのemailを変更する - @PUT - @Path("/{user-id}/email") - @Consumes(MediaType.APPLICATION_FORM_URLENCODED) - public Response updateEmail(@PathParam("user-id") String userId, @FormParam("new-email") String newEmail, @FormParam("token") String token) { - User user = userRepository.getUser(userId); if (user == null) { throw new WebApplicationException( @@ -285,6 +289,69 @@ ); } + + //パスワードのアップデート + user.setPassword(newPassword); + return Response.ok().build(); + + } + + + //単一アカウントのemailの取得 + @GET + @Path("/{user-id}/email") + @Produces(MediaType.APPLICATION_JSON) + public Response getEmail(@PathParam("user-id") String userId, @QueryParam("token") String token) { + + User user = userRepository.getUser(userId); + if (user == null) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + if (token == null || !token.equals(user.getToken())) { + throw new WebApplicationException( + Response.status(Response.Status.FORBIDDEN) + .entity("認証失敗") + .build() + ); + } + return Response.ok(user.getEmail(), MediaType.APPLICATION_JSON).build(); + } + + //指定されたIDのemailを変更する + @PUT + @Path("/{user-id}/email") + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public Response updateEmail(@PathParam("user-id") String userId, @FormParam("new-email") String newEmail, @FormParam("token") String token) { + + if (newEmail == null || newEmail.isBlank()) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); + } + + + User user = userRepository.getUser(userId); + if (user == null) { + throw new WebApplicationException( + Response.status(Response.Status.NOT_FOUND) + .entity("ユーザーが存在しません") + .build() + ); + } + + if (!newEmail.equals(user.getEmail())) { + throw new WebApplicationException(Response.Status.CONFLICT); + } + + if (token == null || !token.equals(user.getToken())) { + throw new WebApplicationException( + Response.status(Response.Status.FORBIDDEN) + .entity("認証失敗") + .build() + ); + } + + + //emailアップデート user.setEmail(newEmail); return Response.ok(user.getEmail(), MediaType.APPLICATION_JSON).build(); @@ -309,6 +376,10 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String updateIcon(@PathParam("user-id") String userId, @FormParam("new-icon") String newIcon, @FormParam("token") String token) { + if ( newIcon == null || newIcon.isBlank() ) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); + } + User user = userRepository.getUser(userId); if (user == null) { throw new WebApplicationException( @@ -372,6 +443,11 @@ @Path("/{user-id}/friends/{pair-id}") @Produces(MediaType.APPLICATION_JSON) public Response getPairId(@PathParam("user-id") String userId, @QueryParam("token") String token, @PathParam("pair-id") String pairId) { + + if (pairId == null || pairId.isBlank()) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); + } + User user = userRepository.getUser(userId); if (user == null) { throw new WebApplicationException( @@ -400,7 +476,11 @@ @DELETE @Path("/{user-id}/friends/{pair-id}") @Produces(MediaType.APPLICATION_JSON) - public Response deleteFriends(@PathParam("user-id") String userId, @PathParam("pair-id") int pairId, @QueryParam("token") String token) { + public Response deleteFriends(@PathParam("user-id") String userId, @PathParam("pair-id") Integer pairId, @QueryParam("token") String token) { + + if (pairId == null || pairId <= 0) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); + } User user = userRepository.getUser(userId); if (user == null) { throw new NotFoundException("IDが存在しません");