Skip to content

Commit

Permalink
refactor(profile): 프로필 업데이트 코드 리팩토링 (#61)
Browse files Browse the repository at this point in the history
* refactor(path): endpoint 변경 mypage -> profile

개요

- REST API에 조금 더 맞는 엔드포인트로 변경하고자, mypage에서 profile로
변경

- 특히나, 백엔드는 페이지와는 연관 없이 정보를 주는 것이기 때문에
page라는 이름 제외

- 추가로, 통일성을 위해, 앞에 root path에 /를 붙임

* refactor(profile): MemberUpdateDto 생성

개요

- Member의 일부 정보를 업데이트할 목적으로 MemberUpdateDto를 사용하는데,
불필요한 정보는 제외한다.

- 패스워드는 아직 디자인이 나온 것이 없어서, 다른 설정 페이지에서
이루어질 수도 있기 때문에 우선 제외

- is_korean은 유저가 업데이트하면 안되는 정보이기에 제외

- major 변경 있을 수 있기 때문에 추가

- 추가로, Builder 패턴을 사용하지 않으므로, @builder 제거

* refactor(profile): 프로필 업데이트 코드 Refactor

개요
- `MemberController`의 `editProfile`
  - `MemberDto`는 현재 멤버의 다양한 정보를 담고 있으므로, 업데이트의 범위를
넘어선 데이터를 가지고 있다. 따라서, `MemberUpdateDto`를 활용해서
Response와 Request Dto용도로 사용한다
  - `Authentication auth`를 사용하여, 현재 로그인 정보를 조금 더 간결하게
가져온다
  - `modelMapper`를 사용해서 Dto와 member의 변환을 쉽게하여 return 한다

- `MemberService`의 `updateMember`
  - 현재 코드에 의하면, MemberController의 getMember() 함수에서 이미
회원 체킹이 들어간다. 따라서, 중복 처리된 검사문을 제거한다.
  - 변경 감지로 인해, set함수만 사용하여 변경을 저장한다.
  • Loading branch information
seungholee-dev authored Apr 10, 2024
1 parent a5b5557 commit bd55cd4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@

import com.dife.api.model.Member;
import com.dife.api.model.dto.MemberDto;
import com.dife.api.model.dto.MemberUpdateDto;
import com.dife.api.model.dto.RegisterRequestDto;
import com.dife.api.model.dto.VerifyEmailDto;
import com.dife.api.repository.MemberRepository;
import com.dife.api.service.MemberService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.modelmapper.ModelMapper;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
Expand All @@ -26,13 +28,12 @@

@RestController
@RequiredArgsConstructor
@RequestMapping("api/members")
@RequestMapping("/api/members")
@Slf4j
public class MemberController {

@Autowired
private MemberRepository memberRepository;
private final MemberService memberService;
private final ModelMapper modelMapper;

@PostMapping("/register")
public ResponseEntity<RegisterRequestDto> register(@Valid @RequestBody RegisterRequestDto request) {
Expand All @@ -42,7 +43,7 @@ public ResponseEntity<RegisterRequestDto> register(@Valid @RequestBody RegisterR
.body(new RegisterRequestDto(request));
}

@GetMapping("/mypage")
@GetMapping("/profile")
public ResponseEntity<MemberDto> profile()
{
String memberEmail = SecurityContextHolder.getContext().getAuthentication().getName();
Expand All @@ -52,15 +53,14 @@ public ResponseEntity<MemberDto> profile()
return ResponseEntity.ok(memberDto);
}

@PutMapping("/mypage")
public ResponseEntity<MemberDto> editProfile(@RequestBody MemberDto memberUpdateDto)
@PutMapping("/profile")
public ResponseEntity<MemberUpdateDto> editProfile(@RequestBody MemberUpdateDto requestDto, Authentication auth)
{
String memberEmail = SecurityContextHolder.getContext().getAuthentication().getName();
Member member = memberService.getMember(memberEmail);
this.memberService.updateMember(member, memberUpdateDto);
String email = auth.getName();
Member member = this.memberService.updateMember(email, requestDto);

MemberDto memberDto = new MemberDto(member);
return ResponseEntity.ok(memberDto);
MemberUpdateDto responseDto = modelMapper.map(member, MemberUpdateDto.class);
return ResponseEntity.ok(responseDto);
}

@PutMapping("/change-password")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.dife.api.model.dto;

import com.dife.api.model.MBTI_category;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import lombok.*;

Expand All @@ -13,19 +12,15 @@
public class MemberUpdateDto {

@NotNull
private String password;
private String bio;

@NotNull
private Boolean is_korean;
private Boolean is_public;

@NotNull
private String bio;
private String major;

@NotNull
private MBTI_category mbti;

@NotNull
private Boolean is_public;

private String nickname;
}
22 changes: 10 additions & 12 deletions rest-api/src/main/java/com/dife/api/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
import com.dife.api.exception.MemberException;
import com.dife.api.exception.RegisterException;
import com.dife.api.jwt.JWTUtil;
import com.dife.api.model.dto.MemberDto;
import com.dife.api.model.dto.RegisterRequestDto;
import com.dife.api.model.dto.VerifyEmailDto;
import lombok.RequiredArgsConstructor;
import com.dife.api.model.Member;
import com.dife.api.model.dto.MemberUpdateDto;
import com.dife.api.repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -67,18 +67,16 @@ public Member getMember(String email) {

}

public void updateMember(Member member, MemberDto memberUpdateDto)
{
String encodedPassword = passwordEncoder.encode(memberUpdateDto.getPassword());
member.setPassword(encodedPassword);
public Member updateMember(String email, MemberUpdateDto dto) {
Member member = this.getMember(email);

member.setIs_korean(memberUpdateDto.getIs_korean());
member.setBio(memberUpdateDto.getBio());
member.setMbti(memberUpdateDto.getMbti());
member.setIs_public(memberUpdateDto.getIs_public());
member.setNickname(memberUpdateDto.getNickname());
member.setBio(dto.getBio());
member.setIs_public(dto.getIs_public());
member.setMajor(dto.getMajor());
member.setMbti(dto.getMbti());
member.setNickname(dto.getNickname());

memberRepository.save(member);
return member;
}

public boolean changePassword(VerifyEmailDto emailDto)
Expand Down

0 comments on commit bd55cd4

Please sign in to comment.