Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GETP-203 refactor: client 컴포넌트 포트-어댑터 아키텍쳐 적용 #137

Merged
merged 8 commits into from
Aug 21, 2024
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package es.princip.getp.api.controller.client.command;

import es.princip.getp.api.controller.ApiResponse;
import es.princip.getp.api.controller.ApiResponse.ApiSuccessResult;
import es.princip.getp.api.controller.client.command.dto.request.EditMyClientRequest;
import es.princip.getp.api.controller.client.command.dto.request.RegisterMyClientRequest;
import es.princip.getp.api.controller.client.command.dto.response.RegisterMyClientResponse;
import es.princip.getp.api.controller.ApiResponse;
import es.princip.getp.api.controller.ApiResponse.ApiSuccessResult;
import es.princip.getp.api.security.details.PrincipalDetails;
import es.princip.getp.domain.client.command.application.ClientService;
import es.princip.getp.domain.client.command.application.command.EditClientCommand;
import es.princip.getp.domain.client.command.application.command.RegisterClientCommand;
import es.princip.getp.application.client.command.EditClientCommand;
import es.princip.getp.application.client.command.RegisterClientCommand;
import es.princip.getp.application.client.port.in.EditClientUseCase;
import es.princip.getp.application.client.port.in.RegisterClientUseCase;
import es.princip.getp.domain.member.model.Member;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
Expand All @@ -22,7 +24,8 @@
@RequiredArgsConstructor
public class MyClientController {

private final ClientService clientService;
private final RegisterClientUseCase registerClientUseCase;
private final EditClientUseCase editClientUseCase;

/**
* 내 의뢰자 정보 등록
Expand All @@ -35,9 +38,9 @@ public ResponseEntity<ApiSuccessResult<RegisterMyClientResponse>> registerMyClie
@RequestBody @Valid final RegisterMyClientRequest request,
@AuthenticationPrincipal final PrincipalDetails principalDetails
) {
final Long memberId = principalDetails.getMember().getMemberId();
final RegisterClientCommand command = request.toCommand(memberId);
final Long clientId = clientService.registerClient(command);
final Member member = principalDetails.getMember();
final RegisterClientCommand command = request.toCommand(member);
final Long clientId = registerClientUseCase.register(command);
final RegisterMyClientResponse response = new RegisterMyClientResponse(clientId);
return ApiResponse.success(HttpStatus.CREATED, response);
}
Expand All @@ -55,7 +58,7 @@ public ResponseEntity<ApiSuccessResult<?>> editMyClient(
) {
final Long memberId = principalDetails.getMember().getMemberId();
final EditClientCommand command = request.toCommand(memberId);
clientService.editClient(command);
editClientUseCase.edit(command);
return ApiResponse.success(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package es.princip.getp.api.controller.client.command.dto.request;

import es.princip.getp.domain.client.command.application.command.EditClientCommand;
import es.princip.getp.domain.client.command.domain.Address;
import es.princip.getp.domain.client.command.domain.BankAccount;
import es.princip.getp.application.client.command.EditClientCommand;
import es.princip.getp.domain.client.model.Address;
import es.princip.getp.domain.client.model.BankAccount;
import es.princip.getp.api.validation.EmailPattern;
import es.princip.getp.api.validation.PhoneNumberPattern;
import es.princip.getp.domain.member.model.Email;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package es.princip.getp.api.controller.client.command.dto.request;

import es.princip.getp.domain.client.command.application.command.RegisterClientCommand;
import es.princip.getp.domain.client.command.domain.Address;
import es.princip.getp.domain.client.command.domain.BankAccount;
import es.princip.getp.api.validation.EmailPattern;
import es.princip.getp.api.validation.PhoneNumberPattern;
import es.princip.getp.application.client.command.RegisterClientCommand;
import es.princip.getp.domain.client.model.Address;
import es.princip.getp.domain.client.model.BankAccount;
import es.princip.getp.domain.member.model.Email;
import es.princip.getp.domain.member.model.Member;
import es.princip.getp.domain.member.model.Nickname;
import es.princip.getp.domain.member.model.PhoneNumber;
import jakarta.validation.Valid;
Expand All @@ -20,9 +21,9 @@ public record RegisterMyClientRequest(
@Valid BankAccount bankAccount // 선택
) {

public RegisterClientCommand toCommand(final Long memberId) {
public RegisterClientCommand toCommand(final Member member) {
return new RegisterClientCommand(
memberId,
member,
Nickname.of(nickname()),
email() == null ? null : Email.of(email()),
PhoneNumber.of(phoneNumber()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package es.princip.getp.api.controller.client.query;

import es.princip.getp.api.controller.client.query.dto.ClientResponse;
import es.princip.getp.api.controller.ApiResponse;
import es.princip.getp.api.controller.ApiResponse.ApiSuccessResult;
import es.princip.getp.domain.client.query.dao.ClientDao;
import es.princip.getp.api.controller.client.query.dto.ClientResponse;
import es.princip.getp.application.client.port.out.ClientQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -18,7 +18,7 @@
@RequiredArgsConstructor
public class ClientQueryController {

private final ClientDao clientDao;
private final ClientQuery clientQuery;

/**
* 의뢰자 정보 조회
Expand All @@ -29,7 +29,7 @@ public class ClientQueryController {
@GetMapping("/{clientId}")
@PreAuthorize("(hasRole('ADMIN') or hasRole('MANAGER')) and isAuthenticated()")
public ResponseEntity<ApiSuccessResult<ClientResponse>> getClient(@PathVariable final Long clientId) {
final ClientResponse response = clientDao.findById(clientId);
final ClientResponse response = clientQuery.findClientById(clientId);
return ApiResponse.success(HttpStatus.OK, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package es.princip.getp.api.controller.client.query;

import es.princip.getp.api.controller.client.query.dto.ClientResponse;
import es.princip.getp.api.controller.ApiResponse;
import es.princip.getp.api.controller.ApiResponse.ApiSuccessResult;
import es.princip.getp.api.controller.client.query.dto.ClientResponse;
import es.princip.getp.api.security.details.PrincipalDetails;
import es.princip.getp.domain.client.query.dao.ClientDao;
import es.princip.getp.application.client.port.out.ClientQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -19,7 +19,7 @@
@RequiredArgsConstructor
public class MyClientQueryController {

private final ClientDao clientDao;
private final ClientQuery clientQuery;

/**
* 내 의뢰자 정보 조회
Expand All @@ -31,7 +31,7 @@ public class MyClientQueryController {
public ResponseEntity<ApiSuccessResult<ClientResponse>> getMyClient(
@AuthenticationPrincipal final PrincipalDetails principalDetails) {
final Long memberId = principalDetails.getMember().getMemberId();
final ClientResponse response = clientDao.findByMemberId(memberId);
final ClientResponse response = clientQuery.findClientByMemberId(memberId);
return ApiResponse.success(HttpStatus.OK, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package es.princip.getp.api.controller.client.query.dto;

import es.princip.getp.domain.client.command.domain.Address;
import es.princip.getp.domain.client.command.domain.BankAccount;
import es.princip.getp.domain.client.command.domain.Client;
import es.princip.getp.domain.member.model.Member;
import es.princip.getp.domain.client.model.Address;
import es.princip.getp.domain.client.model.BankAccount;

import java.time.LocalDateTime;

Expand All @@ -18,18 +16,4 @@ public record ClientResponse(
LocalDateTime createdAt,
LocalDateTime updatedAt
) {

public static ClientResponse of(final Client client, final Member member) {
return new ClientResponse(
client.getClientId(),
member.getNickname().getValue(),
client.getEmail().getValue(),
member.getPhoneNumber().getValue(),
member.getProfileImage().getUrl(),
client.getAddress(),
client.getBankAccount(),
client.getCreatedAt(),
client.getUpdatedAt()
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package es.princip.getp.api.controller.project.query.dto;

import es.princip.getp.domain.client.command.domain.Address;
import es.princip.getp.domain.client.model.Address;

public record ProjectClientResponse(
Long clientId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package es.princip.getp.domain.client.command.application.command;
package es.princip.getp.application.client.command;

import es.princip.getp.domain.client.command.domain.Address;
import es.princip.getp.domain.client.command.domain.BankAccount;
import es.princip.getp.domain.client.model.Address;
import es.princip.getp.domain.client.model.BankAccount;
import es.princip.getp.domain.member.model.Email;
import es.princip.getp.domain.member.model.Nickname;
import es.princip.getp.domain.member.model.PhoneNumber;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package es.princip.getp.domain.client.command.application.command;
package es.princip.getp.application.client.command;

import es.princip.getp.domain.client.command.domain.Address;
import es.princip.getp.domain.client.command.domain.BankAccount;
import es.princip.getp.domain.client.model.Address;
import es.princip.getp.domain.client.model.BankAccount;
import es.princip.getp.domain.member.model.Email;
import es.princip.getp.domain.member.model.Member;
import es.princip.getp.domain.member.model.Nickname;
import es.princip.getp.domain.member.model.PhoneNumber;

public record RegisterClientCommand(
Long memberId,
Member member,
Nickname nickname,
Email email, // 미입력 시 회원 가입 시 작성한 이메일 주소가 기본값
PhoneNumber phoneNumber,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package es.princip.getp.domain.client.exception;
package es.princip.getp.application.client.exception;

import es.princip.getp.common.exception.BusinessLogicException;
import es.princip.getp.common.exception.ErrorDescription;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.getp.application.client.port.in;

import es.princip.getp.application.client.command.EditClientCommand;

public interface EditClientUseCase {

void edit(EditClientCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.getp.application.client.port.in;

import es.princip.getp.application.client.command.RegisterClientCommand;

public interface RegisterClientUseCase {

Long register(RegisterClientCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package es.princip.getp.application.client.port.in;

public interface RemoveClientUseCase {

void remove(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package es.princip.getp.application.client.port.out;

public interface CheckClientPort {

boolean existsBy(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package es.princip.getp.application.client.port.out;

import es.princip.getp.api.controller.client.query.dto.ClientResponse;
import es.princip.getp.api.controller.project.query.dto.ProjectClientResponse;

public interface ClientQuery {

ClientResponse findClientById(final Long clientId);

ClientResponse findClientByMemberId(final Long memberId);

ProjectClientResponse findProjectClientById(final Long clientId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.getp.application.client.port.out;

import es.princip.getp.domain.client.model.Client;

public interface DeleteClientPort {

void delete(Client client);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.getp.application.client.port.out;

import es.princip.getp.domain.client.model.Client;

public interface LoadClientPort {

Client loadBy(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.getp.application.client.port.out;

import es.princip.getp.domain.client.model.Client;

public interface SaveClientPort {

Long save(Client client);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package es.princip.getp.application.client.port.out;

import es.princip.getp.domain.client.model.Client;

public interface UpdateClientPort {

void update(Client client);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package es.princip.getp.application.client.service;

import es.princip.getp.application.client.command.EditClientCommand;
import es.princip.getp.application.client.port.in.EditClientUseCase;
import es.princip.getp.application.client.port.out.LoadClientPort;
import es.princip.getp.application.client.port.out.UpdateClientPort;
import es.princip.getp.application.member.command.EditMemberCommand;
import es.princip.getp.application.member.port.in.EditMemberUseCase;
import es.princip.getp.domain.client.model.Client;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class EditClientService implements EditClientUseCase {

private final EditMemberUseCase editMemberUseCase;

private final LoadClientPort loadClientPort;
private final UpdateClientPort updateClientPort;

@Override
@Transactional
public void edit(EditClientCommand command) {
editMemberUseCase.editMember(EditMemberCommand.from(command));
final Client client = loadClientPort.loadBy(command.memberId());
client.edit(command.email(), command.address(), command.bankAccount());
updateClientPort.update(client);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package es.princip.getp.application.client.service;

import es.princip.getp.application.client.command.RegisterClientCommand;
import es.princip.getp.application.client.exception.AlreadyExistsClientException;
import es.princip.getp.application.client.port.in.RegisterClientUseCase;
import es.princip.getp.application.client.port.out.CheckClientPort;
import es.princip.getp.application.client.port.out.SaveClientPort;
import es.princip.getp.application.member.command.EditMemberCommand;
import es.princip.getp.application.member.port.in.EditMemberUseCase;
import es.princip.getp.domain.client.model.Client;
import es.princip.getp.domain.member.model.Email;
import es.princip.getp.domain.member.model.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
public class RegisterClientService implements RegisterClientUseCase {

private final EditMemberUseCase editMemberUseCase;

private final CheckClientPort checkClientPort;
private final SaveClientPort saveClientPort;

@Override
@Transactional
public Long register(RegisterClientCommand command) {
final Member member = command.member();
if (checkClientPort.existsBy(member.getMemberId())) {
throw new AlreadyExistsClientException();
}
editMemberUseCase.editMember(EditMemberCommand.from(command));
// 이메일이 입력되지 않은 경우 회원 가입 시 작성한 이메일 주소를 기본값으로 사용
final Email email = command.email() == null ? member.getEmail() : command.email();
final Client client = Client.builder()
.email(email)
.bankAccount(command.bankAccount())
.address(command.address())
.memberId(member.getMemberId())
.build();
return saveClientPort.save(client);
}
}
Loading