Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public enum ErrorCode {
USER_DO_NOT_HAVE_GPA(HttpStatus.BAD_REQUEST.value(), "해당 유저의 학점을 찾을 수 없음"),
REJECTED_REASON_REQUIRED(HttpStatus.BAD_REQUEST.value(), "거절 사유가 필요합니다."),

// database
DATA_INTEGRITY_VIOLATION(HttpStatus.CONFLICT.value(), "데이터베이스 무결성 제약조건 위반이 발생했습니다."),

// general
JSON_PARSING_FAILED(HttpStatus.BAD_REQUEST.value(), "JSON 파싱을 할 수 없습니다."),
JWT_EXCEPTION(HttpStatus.BAD_REQUEST.value(), "JWT 토큰을 처리할 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.solidconnection.siteuser.controller;

import com.example.solidconnection.common.exception.CustomException;
import com.example.solidconnection.common.exception.ErrorCode;
import com.example.solidconnection.common.resolver.AuthorizedUser;
import com.example.solidconnection.siteuser.domain.SiteUser;
import com.example.solidconnection.siteuser.dto.MyPageResponse;
import com.example.solidconnection.siteuser.service.MyPageService;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
Comment thread
nayonsoso marked this conversation as resolved.
Outdated
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
Expand All @@ -13,6 +16,8 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import static com.example.solidconnection.common.exception.ErrorCode.DATA_INTEGRITY_VIOLATION;

@RequiredArgsConstructor
@RequestMapping("/my")
@RestController
Expand All @@ -34,7 +39,21 @@ public ResponseEntity<Void> updateMyPageInfo(
@RequestParam(value = "file", required = false) MultipartFile imageFile,
@RequestParam(value = "nickname", required = false) String nickname
) {
myPageService.updateMyPageInfo(siteUser, imageFile, nickname);
return ResponseEntity.ok().build();
try {
myPageService.updateMyPageInfo(siteUser, imageFile, nickname);
return ResponseEntity.ok().build();
} catch (DataIntegrityViolationException e) {
throw new CustomException(determineErrorCode(e));
}
Comment thread
whqtker marked this conversation as resolved.
Outdated
Comment thread
whqtker marked this conversation as resolved.
Outdated
}

private ErrorCode determineErrorCode(DataIntegrityViolationException e) {
String message = e.getMostSpecificCause().getMessage().toLowerCase();

if (message.contains("unique") && message.contains("nickname")) {
return ErrorCode.NICKNAME_ALREADY_EXISTED;
}

return ErrorCode.DATA_INTEGRITY_VIOLATION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.List;

import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_CHANGE_NICKNAME_YET;
import static com.example.solidconnection.common.exception.ErrorCode.NICKNAME_ALREADY_EXISTED;

@RequiredArgsConstructor
@Service
Expand Down Expand Up @@ -48,7 +47,6 @@ public MyPageResponse getMyPageInfo(SiteUser siteUser) {
@Transactional
public void updateMyPageInfo(SiteUser siteUser, MultipartFile imageFile, String nickname) {
if (nickname != null) {
validateNicknameUnique(nickname);
validateNicknameNotChangedRecently(siteUser.getNicknameModifiedAt());
siteUser.setNickname(nickname);
siteUser.setNicknameModifiedAt(LocalDateTime.now());
Expand All @@ -65,12 +63,6 @@ public void updateMyPageInfo(SiteUser siteUser, MultipartFile imageFile, String
siteUserRepository.save(siteUser);
}

private void validateNicknameUnique(String nickname) {
if (siteUserRepository.existsByNickname(nickname)) {
throw new CustomException(NICKNAME_ALREADY_EXISTED);
}
}

Comment thread
whqtker marked this conversation as resolved.
private void validateNicknameNotChangedRecently(LocalDateTime lastModifiedAt) {
if (lastModifiedAt == null) {
return;
Expand Down