From 01347c493a16d7607b3e54a2b7e6f3dd6727ad18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98jminkkk=E2=80=99?= <102847513+jminkkk@users.noreply.github.com> Date: Fri, 15 Nov 2024 22:44:42 +0900 Subject: [PATCH 1/2] =?UTF-8?q?refactor(template):=20=EC=83=88=EB=A1=9C=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=ED=95=98=EB=A0=A4=EB=8A=94=20=ED=83=9C?= =?UTF-8?q?=EA=B7=B8=EC=97=90=20=EC=A4=91=EB=B3=B5=EC=9D=B4=20=EC=9E=88?= =?UTF-8?q?=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20=EC=A0=9C=EA=B1=B0=20=ED=9B=84?= =?UTF-8?q?=20=EC=A0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/codezap/tag/service/TagService.java | 39 ++++++++++++------- .../codezap/tag/service/TagServiceTest.java | 17 ++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/codezap/tag/service/TagService.java b/backend/src/main/java/codezap/tag/service/TagService.java index a1156f5fb..0222be680 100644 --- a/backend/src/main/java/codezap/tag/service/TagService.java +++ b/backend/src/main/java/codezap/tag/service/TagService.java @@ -1,6 +1,5 @@ package codezap.tag.service; -import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; @@ -25,22 +24,34 @@ public class TagService { @Transactional public void createTags(Template template, List tagNames) { - List existingTags = new ArrayList<>(tagRepository.findAllByNames(tagNames)); - List existNames = existingTags.stream() + List existTags = tagRepository.findAllByNames(tagNames); + List existNames = getExistTagNames(existTags); + + List newTags = getOnlyNewTags(existNames, tagNames); + List savedNewTags = tagRepository.saveAll(newTags); + existTags.addAll(savedNewTags); + saveTemplateTags(template, existTags); + } + + private List getExistTagNames(List existTags) { + return existTags.stream() .map(Tag::getName) .toList(); + } + + private List getOnlyNewTags(List existNames, List tagNames) { + return tagNames.stream() + .distinct() + .filter(name -> !existNames.contains(name)) + .map(Tag::new) + .toList(); + } - List newTags = tagRepository.saveAll( - tagNames.stream() - .filter(name -> !existNames.contains(name)) - .map(Tag::new) - .toList() - ); - existingTags.addAll(newTags); - - for (Tag existingTag : existingTags) { - templateTagRepository.save(new TemplateTag(template, existingTag)); - } + private void saveTemplateTags(Template template, List tags) { + List templateTags = tags.stream() + .map(tag -> new TemplateTag(template, tag)) + .toList(); + templateTagRepository.saveAll(templateTags); } public List findAllByTemplate(Template template) { diff --git a/backend/src/test/java/codezap/tag/service/TagServiceTest.java b/backend/src/test/java/codezap/tag/service/TagServiceTest.java index 5c12fc2b9..ff71e8f37 100644 --- a/backend/src/test/java/codezap/tag/service/TagServiceTest.java +++ b/backend/src/test/java/codezap/tag/service/TagServiceTest.java @@ -68,6 +68,23 @@ void createTags_WhenExistTemplateTagContains() { .containsExactlyElementsOf(tagNames); } + @Test + @DisplayName("성공: 저장하려는 태그에 중복이 있는 경우 하나만") + void createTags_WhenDuplicatedTemplateTag() { + // given + Template template = createSavedTemplate(); + String tagName = "tag1"; + List tagNames = Arrays.asList(tagName, tagName); + + // when + sut.createTags(template, tagNames); + + // then + List savedTemplateTagNames = getSavedTemplateTagNames(template); + assertThat(savedTemplateTagNames).hasSize(1) + .containsExactly(tagName); + } + @Test @DisplayName("성공: 이미 있는 태그이지만 이 템플릿의 태그가 아닌 경우 템플릿 태그만 추가") void createTags_WhenExistTagContains() { From 664ea0d5ed3018303cef92d5d67b8f5596d90b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=98jminkkk=E2=80=99?= <102847513+jminkkk@users.noreply.github.com> Date: Fri, 15 Nov 2024 22:57:03 +0900 Subject: [PATCH 2/2] =?UTF-8?q?refactor(service):=20@DisplayName=20?= =?UTF-8?q?=EC=9E=90=EC=84=B8=ED=95=98=EA=B2=8C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/test/java/codezap/tag/service/TagServiceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/test/java/codezap/tag/service/TagServiceTest.java b/backend/src/test/java/codezap/tag/service/TagServiceTest.java index ff71e8f37..e4589eb7c 100644 --- a/backend/src/test/java/codezap/tag/service/TagServiceTest.java +++ b/backend/src/test/java/codezap/tag/service/TagServiceTest.java @@ -69,7 +69,7 @@ void createTags_WhenExistTemplateTagContains() { } @Test - @DisplayName("성공: 저장하려는 태그에 중복이 있는 경우 하나만") + @DisplayName("성공: 저장하려는 태그에 중복이 있는 경우 하나만 생성") void createTags_WhenDuplicatedTemplateTag() { // given Template template = createSavedTemplate();