Skip to content

Commit

Permalink
Refactor : 멤버 컨트롤러/서비스 코드 수정 및 예외처리 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Suhun0331 committed Jul 29, 2024
1 parent cba510f commit cd1bd24
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.springframework.web.bind.annotation.RestControllerAdvice;
import umc.kkijuk.server.common.domian.exception.*;
import umc.kkijuk.server.common.domian.response.ErrorResponse;
import umc.kkijuk.server.member.domain.exception.ConfirmPasswordMismatchException;

@RestControllerAdvice
@RequiredArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public class MemberController {
summary = "회원가입 요청",
description = "회원가입 요청을 받아 성공/실패 여부를 반환합니다.")
@PostMapping
public ResponseEntity<CreateMemberResponse> saveMember(
@RequestBody @Valid MemberJoinDto memberJoinDto
) {
public ResponseEntity<CreateMemberResponse> saveMember(@RequestBody @Valid MemberJoinDto memberJoinDto) {
Member joinMember = memberService.join(memberJoinDto);

return ResponseEntity
Expand All @@ -48,67 +46,42 @@ public ResponseEntity<CreateMemberResponse> saveMember(
description = "마이페이지에서 내 정보들을 가져옵니다.")
@GetMapping("/myPage/info")
public ResponseEntity<MemberInfoResponse> getInfo() {
try {
Long loginUser = LoginUser.get().getId();
Member member = memberService.getMemberInfo(loginUser);
MemberInfoResponse response = new MemberInfoResponse(
member.getEmail(),
member.getName(),
member.getPhoneNumber(),
member.getBirthDate()
);
return ResponseEntity.ok(response);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
Long loginUser = LoginUser.get().getId();
MemberInfoResponse memberInfoResponse = memberService.getMemberInfo(loginUser);
return ResponseEntity
.status(HttpStatus.OK)
.body(memberInfoResponse);
}

@Operation(
summary = "내 정보 수정",
description = "내 정보 수정 요청을 받아 성공/실패를 반환합니다.")
@PutMapping("/myPage/info")
public ResponseEntity<ResultResponse> changeMemberInfo(@RequestBody MemberInfoChangeDto memberInfoChangeDto) {
public ResponseEntity<Boolean> changeMemberInfo(@RequestBody @Valid MemberInfoChangeDto memberInfoChangeDto) {
Long loginUser = LoginUser.get().getId();
try {
memberService.updateMemberInfo(loginUser, memberInfoChangeDto);
return ResponseEntity.ok()
.body(new ResultResponse("information update success"));
}catch (Exception e){
System.out.println("e.getMessage() = " + e.getMessage());
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(new ResultResponse("information update failed"));
}

memberService.updateMemberInfo(loginUser, memberInfoChangeDto);
return ResponseEntity.ok(Boolean.TRUE);
}

@Operation(
summary = "관심분야 조회",
description = "마이페이지에서 관심분야를 조회합니다.")
@GetMapping("/myPage/field")
public ResponseEntity<MemberFieldResponse> getField() {
try {
Long loginUser = LoginUser.get().getId();
List<String> memberField = memberService.getMemberField(loginUser);
return ResponseEntity.ok().body(new MemberFieldResponse(memberField));
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
Long loginUser = LoginUser.get().getId();
List<String> memberField = memberService.getMemberField(loginUser);
return ResponseEntity.ok().body(new MemberFieldResponse(memberField));
}

@Operation(
summary = "관심분야 등록/수정",
description = "초기/마이페이지에서 관심분야를 등록/수정합니다.")
@PostMapping({"/field", "/myPage/field"})
public ResponseEntity<MemberFieldResponse> postField(@RequestBody @Valid MemberFieldDto memberFieldDto) {
try {
Long loginUserId = LoginUser.get().getId();
List<String> updatedMember = memberService.updateMemberField(loginUserId, memberFieldDto);
MemberFieldResponse response = new MemberFieldResponse(updatedMember);
return ResponseEntity.ok(response);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
public ResponseEntity<Boolean> postField(@RequestBody MemberFieldDto memberFieldDto) {

Long loginUserId = LoginUser.get().getId();
memberService.updateMemberField(loginUserId, memberFieldDto);
return ResponseEntity.ok(Boolean.TRUE);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package umc.kkijuk.server.member.controller.response;

import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;
Expand All @@ -14,6 +15,7 @@ public class MemberInfoResponse {
public MemberInfoResponse() {
}

@Builder
public MemberInfoResponse(String email, String name, String phoneNumber, LocalDate birthDate) {
this.email = email;
this.name = name;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package umc.kkijuk.server.common.domian.exception;
package umc.kkijuk.server.member.domain.exception;

public class ConfirmPasswordMismatchException extends RuntimeException {
public ConfirmPasswordMismatchException() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package umc.kkijuk.server.member.domain.exception;

public class FieldUpdateException extends RuntimeException{
public FieldUpdateException() {super("Field update fail.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package umc.kkijuk.server.member.domain.exception;

public class InvalidMemberDataException extends RuntimeException{
public InvalidMemberDataException() {super("MemberData cannot be null.");
}
}
50 changes: 32 additions & 18 deletions src/main/java/umc/kkijuk/server/member/service/MemberService.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package umc.kkijuk.server.member.service;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.member.domain.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.member.controller.response.CreateMemberResponse;
import umc.kkijuk.server.member.controller.response.MemberInfoResponse;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.domain.exception.FieldUpdateException;
import umc.kkijuk.server.member.domain.exception.InvalidMemberDataException;
import umc.kkijuk.server.member.dto.MemberFieldDto;
import umc.kkijuk.server.member.dto.MemberInfoChangeDto;
import umc.kkijuk.server.member.dto.MemberJoinDto;
Expand All @@ -22,7 +22,7 @@
public class MemberService {
private final MemberJpaRepository memberJpaRepository;

public Member findOne(Long memberId) {
public Member getById(Long memberId) {
return memberJpaRepository.findById(memberId)
.orElseThrow(() -> new ResourceNotFoundException("Member", memberId));
}
Expand Down Expand Up @@ -50,31 +50,45 @@ public Member join(MemberJoinDto memberJoinDto) {
// return member.getId();
// }

public Member getMemberInfo(Long memberId) {
return memberJpaRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));
public MemberInfoResponse getMemberInfo(Long memberId) {
Member member = this.getById(memberId);

if(member.getEmail() == null || member.getName() == null || member.getPhoneNumber() == null || member.getBirthDate() == null){
throw new InvalidMemberDataException();
}

return MemberInfoResponse.builder()
.email(member.getEmail())
.name(member.getName())
.phoneNumber(member.getPhoneNumber())
.birthDate(member.getBirthDate())
.build();
}

public List<String> getMemberField(Long memberId){
Member member = memberJpaRepository.findById(memberId)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));
Member member = this.getById(memberId);
return member.getField();
}

@Transactional
public List<String> updateMemberField(Long id,MemberFieldDto memberFieldDto){
Member member = memberJpaRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));
public Member updateMemberField(Long memberId,MemberFieldDto memberFieldDto){
Member member = this.getById(memberId);
member.changeFieldInfo(memberFieldDto.getField());
return member.getField();

if(!member.getField().equals(memberFieldDto.getField())){
throw new FieldUpdateException();
}
return member;
}

@Transactional
public Long updateMemberInfo(Long id,MemberInfoChangeDto memberInfoChangeDto){
Member member = memberJpaRepository.findById(id)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));
public Member updateMemberInfo(Long memberId, MemberInfoChangeDto memberInfoChangeDto){
Member member = this.getById(memberId);
member.changeMemberInfo(memberInfoChangeDto.getPhoneNumber(), memberInfoChangeDto.getBirthDate(), memberInfoChangeDto.getMarketingAgree());
return member.getId();
if(member.getPhoneNumber() == null || member.getBirthDate() == null || member.getMarketingAgree() == null){
throw new InvalidMemberDataException();
}
return member;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package umc.kkijuk.server.member.service;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -11,15 +9,14 @@
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.member.domain.exception.ConfirmPasswordMismatchException;
import umc.kkijuk.server.member.domain.MarketingAgree;
import umc.kkijuk.server.member.domain.Member;
import umc.kkijuk.server.member.domain.State;
import umc.kkijuk.server.member.dto.MemberJoinDto;
import umc.kkijuk.server.member.repository.MemberJpaRepository;

import java.time.LocalDate;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down

0 comments on commit cd1bd24

Please sign in to comment.