Skip to content

Commit

Permalink
Merge pull request #54 from LIKELION-TEAM4-HACKATHON/feature/user-info
Browse files Browse the repository at this point in the history
나의 정보 수정 기능
  • Loading branch information
oosedus authored Jul 26, 2024
2 parents b21d42e + d13f082 commit bfb6916
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/main/java/likelion/MZConnent/api/member/MemberController.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +19,7 @@ public class MemberController {


// 나의 정보 조회
@GetMapping("/api/user/me")
@GetMapping("/api/users/me")
public ResponseEntity<MemberInfoResponse> getMyInfo(@AuthenticationPrincipal UserPrinciple userPrinciple){
String email = userPrinciple.getEmail();
MemberInfoResponse memberInfo = memberService.getMemberInfoByEmail(email);
Expand All @@ -30,12 +30,20 @@ public ResponseEntity<MemberInfoResponse> getMyInfo(@AuthenticationPrincipal Use
}

// 타유저 정보 조회
@GetMapping("/api/user/{userId}")
@GetMapping("/api/users/{userId}")
public ResponseEntity<MemberInfoResponse> getMemberInfo(@PathVariable("userId") Long userId){
MemberInfoResponse memberInfo = memberService.getMemberInfoById(userId);

log.info("멤버 정보 조회 성공: {}", memberInfo.getUsername());

return ResponseEntity.ok(memberInfo);
}
}

@PutMapping("/api/users/me")
public ResponseEntity<UpdateMemberInfoResponse> updateMyInfo(@RequestBody UpdateMemberInfoRequest request, @AuthenticationPrincipal UserPrinciple userPrinciple){
UpdateMemberInfoResponse response = memberService.updateMemberInfo(userPrinciple.getEmail(), request);

return ResponseEntity.ok(response);
}
}

Original file line number Diff line number Diff line change
@@ -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<Long> selfIntroductions;
private String instagramId;
private String facebookId;

}
Original file line number Diff line number Diff line change
@@ -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<CultureCategoryDto> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<SelfIntroduction, Long> {
@Modifying
@Transactional
@Query("DELETE FROM SelfIntroduction si WHERE si.member.email = :email")
void deleteByMemberEmail(@Param("email") String email);
}
57 changes: 57 additions & 0 deletions src/main/java/likelion/MZConnent/service/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -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<SelfIntroduction> 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("계정이 존재하지 않음.");
Expand All @@ -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("문화 카테고리가 존재하지 않습니다.");
}
);
}
}

0 comments on commit bfb6916

Please sign in to comment.