Skip to content

Commit

Permalink
Merge pull request #28 from DigiLabChallengeHackathon/fix/response
Browse files Browse the repository at this point in the history
fix/response for checkdevice
  • Loading branch information
andrewkimswe authored Nov 30, 2024
2 parents 1c1c101 + 1fa49eb commit 9ea63bf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Optional;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -52,7 +53,12 @@ public ResponseEntity<ApiResponse<UserResponse>> checkDevice(@RequestBody @Valid
new UserResponse(user.get().getId(), user.get().getNickname())
));
}
throw new IllegalArgumentException("User does not exist.");
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(
"fail",
"User does not exist",
null
));
}

/**
Expand Down Expand Up @@ -87,7 +93,12 @@ public ResponseEntity<ApiResponse<UserResponse>> signup(@RequestBody @Valid Sign
public ResponseEntity<ApiResponse<UserResponse>> getUserInfo(@PathVariable String deviceId) {
User user = authService.getUserByDeviceId(deviceId);
if (user == null) {
throw new IllegalArgumentException("User not found.");
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(
"fail",
"User not found",
null
));
}
return ResponseEntity.ok(new ApiResponse<>(
"success",
Expand All @@ -102,8 +113,20 @@ public ResponseEntity<ApiResponse<UserResponse>> getUserInfo(@PathVariable Strin
* @return 업데이트된 사용자 정보
*/
@PatchMapping("/nickname")
public ResponseEntity<UserResponse> updateNickname(@RequestBody UpdateNicknameRequest request) {
public ResponseEntity<ApiResponse<UserResponse>> updateNickname(@RequestBody @Valid UpdateNicknameRequest request) {
if (request.getNewNickname() == null || request.getNewNickname().isEmpty()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ApiResponse<>(
"fail",
"Nickname cannot be empty",
null
));
}
User updatedUser = authService.updateNickname(request.getDeviceId(), request.getNewNickname());
return ResponseEntity.ok(new UserResponse(updatedUser.getId(), updatedUser.getNickname()));
return ResponseEntity.ok(new ApiResponse<>(
"success",
"Nickname updated successfully",
new UserResponse(updatedUser.getId(), updatedUser.getNickname())
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ public class GlobalExceptionHandler {
*/
@ExceptionHandler(IllegalArgumentException.class)
public ResponseEntity<ApiResponse<Void>> handleIllegalArgumentException(IllegalArgumentException ex) {
return ResponseEntity.badRequest().body(new ApiResponse<>(
"error",
ex.getMessage(),
null
));
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(new ApiResponse<>(
"fail",
ex.getMessage(),
null
));
}

/**
Expand Down

0 comments on commit 9ea63bf

Please sign in to comment.