Skip to content

Commit

Permalink
Merge pull request #759 from woowacourse-teams/fix/757-template-tag
Browse files Browse the repository at this point in the history
  • Loading branch information
kyum-q authored Oct 9, 2024
2 parents e2115dd + 6eb92fe commit 7468bda
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ default Tag fetchByName(String name) {
() -> new CodeZapException(HttpStatus.NOT_FOUND, "이름이 " + name + "인 태그는 존재하지 않습니다."));
}

Optional<Tag> findByName(String name);
@Query(value = "SELECT * FROM tag WHERE tag.name = BINARY :name", nativeQuery = true)
Optional<Tag> findByName(@Param("name") String name);

@Query("select t.name from Tag t where t.name in :names")
List<String> findNameByNamesIn(@Param("names") List<String> names);
@Query(value = "SELECT * FROM tag WHERE tag.name COLLATE utf8mb4_bin IN :names", nativeQuery = true)
List<Tag> findAllByNames(@Param("names") List<String> names);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface TagRepository {

Optional<Tag> findByName(String name);

List<Tag> findByNameIn(List<String> names);
List<Tag> findAllByNames(List<String> names);

Tag save(Tag tag);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Long> templateIds);
void deleteByTemplateIds(@Param("templateIds") List<Long> templateIds);
}
2 changes: 1 addition & 1 deletion backend/src/main/java/codezap/tag/service/TagService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class TagService {

@Transactional
public void createTags(Template template, List<String> tagNames) {
List<Tag> existingTags = new ArrayList<>(tagRepository.findByNameIn(tagNames));
List<Tag> existingTags = new ArrayList<>(tagRepository.findAllByNames(tagNames));
List<String> existNames = existingTags.stream()
.map(Tag::getName)
.toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Optional<Tag> findByName(String name) {
}

@Override
public List<Tag> findByNameIn(List<String> names) {
public List<Tag> findAllByNames(List<String> names) {
return List.of();
}

Expand Down

0 comments on commit 7468bda

Please sign in to comment.