Skip to content

Commit

Permalink
Merge pull request #63 from mjuCapstone/feat/#62
Browse files Browse the repository at this point in the history
회원 정보 조회 시 응답 수정
  • Loading branch information
jimizip authored Aug 14, 2024
2 parents f036d12 + 8f51cca commit 0dfab86
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
import com.mju.capstone.global.security.util.SecurityUtil;
import com.mju.capstone.member.dto.request.MemberUpdateRequest;
import com.mju.capstone.member.dto.response.MemberUpdateResponse;
import com.mju.capstone.member.entity.Member;
import com.mju.capstone.member.service.MemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/v1")
Expand All @@ -32,7 +31,7 @@ public ResponseEntity<?> getMemberInfo() {
return ResponseEntity.status(HttpStatus.OK)
.body(ControllerMessage.of(
SuccessMessage.OK,
memberService.findByEmail(SecurityUtil.getLoginUserEmail()))
memberService.findMemberInfoByEmail(SecurityUtil.getLoginUserEmail()))
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.mju.capstone.member.dto.response;

import com.mju.capstone.auth.repository.entity.Role;
import com.mju.capstone.global.security.util.SecurityUtil;
import com.mju.capstone.member.entity.Member;
import com.mju.capstone.member.repository.MemberRepository;
import io.swagger.v3.oas.annotations.media.Schema;

@Schema(description = "사용자 정보 조회 응답")
public record MemberInfoResponse(
Long memberId,
String email,
Role role,
String nickName,
int birth,
int height,
int weight,
int level,
int dietLevel) {

public static MemberInfoResponse from(Long memberId, String email, Role role, String nickName, int birth, int height, int weight, int level, int dietLevel) {
return new MemberInfoResponse(memberId, email, role, nickName, birth, height, weight, level, dietLevel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ public record MemberUpdateResponse(
int height,
int weight,
int level,
int dietLevel
) {
int dietLevel) {
public static MemberUpdateResponse fromMember(Member member) {
return new MemberUpdateResponse(member.getId(),member.getNickname(),member.getBirth(),
member.getHeight(), member.getWeight(), member.getLevel(), member.getDietPlan());
}

}
14 changes: 12 additions & 2 deletions src/main/java/com/mju/capstone/member/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mju.capstone.global.security.util.SecurityUtil;
import com.mju.capstone.member.dto.request.MemberUpdateRequest;
import com.mju.capstone.member.dto.request.NutritionCalculatorRequest;
import com.mju.capstone.member.dto.response.MemberInfoResponse;
import com.mju.capstone.member.dto.response.MemberUpdateResponse;
import com.mju.capstone.member.entity.Member;
import com.mju.capstone.member.repository.MemberRepository;
Expand All @@ -28,12 +29,19 @@ public class MemberService {

public Member findByEmail(String email) {
return memberRepository.findByEmail(email)
.orElseThrow(() -> new NotFoundException(ErrorMessage.MEMBER_NOT_FOUND));
.orElseThrow(() -> new NotFoundException(ErrorMessage.MEMBER_NOT_FOUND));
}

public MemberInfoResponse findMemberInfoByEmail(String email) {
Member member = findByEmail(email);
return MemberInfoResponse.from(member.getId(), member.getEmail(), member.getRole(), member.getNickname(), member.getBirth(), member.getHeight(), member.getWeight(), member.getLevel(), member.getDietPlan());
}

@Transactional
public MemberUpdateResponse updateMemberInfo(MemberUpdateRequest updateRequest) {
Member member = findByEmail(SecurityUtil.getLoginUserEmail());
MemberInfoResponse memberInfo = findMemberInfoByEmail(SecurityUtil.getLoginUserEmail());

Member member = findByEmail(memberInfo.email());

if (updateRequest.password().isPresent()) {
String hashedPassword = passwordEncoder.encode(updateRequest.password().get());
Expand All @@ -55,11 +63,13 @@ public MemberUpdateResponse updateMemberInfo(MemberUpdateRequest updateRequest)
if (requiresRecalculation) {
publishRecalculationEvent(member);
}

return MemberUpdateResponse.fromMember(member);
}

private void publishRecalculationEvent(Member member) {
NutritionCalculatorRequest calculatorRequest = NutritionCalculatorRequest.fromMember(member);
publisher.publishEvent(new CalculationEvent(this, calculatorRequest));
}

}

0 comments on commit 0dfab86

Please sign in to comment.