diff --git a/src/main/java/likelion/MZConnent/api/member/MemberController.java b/src/main/java/likelion/MZConnent/api/member/MemberController.java index d4e54de..8eecfe0 100644 --- a/src/main/java/likelion/MZConnent/api/member/MemberController.java +++ b/src/main/java/likelion/MZConnent/api/member/MemberController.java @@ -1,15 +1,15 @@ package likelion.MZConnent.api.member; +import likelion.MZConnent.dto.member.request.UpdateMemberInfoRequest; import likelion.MZConnent.dto.member.response.MemberInfoResponse; +import likelion.MZConnent.dto.member.response.UpdateMemberInfoResponse; import likelion.MZConnent.jwt.principle.UserPrinciple; import likelion.MZConnent.service.member.MemberService; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.ResponseEntity; import org.springframework.security.core.annotation.AuthenticationPrincipal; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @Slf4j @RestController @@ -19,7 +19,7 @@ public class MemberController { // 나의 정보 조회 - @GetMapping("/api/user/me") + @GetMapping("/api/users/me") public ResponseEntity getMyInfo(@AuthenticationPrincipal UserPrinciple userPrinciple){ String email = userPrinciple.getEmail(); MemberInfoResponse memberInfo = memberService.getMemberInfoByEmail(email); @@ -30,7 +30,7 @@ public ResponseEntity getMyInfo(@AuthenticationPrincipal Use } // 타유저 정보 조회 - @GetMapping("/api/user/{userId}") + @GetMapping("/api/users/{userId}") public ResponseEntity getMemberInfo(@PathVariable("userId") Long userId){ MemberInfoResponse memberInfo = memberService.getMemberInfoById(userId); @@ -38,4 +38,12 @@ public ResponseEntity getMemberInfo(@PathVariable("userId") return ResponseEntity.ok(memberInfo); } -} \ No newline at end of file + + @PutMapping("/api/users/me") + public ResponseEntity updateMyInfo(@RequestBody UpdateMemberInfoRequest request, @AuthenticationPrincipal UserPrinciple userPrinciple){ + UpdateMemberInfoResponse response = memberService.updateMemberInfo(userPrinciple.getEmail(), request); + + return ResponseEntity.ok(response); + } +} + diff --git a/src/main/java/likelion/MZConnent/dto/member/request/UpdateMemberInfoRequest.java b/src/main/java/likelion/MZConnent/dto/member/request/UpdateMemberInfoRequest.java new file mode 100644 index 0000000..408109e --- /dev/null +++ b/src/main/java/likelion/MZConnent/dto/member/request/UpdateMemberInfoRequest.java @@ -0,0 +1,20 @@ +package likelion.MZConnent.dto.member.request; + +import likelion.MZConnent.domain.member.Age; +import likelion.MZConnent.domain.member.Gender; +import lombok.*; + +import java.util.List; + +@Getter +@ToString +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class UpdateMemberInfoRequest { + private String username; + private List selfIntroductions; + private String instagramId; + private String facebookId; + +} diff --git a/src/main/java/likelion/MZConnent/dto/member/response/UpdateMemberInfoResponse.java b/src/main/java/likelion/MZConnent/dto/member/response/UpdateMemberInfoResponse.java new file mode 100644 index 0000000..e8532ee --- /dev/null +++ b/src/main/java/likelion/MZConnent/dto/member/response/UpdateMemberInfoResponse.java @@ -0,0 +1,30 @@ +package likelion.MZConnent.dto.member.response; + +import likelion.MZConnent.domain.member.Member; +import likelion.MZConnent.dto.culture.CultureCategoryDto; +import lombok.*; + +import java.util.List; +import java.util.stream.Collectors; + +@Getter +@ToString +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class UpdateMemberInfoResponse { + private Long memberId; + private String username; + private String instagramId; + private String facebookId; + private List selfIntroductions; + + @Builder + public UpdateMemberInfoResponse(Member member) { + this.memberId = member.getId(); + this.username = member.getUsername(); + this.selfIntroductions = member.getSelfIntroductions().stream() + .map(selfIntroduction->new CultureCategoryDto(selfIntroduction.getCultureCategory())) + .collect(Collectors.toList()); + this.instagramId = member.getInstagramId(); + this.facebookId = member.getFacebookId(); + } +} diff --git a/src/main/java/likelion/MZConnent/repository/member/SelfIntroductionRepository.java b/src/main/java/likelion/MZConnent/repository/member/SelfIntroductionRepository.java index 0283e9a..07700f9 100644 --- a/src/main/java/likelion/MZConnent/repository/member/SelfIntroductionRepository.java +++ b/src/main/java/likelion/MZConnent/repository/member/SelfIntroductionRepository.java @@ -1,9 +1,17 @@ package likelion.MZConnent.repository.member; +import io.lettuce.core.dynamic.annotation.Param; +import jakarta.transaction.Transactional; import likelion.MZConnent.domain.member.SelfIntroduction; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @Repository public interface SelfIntroductionRepository extends JpaRepository { + @Modifying + @Transactional + @Query("DELETE FROM SelfIntroduction si WHERE si.member.email = :email") + void deleteByMemberEmail(@Param("email") String email); } diff --git a/src/main/java/likelion/MZConnent/service/member/MemberService.java b/src/main/java/likelion/MZConnent/service/member/MemberService.java index 832badd..682d286 100644 --- a/src/main/java/likelion/MZConnent/service/member/MemberService.java +++ b/src/main/java/likelion/MZConnent/service/member/MemberService.java @@ -1,28 +1,76 @@ package likelion.MZConnent.service.member; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import likelion.MZConnent.domain.culture.CultureCategory; import likelion.MZConnent.domain.member.Member; +import likelion.MZConnent.domain.member.SelfIntroduction; +import likelion.MZConnent.dto.member.request.UpdateMemberInfoRequest; import likelion.MZConnent.dto.member.response.MemberInfoResponse; +import likelion.MZConnent.dto.member.response.UpdateMemberInfoResponse; +import likelion.MZConnent.repository.culture.CultureCategoryRepository; import likelion.MZConnent.repository.member.MemberRepository; +import likelion.MZConnent.repository.member.SelfIntroductionRepository; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.List; +import java.util.stream.Collectors; + @RequiredArgsConstructor @Slf4j @Service @Transactional public class MemberService { private final MemberRepository memberRepository; + private final SelfIntroductionRepository selfIntroductionRepository; + private final CultureCategoryRepository cultureCategoryRepository; + + @PersistenceContext + private EntityManager entityManager; public MemberInfoResponse getMemberInfoByEmail(String email) { return new MemberInfoResponse(findMemberByEmail(email)); } + public MemberInfoResponse getMemberInfoById(Long id) { return new MemberInfoResponse(findMemberById(id)); } + + public UpdateMemberInfoResponse updateMemberInfo(String email, UpdateMemberInfoRequest request) { + log.info(request.getFacebookId()); + log.info(request.getInstagramId()); + + Member member = findMemberByEmail(email); + member.setUsername(request.getUsername()); + member.setInstagramId(request.getInstagramId()); + member.setFacebookId(request.getFacebookId()); + + member.getSelfIntroductions().clear(); + selfIntroductionRepository.deleteByMemberEmail(email); + + List selfIntroductions = request.getSelfIntroductions().stream() + .map((id) -> { + SelfIntroduction selfIntroduction = SelfIntroduction.builder() + .member(member) + .cultureCategory(findCultureCategoryById(id)) + .build(); + selfIntroductionRepository.save(selfIntroduction); + return selfIntroduction; + }).collect(Collectors.toList()); + + member.setSelfIntroductions(selfIntroductions); + + return UpdateMemberInfoResponse.builder() + .member(member) + .build(); + } + + private Member findMemberByEmail(String email) { return memberRepository.findByEmail(email).orElseThrow(() -> { log.info("계정이 존재하지 않음."); @@ -36,4 +84,13 @@ private Member findMemberById(Long id) { return new IllegalArgumentException("계정이 존재하지 않습니다."); }); } + + private CultureCategory findCultureCategoryById(Long id) { + return cultureCategoryRepository.findById(id).orElseThrow( + ()->{ + log.info("문화 카테고리가 존재하지 않음."); + return new IllegalArgumentException("문화 카테고리가 존재하지 않습니다."); + } + ); + } }