From 0590555a240083225b36401ec5dec9933bd28c8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=AC=EC=9A=B0?= Date: Tue, 8 Oct 2024 19:11:59 +0900 Subject: [PATCH 1/2] =?UTF-8?q?fix(tag):=20tag=20name=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=EC=8B=9C=20=EB=8C=80=EC=86=8C=EB=AC=B8=EC=9E=90=20=EA=B5=AC?= =?UTF-8?q?=EB=B6=84=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../tag/repository/TagJpaRepository.java | 9 ++-- .../codezap/tag/repository/TagRepository.java | 2 +- .../repository/TemplateTagJpaRepository.java | 3 +- .../java/codezap/tag/service/TagService.java | 2 +- .../tag/repository/TagJpaRepositoryTest.java | 44 +++++++++++++++++++ .../repository/FakeTagRepository.java | 2 +- 6 files changed, 55 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java b/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java index 27d7e4257..021057b19 100644 --- a/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java +++ b/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java @@ -24,8 +24,11 @@ default Tag fetchByName(String name) { () -> new CodeZapException(HttpStatus.NOT_FOUND, "이름이 " + name + "인 태그는 존재하지 않습니다.")); } - Optional findByName(String name); + @Query(value = "SELECT * FROM tag WHERE tag.name = BINARY :name", nativeQuery = true) + Optional findByName(@Param("name") String name); - @Query("select t.name from Tag t where t.name in :names") - List findNameByNamesIn(@Param("names") List names); + @Query( + value = "SELECT * FROM tag WHERE tag.name COLLATE utf8mb4_bin IN :names", + nativeQuery = true) + List findAllByNames(@Param("names") List names); } diff --git a/backend/src/main/java/codezap/tag/repository/TagRepository.java b/backend/src/main/java/codezap/tag/repository/TagRepository.java index ab5760e51..bc547bda9 100644 --- a/backend/src/main/java/codezap/tag/repository/TagRepository.java +++ b/backend/src/main/java/codezap/tag/repository/TagRepository.java @@ -13,7 +13,7 @@ public interface TagRepository { Optional findByName(String name); - List findByNameIn(List names); + List findAllByNames(List names); Tag save(Tag tag); diff --git a/backend/src/main/java/codezap/tag/repository/TemplateTagJpaRepository.java b/backend/src/main/java/codezap/tag/repository/TemplateTagJpaRepository.java index 5363b6cf4..bf9430377 100644 --- a/backend/src/main/java/codezap/tag/repository/TemplateTagJpaRepository.java +++ b/backend/src/main/java/codezap/tag/repository/TemplateTagJpaRepository.java @@ -5,6 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import codezap.tag.domain.Tag; import codezap.template.domain.Template; @@ -51,5 +52,5 @@ WHERE t.id IN ( @Modifying(clearAutomatically = true) @Query("DELETE FROM TemplateTag t WHERE t.template.id in :templateIds") - void deleteByTemplateIds(List templateIds); + void deleteByTemplateIds(@Param("templateIds") List templateIds); } diff --git a/backend/src/main/java/codezap/tag/service/TagService.java b/backend/src/main/java/codezap/tag/service/TagService.java index 5a7076654..44d279b34 100644 --- a/backend/src/main/java/codezap/tag/service/TagService.java +++ b/backend/src/main/java/codezap/tag/service/TagService.java @@ -24,7 +24,7 @@ public class TagService { @Transactional public void createTags(Template template, List tagNames) { - List existingTags = new ArrayList<>(tagRepository.findByNameIn(tagNames)); + List existingTags = new ArrayList<>(tagRepository.findAllByNames(tagNames)); List existNames = existingTags.stream() .map(Tag::getName) .toList(); diff --git a/backend/src/test/java/codezap/tag/repository/TagJpaRepositoryTest.java b/backend/src/test/java/codezap/tag/repository/TagJpaRepositoryTest.java index 5025d4555..0ea3ff379 100644 --- a/backend/src/test/java/codezap/tag/repository/TagJpaRepositoryTest.java +++ b/backend/src/test/java/codezap/tag/repository/TagJpaRepositoryTest.java @@ -3,6 +3,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import java.util.List; import java.util.Optional; import org.junit.jupiter.api.DisplayName; @@ -57,6 +58,19 @@ void fetchByNameSuccess() { assertThat(actual).isEqualTo(tag); } + @Test + @DisplayName("태그명으로 태그 조회 성공 : 대소문자 구분") + void findByNameSuccessCaseSensitive() { + Tag lowerCaseTag = tagRepository.save(new Tag("java")); + Tag upperCaseTag = tagRepository.save(new Tag("Java")); + + var actual1 = tagRepository.fetchByName("java"); + var actual2 = tagRepository.fetchByName("Java"); + + assertThat(actual1).isEqualTo(lowerCaseTag); + assertThat(actual2).isEqualTo(upperCaseTag); + } + @Test @DisplayName("태그명으로 태그 조회 실패 : 존재하지 않는 태그명인 경우 에러가 발생한다.") void fetchByNameFailByNotExistsId() { @@ -89,4 +103,34 @@ void findByNameFailByNotExistsId() { assertThat(actual).isEmpty(); } } + + @Nested + @DisplayName("태그명 리스트로 태그 조회") + class FindAllByNames { + + @Test + @DisplayName("태그명 리스트로 태그 조회 성공") + void findAllByNamesSuccess() { + Tag lowerCaseTag1 = tagRepository.save(new Tag("java")); + Tag lowerCaseTag2 = tagRepository.save(new Tag("javascript")); + Tag lowerCaseTag3 = tagRepository.save(new Tag("typescript")); + Tag upperCaseTag1 = tagRepository.save(new Tag("Java")); + Tag upperCaseTag2 = tagRepository.save(new Tag("Javascript")); + Tag upperCaseTag3 = tagRepository.save(new Tag("Typescript")); + tagRepository.saveAll(List.of( + lowerCaseTag1, + lowerCaseTag2, + lowerCaseTag3, + upperCaseTag1, + upperCaseTag2, + upperCaseTag3)); + + var names = List.of(lowerCaseTag1.getName(), lowerCaseTag3.getName()); + var actual = tagRepository.findAllByNames(names); + + assertThat(actual) + .containsExactlyInAnyOrder(lowerCaseTag1, lowerCaseTag3) + .doesNotContain(lowerCaseTag2, upperCaseTag1, upperCaseTag2, upperCaseTag3); + } + } } diff --git a/backend/src/test/java/codezap/template/repository/FakeTagRepository.java b/backend/src/test/java/codezap/template/repository/FakeTagRepository.java index f7175c519..86f651a3c 100644 --- a/backend/src/test/java/codezap/template/repository/FakeTagRepository.java +++ b/backend/src/test/java/codezap/template/repository/FakeTagRepository.java @@ -42,7 +42,7 @@ public Optional findByName(String name) { } @Override - public List findByNameIn(List names) { + public List findAllByNames(List names) { return List.of(); } From 6eb92feed22b7d8ddcde0fe627943a35d94e105d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=9E=AC=EC=9A=B0?= Date: Tue, 8 Oct 2024 19:35:15 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor(repository):=20=EB=B6=88=ED=95=84?= =?UTF-8?q?=EC=9A=94=ED=95=9C=20=EA=B0=9C=ED=96=89=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/codezap/tag/repository/TagJpaRepository.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java b/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java index 021057b19..06a00d318 100644 --- a/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java +++ b/backend/src/main/java/codezap/tag/repository/TagJpaRepository.java @@ -27,8 +27,6 @@ default Tag fetchByName(String name) { @Query(value = "SELECT * FROM tag WHERE tag.name = BINARY :name", nativeQuery = true) Optional findByName(@Param("name") String name); - @Query( - value = "SELECT * FROM tag WHERE tag.name COLLATE utf8mb4_bin IN :names", - nativeQuery = true) + @Query(value = "SELECT * FROM tag WHERE tag.name COLLATE utf8mb4_bin IN :names", nativeQuery = true) List findAllByNames(@Param("names") List names); }