Refactor translator, processor

This commit is contained in:
Rebwon
2021-08-31 21:05:27 +09:00
committed by MaengSol
parent 4864ec9889
commit 476704f97f
9 changed files with 41 additions and 42 deletions

View File

@@ -2,8 +2,8 @@ package com.yam.app.account.application;
import com.yam.app.account.domain.RegisterAccountEvent;
import com.yam.app.account.domain.RegisterAccountProcessor;
import com.yam.app.account.presentation.AccountResponse;
import com.yam.app.account.presentation.RegisterAccountRequest;
import com.yam.app.account.presentation.RegisterAccountResponse;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -12,11 +12,11 @@ import org.springframework.transaction.annotation.Transactional;
public class AccountFacade {
private final RegisterAccountProcessor processor;
private final AccountResponseTranslator translator;
private final AccountTranslator translator;
private final ApplicationEventPublisher publisher;
public AccountFacade(RegisterAccountProcessor processor,
AccountResponseTranslator translator,
AccountTranslator translator,
ApplicationEventPublisher publisher) {
this.processor = processor;
this.translator = translator;
@@ -24,11 +24,9 @@ public class AccountFacade {
}
@Transactional
public RegisterAccountResponse register(RegisterAccountRequest request) {
var command = request.toCommand();
var entity = processor.process(command.getEmail(), command.getNickname(),
command.getPassword());
public AccountResponse register(RegisterAccountRequest request) {
var entity = processor.process(translator.toCommand(request));
publisher.publishEvent(new RegisterAccountEvent(entity));
return translator.translate(entity);
return translator.toResponse(entity);
}
}

View File

@@ -1,14 +0,0 @@
package com.yam.app.account.application;
import com.yam.app.account.domain.Account;
import com.yam.app.account.presentation.RegisterAccountResponse;
import org.springframework.stereotype.Component;
@Component
public final class AccountResponseTranslator {
public RegisterAccountResponse translate(Account entity) {
return new RegisterAccountResponse(entity.getId(), entity.getEmail(),
entity.getNickname());
}
}

View File

@@ -0,0 +1,20 @@
package com.yam.app.account.application;
import com.yam.app.account.domain.Account;
import com.yam.app.account.presentation.AccountResponse;
import com.yam.app.account.presentation.RegisterAccountRequest;
import org.springframework.stereotype.Component;
@Component
public final class AccountTranslator {
public RegisterAccountCommand toCommand(RegisterAccountRequest request) {
return new RegisterAccountCommand(request.getEmail(), request.getNickname(),
request.getPassword());
}
public AccountResponse toResponse(Account entity) {
return new AccountResponse(entity.getId(), entity.getEmail(),
entity.getNickname());
}
}

View File

@@ -1,5 +1,7 @@
package com.yam.app.account.domain;
import com.yam.app.account.application.RegisterAccountCommand;
public final class RegisterAccountProcessor {
private final AccountRepository accountRepository;
@@ -11,16 +13,17 @@ public final class RegisterAccountProcessor {
this.passwordEncrypter = passwordEncrypter;
}
public Account process(String email, String nickname, String password) {
if (accountRepository.existsByEmail(email)) {
public Account process(RegisterAccountCommand command) {
if (accountRepository.existsByEmail(command.getEmail())) {
throw new IllegalStateException();
}
if (accountRepository.existsByNickname(nickname)) {
if (accountRepository.existsByNickname(command.getNickname())) {
throw new IllegalStateException();
}
String encodedPassword = passwordEncrypter.encode(password);
String encodedPassword = passwordEncrypter.encode(command.getPassword());
return accountRepository.save(Account.of(email, nickname, encodedPassword));
return accountRepository.save(
Account.of(command.getEmail(), command.getNickname(), encodedPassword));
}
}

View File

@@ -3,13 +3,13 @@ package com.yam.app.account.presentation;
import lombok.Getter;
@Getter
public final class RegisterAccountResponse {
public final class AccountResponse {
private final Long id;
private final String email;
private final String nickname;
public RegisterAccountResponse(Long id, String email, String nickname) {
public AccountResponse(Long id, String email, String nickname) {
this.id = id;
this.email = email;
this.nickname = nickname;

View File

@@ -23,7 +23,7 @@ public final class RegisterAccountApi {
}
@PostMapping("/api/accounts")
public ResponseEntity<RegisterAccountResponse> register(
public ResponseEntity<AccountResponse> register(
@RequestBody @Valid RegisterAccountRequest request) {
return ResponseEntity.ok(accountFacade.register(request));
}

View File

@@ -1,6 +1,5 @@
package com.yam.app.account.presentation;
import com.yam.app.account.application.RegisterAccountCommand;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
@@ -19,8 +18,4 @@ public final class RegisterAccountRequest {
@Size(min = 8, max = 12, message = "비밀번호는 최소 8자, 최대 12자까지 허용됩니다.")
@NotBlank
private String password;
public RegisterAccountCommand toCommand() {
return new RegisterAccountCommand(email, nickname, password);
}
}

View File

@@ -28,8 +28,7 @@ class RegisterAccountProcessorTest {
var command = new RegisterAccountCommand("rebwon@gmail.com", "rebwon", "password!");
// Act
Account account = processor.process(command.getEmail(), command.getNickname(),
command.getPassword());
var account = processor.process(command);
// Assert
assertThat(account.getId()).isEqualTo(1L);
@@ -40,8 +39,7 @@ class RegisterAccountProcessorTest {
// Act & Assert
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> processor.process(command.getEmail(), command.getNickname(),
command.getPassword()));
.isThrownBy(() -> processor.process(command));
}),
DynamicTest.dynamicTest("닉네임 검증에 실패하여 예외를 리턴한다.", () -> {
// Arrange
@@ -49,8 +47,7 @@ class RegisterAccountProcessorTest {
// Act & Assert
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> processor.process(command.getEmail(), command.getNickname(),
command.getPassword()));
.isThrownBy(() -> processor.process(command));
})
);
}

View File

@@ -41,7 +41,7 @@ class RegisterAccountApiTests {
// Act
when(accountFacade.register(request)).thenReturn(
new RegisterAccountResponse(1L, "msolo021015@gmail.com", "rebwon"));
new AccountResponse(1L, "msolo021015@gmail.com", "rebwon"));
final var actions = mockMvc.perform(post("/api/accounts")
.accept(MediaType.APPLICATION_JSON)