Refactor Account Query API
- change to query with profile information when query account information.
This commit is contained in:
@@ -23,7 +23,6 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
public class AccountFacade {
|
||||
|
||||
private final RegisterAccountProcessor registerProcessor;
|
||||
private final AccountTranslator translator;
|
||||
private final ApplicationEventPublisher publisher;
|
||||
private final ConfirmRegisterAccountProcessor confirmRegisterProcessor;
|
||||
private final LoginAccountProcessor loginProcessor;
|
||||
@@ -31,12 +30,11 @@ public class AccountFacade {
|
||||
private final UpdateAccountProcessor updateProcessor;
|
||||
|
||||
public AccountFacade(RegisterAccountProcessor registerProcessor,
|
||||
AccountTranslator translator, ApplicationEventPublisher publisher,
|
||||
ApplicationEventPublisher publisher,
|
||||
ConfirmRegisterAccountProcessor confirmRegisterProcessor,
|
||||
LoginAccountProcessor loginProcessor, AccountReader accountReader,
|
||||
UpdateAccountProcessor updateProcessor) {
|
||||
this.registerProcessor = registerProcessor;
|
||||
this.translator = translator;
|
||||
this.publisher = publisher;
|
||||
this.confirmRegisterProcessor = confirmRegisterProcessor;
|
||||
this.loginProcessor = loginProcessor;
|
||||
@@ -64,9 +62,11 @@ public class AccountFacade {
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public AccountResponse findInfo(String email) {
|
||||
return translator.toResponse(accountReader.findByEmail(email)
|
||||
.orElseThrow(() -> new AccountNotFoundException(email)));
|
||||
public AccountResponse findInfo(Authentication authentication) {
|
||||
var memberAccount = accountReader.findByEmailAndMemberId(
|
||||
authentication.getCredentials(), authentication.getMemberId());
|
||||
return new AccountResponse(memberAccount.getId(), memberAccount.getEmail(),
|
||||
memberAccount.getNickname(), memberAccount.getImage());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.yam.app.account.application;
|
||||
|
||||
import com.yam.app.account.domain.Account;
|
||||
import com.yam.app.account.presentation.AccountResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
final class AccountTranslator {
|
||||
|
||||
public AccountResponse toResponse(Account entity) {
|
||||
return new AccountResponse(entity.getId(), entity.getEmail(),
|
||||
entity.isEmailVerified(), entity.getJoinedAt(), entity.getLastModifiedAt(),
|
||||
entity.getWithdrawalAt(), entity.isWithdraw(), entity.getRole().name());
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
package com.yam.app.account.domain;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
public interface AccountReader {
|
||||
|
||||
boolean existsByEmail(String email);
|
||||
|
||||
Optional<Account> findByEmail(String email);
|
||||
|
||||
MemberAccount findByEmailAndMemberId(@Param("email") String email,
|
||||
@Param("memberId") Long memberId);
|
||||
}
|
||||
|
||||
19
src/main/java/com/yam/app/account/domain/MemberAccount.java
Normal file
19
src/main/java/com/yam/app/account/domain/MemberAccount.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.yam.app.account.domain;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
public final class MemberAccount {
|
||||
|
||||
private final Long id;
|
||||
private final String email;
|
||||
private final String nickname;
|
||||
private final String image;
|
||||
|
||||
public MemberAccount(Long id, String email, String nickname, String image) {
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
this.nickname = nickname;
|
||||
this.image = image;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package com.yam.app.account.infrastructure;
|
||||
import com.yam.app.account.domain.Account;
|
||||
import com.yam.app.account.domain.AccountReader;
|
||||
import com.yam.app.account.domain.AccountRepository;
|
||||
import com.yam.app.account.domain.MemberAccount;
|
||||
import java.util.Optional;
|
||||
import org.mybatis.spring.SqlSessionTemplate;
|
||||
|
||||
@@ -44,4 +45,10 @@ public final class MybatisAccountRepository implements AccountRepository, Accoun
|
||||
public Optional<Account> findByEmail(String email) {
|
||||
return template.getMapper(AccountReader.class).findByEmail(email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MemberAccount findByEmailAndMemberId(String email,
|
||||
Long memberId) {
|
||||
return template.getMapper(AccountReader.class).findByEmailAndMemberId(email, memberId);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,8 @@ public final class AccountQueryApi {
|
||||
@GetMapping("/api/accounts/me")
|
||||
public ResponseEntity<ApiResult<?>> findInfo(
|
||||
@AuthenticationPrincipal Authentication authentication) {
|
||||
return ResponseEntity
|
||||
.ok(ApiResult.success(accountFacade.findInfo(authentication.getCredentials())));
|
||||
return ResponseEntity.ok(
|
||||
ApiResult.success(accountFacade.findInfo(authentication)));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
package com.yam.app.account.presentation;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@@ -8,23 +7,13 @@ public final class AccountResponse {
|
||||
|
||||
private final Long id;
|
||||
private final String email;
|
||||
private final boolean emailVerified;
|
||||
private final LocalDateTime joinedAt;
|
||||
private final LocalDateTime lastModifiedAt;
|
||||
private final LocalDateTime withdrawalAt;
|
||||
private final boolean withdraw;
|
||||
private final String role;
|
||||
private final String nickname;
|
||||
private final String image;
|
||||
|
||||
public AccountResponse(Long id, String email, boolean emailVerified,
|
||||
LocalDateTime joinedAt, LocalDateTime lastModifiedAt, LocalDateTime withdrawalAt,
|
||||
boolean withdraw, String role) {
|
||||
public AccountResponse(Long id, String email, String nickname, String image) {
|
||||
this.id = id;
|
||||
this.email = email;
|
||||
this.emailVerified = emailVerified;
|
||||
this.joinedAt = joinedAt;
|
||||
this.lastModifiedAt = lastModifiedAt;
|
||||
this.withdrawalAt = withdrawalAt;
|
||||
this.withdraw = withdraw;
|
||||
this.role = role;
|
||||
this.nickname = nickname;
|
||||
this.image = image;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package com.yam.app.comment.domain;
|
||||
package com.yam.app.article.domain;
|
||||
|
||||
import com.yam.app.common.EntityNotFoundException;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.yam.app.comment.domain;
|
||||
|
||||
import com.yam.app.article.domain.ArticleNotFoundException;
|
||||
import com.yam.app.article.domain.ArticleReader;
|
||||
import com.yam.app.common.UnauthorizedRequestException;
|
||||
|
||||
|
||||
@@ -14,4 +14,21 @@
|
||||
SELECT * FROM ACCOUNT WHERE email = #{email}
|
||||
</select>
|
||||
|
||||
<select id="findByEmailAndMemberId" resultMap="memberAccount">
|
||||
SELECT a.id AS account_id,
|
||||
a.email AS account_email,
|
||||
m.nickname AS member_nickname,
|
||||
m.image AS member_image
|
||||
FROM account a
|
||||
INNER JOIN member m ON m.id = #{memberId}
|
||||
WHERE a.email = #{email};
|
||||
</select>
|
||||
|
||||
<resultMap id="memberAccount" type="com.yam.app.account.domain.MemberAccount">
|
||||
<id property="id" column="account_id" />
|
||||
<result property="email" column="account_email" />
|
||||
<result property="nickname" column="member_nickname" />
|
||||
<result property="image" column="member_image" />
|
||||
</resultMap>
|
||||
|
||||
</mapper>
|
||||
|
||||
@@ -1,19 +1,20 @@
|
||||
insert into member(nickname, image, status)
|
||||
values ('rebwon', 'temp.png', 'ALIVE'),
|
||||
('loginCheck', 'temp.png', 'ALIVE');
|
||||
|
||||
insert into account(email, email_check_token, email_check_token_generated_at, email_verified,
|
||||
joined_at, last_modified_at, password, withdraw, role, status)
|
||||
values ('jiwonDev@gmail.com', 'emailchecktoken', now(), false, now(), now(), 'password!',
|
||||
false, 'DEFAULT', 'ALIVE'),
|
||||
('loginCheck@gmail.com', 'emailchecktoken1', now(), true, now(), now(),
|
||||
'$2a$10$EqbMbYB0vcZnuA5CClqa9uiLDnjA6pCjxn208ZchzA2q3ofqnkhcq',
|
||||
false, 'DEFAULT', 'ALIVE');
|
||||
|
||||
insert into member(nickname, image, status)
|
||||
values ('rebwon', 'temp.png', 'ALIVE');
|
||||
|
||||
insert into account(email, email_check_token, email_check_token_generated_at, email_verified,
|
||||
joined_at, last_modified_at, password, withdraw, role, member_id, status)
|
||||
values ('rebwon@gmail.com', 'emailchecktoken1', now(), true, now(), now(),
|
||||
'$2a$10$g1V1fmucOwRZX.bWCb9k5uFc/EylGvoYw6N8m90RxIcvyYVhFBl1C',
|
||||
false, 'DEFAULT', 1, 'ALIVE'),
|
||||
('loginCheck@gmail.com', 'emailchecktoken1', now(), true, now(), now(),
|
||||
'$2a$10$EqbMbYB0vcZnuA5CClqa9uiLDnjA6pCjxn208ZchzA2q3ofqnkhcq',
|
||||
false, 'DEFAULT', 1, 'ALIVE');
|
||||
false, 'DEFAULT', 2, 'ALIVE');
|
||||
|
||||
insert into article(title, content, image, status, created_at, modified_at, member_id)
|
||||
values ('sample-title', 'sample-content', 'sample.png', 'ALIVE', now(), now(), 1);
|
||||
@@ -35,7 +36,7 @@ insert into account(email, email_check_token, email_check_token_generated_at, em
|
||||
joined_at, last_modified_at, password, withdraw, role, member_id, status)
|
||||
values ('comment@gmail.com', 'emailchecktoken1', now(), true, now(), now(),
|
||||
'$2a$10$EqbMbYB0vcZnuA5CClqa9uiLDnjA6pCjxn208ZchzA2q3ofqnkhcq',
|
||||
false, 'DEFAULT', 2, 'ALIVE');
|
||||
false, 'DEFAULT', 3, 'ALIVE');
|
||||
|
||||
INSERT INTO comment(content, created_at, modified_at, status, article_id, member_id)
|
||||
VALUES ('sample content1', now(), now(), 'ALIVE', 1, 2);
|
||||
VALUES ('sample content1', now(), now(), 'ALIVE', 1, 3);
|
||||
|
||||
@@ -17,6 +17,11 @@ public final class FakeAccountRepository implements AccountRepository, AccountRe
|
||||
.findAny();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MemberAccount findByEmailAndMemberId(String email, Long memberId) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsByEmail(String email) {
|
||||
return data.values().stream()
|
||||
|
||||
@@ -4,6 +4,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
|
||||
import com.yam.app.article.domain.Article;
|
||||
import com.yam.app.article.domain.ArticleNotFoundException;
|
||||
import com.yam.app.article.domain.FakeArticleRepository;
|
||||
import com.yam.app.common.UnauthorizedRequestException;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -95,9 +95,8 @@ final class AccountIntegrationTests extends AbstractIntegrationTests {
|
||||
.andExpect(jsonPath("$.message").doesNotExist())
|
||||
.andExpect(jsonPath("$.data.id").isNumber())
|
||||
.andExpect(jsonPath("$.data.email").isString())
|
||||
.andExpect(jsonPath("$.data.emailVerified").value(true))
|
||||
.andExpect(jsonPath("$.data.withdraw").value(false))
|
||||
.andExpect(jsonPath("$.data.role").value("DEFAULT"));
|
||||
.andExpect(jsonPath("$.data.nickname").isString())
|
||||
.andExpect(jsonPath("$.data.image").isString());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user