π 2λ¨κ³ - μ₯λ°κ΅¬λ κΈ°λ₯#38
Conversation
hongmoSung
commented
May 14, 2023
- μ¬μ©μ κΈ°λ₯ ꡬν
- μ¬μ©μ μ€μ νμ΄μ§ μ°λ
- μ₯λ°κ΅¬λ κΈ°λ₯ ꡬν
- μ₯λ°κ΅¬λ νμ΄μ§ μ°λ
- μ¬μ©μ κΈ°λ₯ ꡬν - μ¬μ©μ μ€μ νμ΄μ§ μ°λ - μ₯λ°κ΅¬λ κΈ°λ₯ ꡬν - μ₯λ°κ΅¬λ νμ΄μ§ μ°λ
Gomding
left a comment
There was a problem hiding this comment.
μλ
νμΈμ νλͺ¨λ!!
μ₯λ°κ΅¬λ λ―Έμ
2λ¨κ³λ λΉ λ₯΄κ² μ§νν΄μ£Όμ
¨λ€μ π π π
1λ¨κ³ merge νλ©° λ¨κ²Όλ 리뷰λ νμΈμ΄ μλκ² κ°μ 리λ§μΈλ λ립λλ€..!
λͺ κ°μ§ μ½λ©νΈ λ¨κ²ΌμΌλ νμΈλΆνλλ €μ π
κΆκΈν μ μμΌλ©΄ μΈμ λ νΈνκ² DM λ¨κ²¨μ£ΌμΈμ~
| CartMember cartMember; | ||
| try { | ||
| cartMember = jdbcTemplate.queryForObject(sql, carMemberRowMapper(), email, password); | ||
| } catch (Exception e) { | ||
| return Optional.empty(); | ||
| }; | ||
| return Optional.ofNullable(cartMember); |
There was a problem hiding this comment.
CartMember λ³μλ₯Ό ν λΉνλ κ³Όμ μ΄ νμνμ§ μμ 보μ¬μ!
λ³μλ₯Ό ν λΉνμ§ μκ³ κ°κ°μ μν©μ λ°λ‘ λ°ννλ κ²μ μ΄λ»κ² μκ°νμλμ?
| CartMember cartMember; | |
| try { | |
| cartMember = jdbcTemplate.queryForObject(sql, carMemberRowMapper(), email, password); | |
| } catch (Exception e) { | |
| return Optional.empty(); | |
| }; | |
| return Optional.ofNullable(cartMember); | |
| try { | |
| return Optional.ofNullable(jdbcTemplate.queryForObject(sql, carMemberRowMapper(), email, password)); | |
| } catch (Exception e) { | |
| return Optional.empty(); | |
| }; |
| private RowMapper<CartMember> carMemberRowMapper() { | ||
| return (rs, rowNum) -> { |
There was a problem hiding this comment.
ν΄λΉ λ©μλλ₯Ό νΈμΆν λλ§λ€ RowMapper κ° μμ±λ κ²μΌλ‘ 보μ¬μ :)
νλλ‘ λ§λ€μ§ μμΌμ μ΄μ κ° μμκΉμ?
| private RowMapper<CartMember> carMemberRowMapper() { | |
| return (rs, rowNum) -> { | |
| private final RowMapper<CartMember> carMemberRowMapper = (rs, rowNum) -> { |
There was a problem hiding this comment.
μ μ²μμ μλ €μ£Όμ λ΄μ©μ λ°λ‘ μ΄ν΄νμ§ λͺ»νμ¬μ λ°λ‘ λ©μλλ‘ λΆλ¦¬νμ¬ λ§λ€μμμ΅λλ€.
μλ €μ£Όμ λ΄μ©μ²λΌ νλλ‘ λ§λ€μ΄ μμ νμμ΅λλ€.
| 'https://i.namu.wiki/i/6dLtHMNcLvz8BWVrtEB9CNEPqjDEhLIbtEpnZdIcbxZ4TZPc-P-Yk69qoEWSIaCudos3zI0mT-jw89Lm881FHg.webp', | ||
| now()); | ||
|
|
||
| create table CART_MEMBER |
There was a problem hiding this comment.
CART_MEMBER λΌλ μ΄λ¦μ CART λΌλ μλΉμ€μ λ무 νμ λμ΄ μλ λλμ΄κ΅°μ π€
λμ€μ λ€λ₯Έ κΈ°λ₯μμ μ¬μ©νλ©΄ μ΄μν ν
μ΄λΈμ΄ λμ§ μμκΉμ? π€
| } | ||
|
|
||
| @Override | ||
| public List<ViewCartItem> getCartItems(Long memberId) { |
There was a problem hiding this comment.
Repository μμ presentation μμμ DTOλ₯Ό λ°νν΄μ£Όκ³ μκ΅°μ π€
persistance μμμμ View μ λ΄μ©κΉμ§ μμλ²λ¦¬κ²λλ
μλ΅κ°μ λ³νμ persistance μμμ΄ μν₯μ λ°κ² λ μ μκ² λ€μ..!
entity λΌλ κ°μ²΄λ₯Ό λ§λ€μ΄ μ£Όμ ¨μμλ View μμμ DTO λ₯Ό λ°λ‘ μ¬μ©νμ μ΄μ κ° κΆκΈν΄μ!
There was a problem hiding this comment.
νλ©΄μ λ
ΈμΆν΄μΌν λ°μ΄ν°λ₯Ό λ§μΆκΈ° μν΄
join μΌλ‘ νμν λ°μ΄ν°λ₯Ό λ§μΆλ€ 보λ View μμμ dto κ° μ ν©νλ€ μκ°μ΄ λ€μ΄
μ¬μ©νμμ΅λλ€. π
There was a problem hiding this comment.
Repository μμλ μ μ₯μμ μλ λ°μ΄ν°λ₯Ό μ‘°ννκ³ μλ΅ν΄μ£Όλ κ² κΉμ§κ° μ± μμ΄λΌκ³ μκ°ν΄μ!
View μκ² μ΄λ»κ² μλ΅λ μ§λ κ΄μ¬μ¬ λ°μ΄ μλκΉμ? π€
Entity κ°μ²΄λ λλ©μΈ κ°μ²΄μ λ§€ννλ λ°©λ²λ κ³ λ €ν΄λ³Ό μ μκ² λ€μ π
| int updateCount = jdbcTemplate.update(sql, id); | ||
| if (updateCount != 1) { | ||
| throw new RuntimeException(id + "μ ν΄λΉνλ μ₯λ°κ΅¬λ μμ΄ν¬μ μμ νλλ° μ€λ₯κ° λ°μνμμ΅λλ€."); | ||
| } |
There was a problem hiding this comment.
1λ¨κ³ merge νλ©΄μ λ¨κ²Όλ λΆλΆμ λν μ견λ νμΈλΆνλλ €μ ! π
delete μ DAO λ updateCount λ§μ λ°ννκ³
μμΈμ²λ¦¬λ νΈμΆνλ μͺ½μμ ν΄λ³΄λ©΄ μ΄λ¨κΉμ?DAO λ κ°λ₯νλ©΄ λ°κΉ₯μμ μ΄λ»κ² μ¬μ©λ μ§μ κ΄μ¬μ κ°μ§μ§ μλλ‘ κ΅¬ννλκ²μ΄ μ’μ΅λλ€ :)
μ΄λ€ λ‘μ§μμλ μμ ν λ λμμ΄ μμ΄λ μμΈκ° λ°μνμ§ μμμΌν μλ μκ² μ£ ?
| return Optional.ofNullable(new AuthInfo(email, password)); | ||
| } | ||
|
|
||
| return Optional.empty(); |
There was a problem hiding this comment.
μΈμ¦ κ°μ μΆμΆν μ μμΌλ©΄ Optional.emtpy() λ₯Ό λ°ννλλ‘ ν΄μ£Όμ
¨κ΅°μ π―
| Optional<AuthInfo> optionalAuthInfo = auth.extract(request); | ||
|
|
||
| if (optionalAuthInfo.isEmpty()) { | ||
| return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); | ||
|
|
||
| } |
There was a problem hiding this comment.
μ¬μ©μ μΈμ¦μ μν΄ κ°μ λ‘μ§μ΄ μ€λ³΅μ¬μ©λκ³ μκ΅°μ!
Interceptor μ ArgumanetResolver μ λν νμ΅ ν μ€νΈλ₯Ό μ§νν΄λ³ΌκΉμ?
| import java.util.Optional; | ||
|
|
||
| @Repository | ||
| public class MemberJdbcRepository implements MemberRepository { |
There was a problem hiding this comment.
ν
μ€νΈλ€μ μ μ νκ² μμ±ν΄λ³΄μ
¨μΌλ©΄ μ’κ² μ΄μ~!!
μ€νλ§μμ μ 곡νλ ν
μ€νΈ λꡬλ€μ νμ©ν΄λ³΄μ£ :)
Spring νλ μμν¬λ₯Ό μ¬μ©ν ν μ€νΈλ μμ±ν΄λ³΄λ©΄ μ΄λ¨κΉμ?
κ°λ¨νκ² @jdbcTest λΌλ μ λν μ΄μ μ μ¬μ©ν΄μ Repository ν μ€νΈλΆν° μμ±ν΄λ³΄μλ©΄ μ’κ² μ΄μ!
https://www.baeldung.com/spring-jdbctemplate-testing+) λ λμκ°μ Service , Controller ν μ€νΈλ μμ±ν΄λ³΄μλ©΄ μ’κ² κ΅°μ π
Controller ν μ€νΈλ ProductIntegrationTest μ μν ν μ€νΈκ° μμΌλ λ€λ₯Έ path λ€λ ν μ€νΈλ₯Ό μμ±ν΄λ³Ό μ μκ² λ€μ!
| } | ||
|
|
||
| public List<ViewMember> getMembers() { | ||
| return memberRepository.getMembers().stream().map(ViewMember::new) |
There was a problem hiding this comment.
λ©μλ 체μ΄λμ ν λλ κ° λ©μλλ§λ€ κ°νν΄μ£Όλ κ²μ΄ κ°λ μ±μ΄ μ’λ€κ³ μκ°ν΄μ~
| return memberRepository.getMembers().stream().map(ViewMember::new) | |
| return memberRepository.getMembers() | |
| .stream() | |
| .map(ViewMember::new) |
Gomding
left a comment
There was a problem hiding this comment.
μλ
νμΈμ νλͺ¨λ!
Interceptor μ ꡬνν΄μ£Όμ
¨κ΅°μ π
μΆκ°λ‘ λͺκ°μ§ μ½λ©νΈ λ¨κ²ΌμΌλ νμΈ λΆνλλ €μ!!
κΆκΈν μ μμΌμλ©΄ μΈμ λ νΈνκ² DM λ¨κ²¨μ£ΌμΈμ π
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class ItemService { |
There was a problem hiding this comment.
μ΄λ€ Item μ λν Service μΈμ§ ν΄λμ€μ΄λ¦λ§ λ³΄κ³ νμ ν μ μμ΄λ³΄μ¬μ!
CartItemService λ μ΄λ»κ² μκ°νμλμ?
|
|
||
| private final MemberRepository memberRepository; | ||
| private final ProductRepository productRepository; | ||
| private final ItemRepository itemRepository; |
There was a problem hiding this comment.
ItemRepository λ κ°μ μ견μ
λλ€ π
|
|
||
| public void addToCart(AuthInfo authInfo, RequestAddItem addItem) { | ||
| CartMember member = memberRepository.getMember(authInfo.getEmail(), authInfo.getPassword()) | ||
| .orElseThrow(() -> new RuntimeException(authInfo + "μ ν΄λΉνλ λ§΄λ²κ° μμ΅λλ€.")); |
There was a problem hiding this comment.
νμ¬ Application μμ μλ¬κ° λ°μνλ©΄ ν΄λΌμ΄μΈνΈμκ² μ΄λ»κ² μλ΅λκ³ μμκΉμ? π
ControllerAdviceμ ExceptionHandler μ λν΄ νμ΅ν΄λ³ΌκΉμ!
https://tecoble.techcourse.co.kr/post/2021-05-10-controller_advice_exception_handler/
| import java.util.Optional; | ||
|
|
||
| @Component | ||
| public class MyInterceptor implements HandlerInterceptor { |
There was a problem hiding this comment.
HandlerInterceptor λ₯Ό μ¬μ©ν΄μ£Όμ ¨κ΅°μ π
| Optional<AuthInfo> optionalAuthInfo = auth.extract(request); | ||
|
|
||
| if (optionalAuthInfo.isEmpty()) { | ||
| return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build(); | ||
|
|
||
| } |
There was a problem hiding this comment.
- Interceptor μμ μ¬μ©μ μΈμ¦μ μ§ννλλ° νλ² λ κ²μ¬νμλ μ΄μ κ° κΆκΈν΄μ!
- λ§μ½ AuthInfo λΌλ μ λ³΄κ° νμνλ€λ©΄
ArgumentResolverλ₯Ό μ¬μ©ν΄λ³΄λ건 μ΄λ¨κΉμ :)
| @Override | ||
| public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
| Optional<AuthInfo> optionalAuthInfo = auth.extract(request); | ||
| return optionalAuthInfo.isPresent(); | ||
| } |
There was a problem hiding this comment.
Header μ μ¬μ©μ μ λ³΄κ° λ€μ΄μλμ§λ§ νμΈνλ©΄ μ¬μ©μ μΈμ¦μ΄ λλ€κ³ ν μ μμκΉμ? π€
μ¬μ©μμ μ λ³΄κ° μ€μ μ¬μ©μ μ 보μ μΌμΉνλμ§ νμΈν΄μ£Όμ΄μΌνμ§ μμκΉμ?
νμ¬λ email κ³Ό password μ λ³΄κ° λ€μ΄μμΌλ μ μ₯μμ μλ μ¬μ©μ μ 보μ λΉκ΅ν΄λ³Ό μ μκ² λ€μ π
| import java.util.Optional; | ||
|
|
||
| @Component | ||
| public class MyInterceptor implements HandlerInterceptor { |
There was a problem hiding this comment.
μΈμ¦μ μν Interceptor μ΄λ κ·Έμ λ§λ μ΄λ¦μ μ§μ΄μ£Όλ©΄ μ΄λ¨κΉμ? π
|
|
||
| import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
|
||
| class MemberJdbcRepositoryTest { |
There was a problem hiding this comment.
@JdbcTest λ₯Ό νμ©νλ©΄ Repository λΉμ μ μ£Όμ
λ°μ μ¬μ©ν μ μμ΅λλ€ :)
μ€νλ§μμ ν
μ€νΈλ₯Ό μ½κ²ν μ μλλ‘ μ 곡νλ κΈ°λ₯μ΄μ£ π
+) @Sql μ λν
μ΄μ
μ μ¬μ©νλ©΄ sql μ€ν¬λ¦½νΈ μ€νμ΄ κ°λ₯ν©λλ€ :)
λ¨ @JdbcTest μ λν
μ΄μ
μ μ¬μ©νλ©΄ ν
μ€νΈ μ€ν μ ~ν λ₯Ό νΈλμμ
λ²μλ‘ μ‘μμ
κ° ν
μ€νΈ λ©μλ μ€ν ν λ‘€λ°±μ μ€νν©λλ€.
μ¦, κ° ν μ€νΈ λ©μλμμ μ€νν 쿼리λ€μ λ‘€λ°±μ΄ λμ΄ ν μ€νΈ λ 립μ μΌλ‘ μ€νμ΄ κ°λ₯ν΄μ§λλ€.
sql μ€ν¬λ¦½νΈ λμ @BeforeEachμμ λλ―Έλ°μ΄ν°λ₯Ό λ£μ΄μ£Όλ건 μ΄λ¨κΉμ?
| class MemberJdbcRepositoryTest { | |
| @Sql("classpath:test-data.sql") | |
| @JdbcTest( | |
| includeFilters = { | |
| @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Repository.class) | |
| } | |
| ) | |
| class MemberJdbcRepositoryTest { | |
| @Autowired | |
| private MemberRepository memberRepository; |
| import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
|
||
| @Configuration | ||
| public class WebConfig implements WebMvcConfigurer { |
| @Service | ||
| public class ItemService { |
There was a problem hiding this comment.
Repository ν
μ€νΈλ₯Ό μμ±ν΄μ£Όμ
¨μΌλ λ€μμ Service ν
μ€νΈλ μμ±ν΄λ³ΌκΉμ? π
@SpringBootTest μ λν
μ΄μ
μ μ¬μ©νλ©΄ ν
μ€νΈμμ μ€νλ§ νκ²½κ³Ό λΉμ·νκ² ν
μ€νΈκ° κ°λ₯ν©λλ€ :)
λΉμ μ£Όμ
λ°λκ²λ κ°λ₯νμ£ :)