Skip to content

Commit

Permalink
refactor: 닉네임, 전화번호, 프로필 사진 URI 필드를 Member로 이동 (#65)
Browse files Browse the repository at this point in the history
* refactor: 닉네임, 전화번호, 프로필 사진 URI 필드를 Member로 이동

* rename: Test Fixture 클래스를 각 도메인의 fixture 패키지로 이동

* test: 리팩토링한 Member에 대해 테스트 코드 변경

* refactor: Member에 속한 필드의 갱신 책임을 도메인 레이어에서 서비스 레이어로 이동

* test: Member에 속한 필드의 갱신 책임을 도메인 레이어에서 서비스 레이어로 이동에 대한 테스트 작성

* test: 내 회원 정보 수정 기능 서비스 단위 테스트 작성

* test: 엔티티 비교에 `assertSoftly` 적용
  • Loading branch information
scv1702 authored Jun 3, 2024
1 parent 88b1011 commit 2976982
Show file tree
Hide file tree
Showing 32 changed files with 492 additions and 322 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
package es.princip.getp.domain.client.controller;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import es.princip.getp.domain.client.dto.request.CreateClientRequest;
import es.princip.getp.domain.client.dto.response.ClientResponse;
import es.princip.getp.domain.client.service.ClientService;
import es.princip.getp.domain.member.domain.entity.Member;
import es.princip.getp.global.security.details.PrincipalDetails;
import es.princip.getp.global.util.ApiResponse;
import es.princip.getp.global.util.ApiResponse.ApiSuccessResult;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/client")
Expand All @@ -37,8 +31,8 @@ public class ClientController {
public ResponseEntity<ApiSuccessResult<ClientResponse>> create(
@RequestBody @Valid CreateClientRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) {
Member member = principalDetails.getMember();
ClientResponse response = ClientResponse.from(clientService.create(member, request));
Long memberId = principalDetails.getMember().getMemberId();
ClientResponse response = ClientResponse.from(clientService.create(memberId, request));
return ResponseEntity.status(HttpStatus.CREATED).body(ApiResponse.success(HttpStatus.CREATED, response));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
import es.princip.getp.global.domain.values.Address;
import es.princip.getp.global.domain.values.BankAccount;
import jakarta.persistence.*;
import lombok.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -24,19 +27,9 @@ public class Client extends BaseTimeEntity {
@Column(name = "client_id")
private Long clientId;

@Column(name = "nickname")
private String nickname;

@Column(name = "email")
private String email;

@Column(name = "phone_number")
private String phoneNumber;

@Column(name = "profile_image_uri")
@Setter
private String profileImageUri;

//TODO: 주소기반산업지원서비스 API로 확장 예정
@Embedded
private Address address;
Expand All @@ -53,41 +46,43 @@ public class Client extends BaseTimeEntity {

@Builder
public Client(
final String nickname,
final String email,
final String phoneNumber,
final String profileImageUri,
final Address address,
final BankAccount bankAccount,
final Member member
) {
this.nickname = nickname;
this.email = email;
this.phoneNumber = phoneNumber;
this.profileImageUri = profileImageUri;
this.email = email == null ? member.getEmail() : email;
this.address = address;
this.bankAccount = bankAccount;
this.member = member;
member.setClient(this);
}

public static Client from(final Member member, final CreateClientRequest request) {
public static Client from(
final Member member,
final CreateClientRequest request
) {
return Client.builder()
.nickname(request.nickname())
.email(request.email() == null ? member.getEmail() : request.email())
.phoneNumber(request.phoneNumber())
.profileImageUri(request.profileImageUri())
.email(request.email())
.address(request.address())
.bankAccount(request.bankAccount())
.member(member)
.build();
}

public String getNickname() {
return this.member.getNickname();
}

public String getPhoneNumber() {
return this.member.getPhoneNumber();
}

public String getProfileImageUri() {
return this.member.getProfileImageUri();
}

public void update(final UpdateClientRequest request) {
this.nickname = request.nickname();
this.email = request.email();
this.phoneNumber = request.phoneNumber();
this.profileImageUri = request.profileImageUri();
this.address = request.address();
this.bankAccount = request.bankAccount();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package es.princip.getp.domain.client.service;

import es.princip.getp.domain.client.domain.entity.Client;
import es.princip.getp.domain.client.dto.request.CreateClientRequest;
import es.princip.getp.domain.client.dto.request.UpdateClientRequest;
import es.princip.getp.domain.client.domain.entity.Client;
import es.princip.getp.domain.client.exception.ClientErrorCode;
import es.princip.getp.domain.client.repository.ClientRepository;
import es.princip.getp.domain.member.domain.entity.Member;
import es.princip.getp.domain.member.dto.request.UpdateMemberRequest;
import es.princip.getp.domain.member.service.MemberService;
import es.princip.getp.global.exception.BusinessLogicException;
import java.util.Optional;
import java.util.function.Supplier;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.function.Supplier;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class ClientService {

private final MemberService memberService;
private final ClientRepository clientRepository;

private Client get(Optional<Client> client) {
Expand All @@ -40,13 +43,15 @@ public Client getByClientId(Long clientId) {
}

@Transactional
public Client create(Member member, CreateClientRequest request) {
Client client = Client.from(member, request);
public Client create(Long memberId, CreateClientRequest request) {
memberService.update(memberId, UpdateMemberRequest.from(request));
Client client = Client.from(memberService.getByMemberId(memberId), request);
return clientRepository.save(client);
}

@Transactional
public Client update(Long memberId, UpdateClientRequest request) {
memberService.update(memberId, UpdateMemberRequest.from(request));
Client client = getByMemberId(memberId);
client.update(request);
return client;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package es.princip.getp.domain.member.domain.entity;

import es.princip.getp.domain.base.BaseTimeEntity;
import es.princip.getp.domain.client.domain.entity.Client;
import es.princip.getp.domain.member.domain.enums.MemberType;
import es.princip.getp.domain.member.dto.request.CreateMemberRequest;
import es.princip.getp.domain.people.domain.entity.People;
import es.princip.getp.domain.member.dto.request.UpdateMemberRequest;
import es.princip.getp.domain.serviceTerm.domain.entity.ServiceTermAgreement;
import jakarta.persistence.*;
import lombok.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.ArrayList;
Expand All @@ -34,51 +36,46 @@ public class Member extends BaseTimeEntity {
@Column(name = "member_type")
private MemberType memberType;

@OneToOne(mappedBy = "member", cascade = CascadeType.ALL)
@Setter
private Client client;
@Column(name = "nickname")
private String nickname;

@OneToOne(mappedBy = "member", cascade = CascadeType.ALL)
@Setter
private People people;
@Column(name = "profile_image_uri")
private String profileImageUri;

@Column(name = "phone_number")
private String phoneNumber;

@OneToMany(mappedBy = "member", cascade = CascadeType.ALL)
private List<ServiceTermAgreement> serviceTermAgreements = new ArrayList<>();

@Builder
public Member(
Long memberId,
String email,
String password,
MemberType memberType,
LocalDateTime createdAt,
LocalDateTime updatedAt
final String email,
final String password,
final MemberType memberType,
final String nickname,
final String profileImageUri,
final String phoneNumber,
final LocalDateTime createdAt,
final LocalDateTime updatedAt
) {
this.memberId = memberId;
this.email = email;
this.password = password;
this.memberType = memberType;
this.nickname = nickname;
this.profileImageUri = profileImageUri;
this.phoneNumber = phoneNumber;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}

public String getProfileImageUri() {
if (client != null)
return client.getProfileImageUri();
if (people != null)
return people.getProfileImageUri();
return null;
}

public String getNickname() {
if (client != null)
return client.getNickname();
if (people != null)
return people.getNickname();
return null;
public void update(final UpdateMemberRequest request) {
this.nickname = request.nickname();
this.phoneNumber = request.phoneNumber();
this.profileImageUri = request.profileImageUri();
}

public static Member from(CreateMemberRequest request) {
public static Member from(final CreateMemberRequest request) {
return Member.builder()
.email(request.email())
.password(request.password())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package es.princip.getp.domain.member.dto.request;

import es.princip.getp.domain.client.dto.request.CreateClientRequest;
import es.princip.getp.domain.client.dto.request.UpdateClientRequest;
import es.princip.getp.domain.people.dto.request.CreatePeopleRequest;
import es.princip.getp.domain.people.dto.request.UpdatePeopleRequest;

public record UpdateMemberRequest(
String nickname,
String phoneNumber,
String profileImageUri
) {

public static UpdateMemberRequest from(final CreatePeopleRequest request) {
return new UpdateMemberRequest(
request.nickname(),
request.phoneNumber(),
request.profileImageUri()
);
}

public static UpdateMemberRequest from(final UpdatePeopleRequest request) {
return new UpdateMemberRequest(
request.nickname(),
request.phoneNumber(),
request.profileImageUri()
);
}

public static UpdateMemberRequest from(final CreateClientRequest request) {
return new UpdateMemberRequest(
request.nickname(),
request.phoneNumber(),
request.profileImageUri()
);
}

public static UpdateMemberRequest from(final UpdateClientRequest request) {
return new UpdateMemberRequest(
request.nickname(),
request.phoneNumber(),
request.profileImageUri()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import es.princip.getp.domain.auth.exception.SignUpErrorCode;
import es.princip.getp.domain.member.domain.entity.Member;
import es.princip.getp.domain.member.dto.request.CreateMemberRequest;
import es.princip.getp.domain.member.dto.request.UpdateMemberRequest;
import es.princip.getp.domain.member.exception.MemberErrorCode;
import es.princip.getp.domain.member.repository.MemberRepository;
import es.princip.getp.domain.serviceTerm.domain.entity.ServiceTerm;
Expand Down Expand Up @@ -79,4 +80,10 @@ public URI updateProfileImage(Member member, MultipartFile image) {
Path path = Paths.get(String.valueOf(member.getMemberId())).resolve("profile");
return URI.create("/" + PathUtil.toURI(imageStorageService.storeImage(path, image)));
}

@Transactional
public void update(Long memberId, UpdateMemberRequest request) {
Member member = getByMemberId(memberId);
member.update(request);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package es.princip.getp.domain.people.controller;

import es.princip.getp.domain.member.domain.entity.Member;
import es.princip.getp.domain.people.dto.request.CreatePeopleRequest;
import es.princip.getp.domain.people.dto.response.people.CardPeopleResponse;
import es.princip.getp.domain.people.dto.response.people.CreatePeopleResponse;
Expand Down Expand Up @@ -44,8 +43,8 @@ public class PeopleController extends ControllerSupport {
public ResponseEntity<ApiSuccessResult<CreatePeopleResponse>> createPeople(
@RequestBody @Valid CreatePeopleRequest request,
@AuthenticationPrincipal PrincipalDetails principalDetails) throws URISyntaxException {
Member member = principalDetails.getMember();
CreatePeopleResponse response = CreatePeopleResponse.from(peopleService.create(member, request));
Long memberId = principalDetails.getMember().getMemberId();
CreatePeopleResponse response = CreatePeopleResponse.from(peopleService.create(memberId, request));
return ResponseEntity.created(new URI("/people/" + response.peopleId()))
.body(ApiResponse.success(HttpStatus.CREATED, response));
}
Expand Down
Loading

0 comments on commit 2976982

Please sign in to comment.