Skip to content

Commit

Permalink
Merge pull request #107 from ttoklip/Fix/12-mypage-privacy
Browse files Browse the repository at this point in the history
fix: �개인정보 생성 및 수정시 오류 해결
  • Loading branch information
toychip authored Feb 19, 2024
2 parents f56062d + a48b837 commit 3f0f037
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public SuccessResponse<MyPageResponse> getMyProfile() {
)))})
@PatchMapping("/edit")
public SuccessResponse<Message> edit(@ModelAttribute @Validated final PrivacyCreateRequest request) {
Message message = profileService.insert(request);
Message message = profileService.edit(request);
return new SuccessResponse<>(message);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import com.api.ttoklip.domain.privacy.domain.Interest;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface InterestRepository extends JpaRepository <Interest, Long> {
@Modifying
@Query("DELETE FROM Interest i WHERE i.member.id = :memberId")
void deleteAllByMemberId(@Param("memberId") Long memberId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,58 @@ public Long register(final Profile profile) {
// ------------- 회원 가입 후 입력 받을 닉네임, 우리동네 설정, 나의 동릭 경험, 관심 카테고리 선택 -------------
@Transactional
public Message insert(final PrivacyCreateRequest request) {
Member currentMember = insertPrivacy(request);

// 카테고리 없을 경우 에러 처리
if (request.getCategories() == null && request.getCategories().isEmpty()) {
throw new ApiException(ErrorType.CATEGORY_NOT_EXISTS);
}

registerInterest(request, currentMember);
return Message.insertPrivacy();
}

private Member insertPrivacy(final PrivacyCreateRequest request) {
Member currentMember = memberService.findByIdWithProfile(getCurrentMember().getId());

updateProfileImage(request, currentMember);

MemberEditor editor = getEditor(currentMember, request);
currentMember.insertPrivacy(editor);
registerInterest(request, currentMember);
return currentMember;
}

// ------------- 회원 가입 후 입력 받을 닉네임, 우리동네 설정, 나의 동릭 경험, 관심 카테고리 선택 -------------
private void registerInterest(final PrivacyCreateRequest request, final Member currentMember) {
interestRepository.deleteAllByMemberId(getCurrentMember().getId());

List<Category> requestCategories = request.getCategories();
List<Interest> interests = requestCategories
.stream()
.map(category -> Interest.of(currentMember, category))
.toList();

interestRepository.saveAll(interests);
}

@Transactional
public Message edit(final PrivacyCreateRequest request) {
Member currentMember = insertPrivacy(request);

// 실제 존재할 때만 사용
if (request.getCategories() != null && !request.getCategories().isEmpty()) {
registerInterest(request, currentMember);
}

return Message.insertPrivacy();
}

private void updateProfileImage(final PrivacyCreateRequest request, final Member currentMember) {
// 프로필 이미지 URL 변경
MultipartFile profileImage = request.getProfileImage();
String uploadUrl = s3FileUploader.uploadMultipartFile(profileImage);
currentMember.getProfile().changeProfile(uploadUrl);
if (profileImage != null && !profileImage.isEmpty()) {
String uploadUrl = s3FileUploader.uploadMultipartFile(profileImage);
currentMember.getProfile().changeProfile(uploadUrl);
}
}

private MemberEditor getEditor(final Member currentMember, final PrivacyCreateRequest request) {
Expand All @@ -72,15 +109,6 @@ private MemberEditor getEditor(final Member currentMember, final PrivacyCreateRe
.build();
}

private void registerInterest(final PrivacyCreateRequest request, final Member currentMember) {
List<Category> requestCategories = request.getCategories();
List<Interest> interests = requestCategories
.stream()
.map(category -> Interest.of(currentMember, category))
.toList();
interestRepository.saveAll(interests);
}

// ------------- 회원 가입 후 입력 받을 닉네임, 우리동네 설정, 나의 동릭 경험, 관심 카테고리 선택 끝 -------------


Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/api/ttoklip/global/exception/ErrorType.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public enum ErrorType {

// ------------------------------------------ Category ------------------------------------------
CATEGORY_NOT_FOUND(NOT_FOUND, "Category_4040", "카테고리를 찾을 수 없습니다."),
CATEGORY_NOT_EXISTS(NOT_FOUND, "Category_4041", "카테고리는 필수로 선택해야합니다."),



// ------------------------------------------ Report ------------------------------------------
Expand Down

0 comments on commit 3f0f037

Please sign in to comment.