From 976ce6eaaad78d9210fd9c8cd01976c6a2365c12 Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 12:38:07 +0900 Subject: [PATCH 01/15] =?UTF-8?q?feat(manager)=20:=20=ED=81=AC=EB=A1=A4?= =?UTF-8?q?=EB=A7=81=EB=AC=B8=EC=A0=9C=20=EC=A1=B0=ED=9A=8C=20=EC=84=A0?= =?UTF-8?q?=ED=83=9D=20=EB=82=A0=EC=A7=9C=EB=A1=9C=20=EC=9D=BC=EC=B9=98?= =?UTF-8?q?=ED=95=98=EB=8A=94=20=EB=AC=B8=EC=A0=9C=20=EB=A6=AC=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20=EB=B0=8F=20=EC=A1=B0=ED=9A=8C?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8(4/2=20=EB=82=A0=EC=A7=9C?= =?UTF-8?q?=EB=A1=9C=20=EC=A1=B0=ED=9A=8C,=20=EC=B6=94=ED=9B=84=20Paramete?= =?UTF-8?q?rizedTest=EB=A1=9C=20=EB=B3=80=EA=B2=BD)=20=EC=84=A0=ED=83=9D?= =?UTF-8?q?=EB=90=9C=20=EB=AC=B8=EC=A0=9C=20=EC=A0=95=EB=B3=B4=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C=20=EB=B0=8F=20=EC=A1=B0=ED=9A=8C=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8(=EC=B6=94=ED=9B=84=20ParameterizedTest=EB=A1=9C=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 61 +++++++++++++++++++ .../NewsnippetBackApplication.java | 6 ++ .../domain/aggregate/entity/Category.java | 2 + .../domain/aggregate/entity/CrawlingQuiz.java | 2 + .../domain/dto/CrawlingQuizDTO.java | 32 ++++++++++ .../newsnippetback/domain/dto/QuizDTO.java | 1 - .../domain/repository/CategoryRepository.java | 7 +++ .../repository/CrawlingQuizRepository.java | 12 ++++ .../domain/repository/QuizRepository.java | 7 +++ .../service/ManageServiceTest.java | 34 +++++++++++ 10 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java create mode 100644 src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java create mode 100644 src/main/java/org/triumers/newsnippetback/domain/repository/CategoryRepository.java create mode 100644 src/main/java/org/triumers/newsnippetback/domain/repository/CrawlingQuizRepository.java create mode 100644 src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java create mode 100644 src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java new file mode 100644 index 0000000..f31775e --- /dev/null +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -0,0 +1,61 @@ +package org.triumers.newsnippetback.Application.service; + +import org.modelmapper.ModelMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.triumers.newsnippetback.domain.aggregate.entity.Category; +import org.triumers.newsnippetback.domain.aggregate.entity.CrawlingQuiz; +import org.triumers.newsnippetback.domain.dto.CrawlingQuizDTO; +import org.triumers.newsnippetback.domain.repository.CategoryRepository; +import org.triumers.newsnippetback.domain.repository.CrawlingQuizRepository; +import org.triumers.newsnippetback.domain.repository.QuizRepository; + +import java.time.LocalDate; +import java.util.List; +import java.util.stream.Collectors; + +@Service +public class ManageService { + + QuizRepository quizRepository; + CrawlingQuizRepository crawlingQuizRepository; + CategoryRepository categoryRepository; + ModelMapper mapper; + + @Autowired + public ManageService(QuizRepository quizRepository, CrawlingQuizRepository crawlingQuizRepository, + CategoryRepository categoryRepository, ModelMapper mapper) { + this.quizRepository = quizRepository; + this.crawlingQuizRepository = crawlingQuizRepository; + this.categoryRepository = categoryRepository; + this.mapper = mapper; + } + + public List selectCrawlingQuizListByDate(LocalDate date){ + + List crawlingQuizList = crawlingQuizRepository.findByNewsDate(date); + + List crawlingQuizDTOList = crawlingQuizList.stream() + .map(crawlingQuiz -> mapper.map(crawlingQuiz, CrawlingQuizDTO.class)) + .collect(Collectors.toList()); + + for (int i = 0; i < crawlingQuizList.size(); i++) { + Category category = categoryRepository.findById(crawlingQuizList.get(i).getCategoryId()) + .orElseThrow(IllegalAccessError::new); + crawlingQuizDTOList.get(i).setCategory(category); + } + + return crawlingQuizDTOList; + } + + public CrawlingQuizDTO selectCrawlingQuizByID(int id) { + CrawlingQuiz crawlingQuiz = crawlingQuizRepository.findById(id).orElseThrow(IllegalAccessError::new); + CrawlingQuizDTO crawlingQuizDTO = mapper.map(crawlingQuiz, CrawlingQuizDTO.class); + + Category category = categoryRepository.findById(crawlingQuiz.getCategoryId()) + .orElseThrow(IllegalAccessError::new); + crawlingQuizDTO.setCategory(category); + + return crawlingQuizDTO; + } +} diff --git a/src/main/java/org/triumers/newsnippetback/NewsnippetBackApplication.java b/src/main/java/org/triumers/newsnippetback/NewsnippetBackApplication.java index d6e1ac3..5f9d4f7 100644 --- a/src/main/java/org/triumers/newsnippetback/NewsnippetBackApplication.java +++ b/src/main/java/org/triumers/newsnippetback/NewsnippetBackApplication.java @@ -1,7 +1,9 @@ package org.triumers.newsnippetback; +import org.modelmapper.ModelMapper; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; @SpringBootApplication public class NewsnippetBackApplication { @@ -10,4 +12,8 @@ public static void main(String[] args) { SpringApplication.run(NewsnippetBackApplication.class, args); } + @Bean + public ModelMapper modelMapper() { + return new ModelMapper(); + } } diff --git a/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Category.java b/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Category.java index 0c1bd1e..fedfad7 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Category.java +++ b/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Category.java @@ -1,9 +1,11 @@ package org.triumers.newsnippetback.domain.aggregate.entity; import jakarta.persistence.*; +import lombok.Data; @Entity @Table(name = "tbl_category") +@Data public class Category { @Id diff --git a/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/CrawlingQuiz.java b/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/CrawlingQuiz.java index 98450fd..a6bf8d2 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/CrawlingQuiz.java +++ b/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/CrawlingQuiz.java @@ -1,11 +1,13 @@ package org.triumers.newsnippetback.domain.aggregate.entity; import jakarta.persistence.*; +import lombok.Data; import java.time.LocalDate; @Entity @Table(name = "tbl_crawling_quiz") +@Data public class CrawlingQuiz { @Id diff --git a/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java b/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java new file mode 100644 index 0000000..b931882 --- /dev/null +++ b/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java @@ -0,0 +1,32 @@ +package org.triumers.newsnippetback.domain.dto; + +import jakarta.persistence.Column; +import lombok.Data; +import org.triumers.newsnippetback.domain.aggregate.entity.Category; + +import java.time.LocalDate; + +@Data +public class CrawlingQuizDTO { + private int id; + + private String content; + + private String optionA; + + private String optionB; + + private String optionC; + + private String optionD; + + private String answer; + + private String explanation; + + private String newsLink; + + private LocalDate newsDate; + + private Category category; +} diff --git a/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java b/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java index 664fa1f..dbdfb75 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java +++ b/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java @@ -2,7 +2,6 @@ import jakarta.persistence.*; import org.triumers.newsnippetback.domain.aggregate.entity.Category; -import org.triumers.newsnippetback.domain.aggregate.entity.CrawlingQuiz; import java.time.LocalDate; diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/CategoryRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/CategoryRepository.java new file mode 100644 index 0000000..81d1c1a --- /dev/null +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/CategoryRepository.java @@ -0,0 +1,7 @@ +package org.triumers.newsnippetback.domain.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.triumers.newsnippetback.domain.aggregate.entity.Category; + +public interface CategoryRepository extends JpaRepository { +} diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/CrawlingQuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/CrawlingQuizRepository.java new file mode 100644 index 0000000..6316308 --- /dev/null +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/CrawlingQuizRepository.java @@ -0,0 +1,12 @@ +package org.triumers.newsnippetback.domain.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; +import org.triumers.newsnippetback.domain.aggregate.entity.CrawlingQuiz; + +import java.time.LocalDate; +import java.util.List; + +public interface CrawlingQuizRepository extends JpaRepository { + List findByNewsDate(LocalDate date); +} diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java new file mode 100644 index 0000000..2678f1f --- /dev/null +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -0,0 +1,7 @@ +package org.triumers.newsnippetback.domain.repository; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; + +public interface QuizRepository extends JpaRepository { +} diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java new file mode 100644 index 0000000..f16c1db --- /dev/null +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -0,0 +1,34 @@ +package org.triumers.newsnippetback.Application.service; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.triumers.newsnippetback.domain.dto.CrawlingQuizDTO; + +import java.time.LocalDate; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class ManageServiceTest { + + ManageService manageService; + + @Autowired + public ManageServiceTest(ManageService manageService) { + this.manageService = manageService; + } + + @Test + void selectCrawlingQuizListByDate(){ + List quizDTOList = manageService.selectCrawlingQuizListByDate(LocalDate.of(2024,4,2)); + assertNotNull(quizDTOList); + } + + @Test + void selectCrawlingQuizById(){ + CrawlingQuizDTO quizDTO = manageService.selectCrawlingQuizByID(1); + } + +} \ No newline at end of file From 7b2c33abdd50cf642d3eee7a4f671ab2bf0356bc Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 14:32:34 +0900 Subject: [PATCH 02/15] =?UTF-8?q?feat(Quiz)=20:=20=EC=B6=9C=EC=A0=9C?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=A0=80=EC=9E=A5=20=EC=B6=9C=EC=A0=9C=20?= =?UTF-8?q?=EC=84=A0=ED=83=9D=EB=90=9C=20=EB=AC=B8=EC=A0=9C=20=EC=A0=95?= =?UTF-8?q?=EB=B3=B4=20Quiz=20=EA=B0=9D=EC=B2=B4=EB=A1=9C=20=EB=B3=80?= =?UTF-8?q?=ED=99=98=20=ED=9B=84=20DB=EC=97=90=20=EC=A0=80=EC=9E=A5=20?= =?UTF-8?q?=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20(=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=EB=8A=94=20=EC=B6=94=ED=9B=84=20ParameterizedTest?= =?UTF-8?q?=EB=A1=9C=20=EB=B3=80=EA=B2=BD)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 24 +++++++++++++++++++ .../domain/aggregate/entity/Quiz.java | 2 ++ .../newsnippetback/domain/dto/QuizDTO.java | 2 ++ .../service/ManageServiceTest.java | 12 ++++++++++ 4 files changed, 40 insertions(+) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index f31775e..739e6c2 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -1,11 +1,15 @@ package org.triumers.newsnippetback.Application.service; +import jakarta.transaction.Transactional; import org.modelmapper.ModelMapper; +import org.modelmapper.convention.MatchingStrategies; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.triumers.newsnippetback.domain.aggregate.entity.Category; import org.triumers.newsnippetback.domain.aggregate.entity.CrawlingQuiz; +import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; import org.triumers.newsnippetback.domain.dto.CrawlingQuizDTO; +import org.triumers.newsnippetback.domain.dto.QuizDTO; import org.triumers.newsnippetback.domain.repository.CategoryRepository; import org.triumers.newsnippetback.domain.repository.CrawlingQuizRepository; import org.triumers.newsnippetback.domain.repository.QuizRepository; @@ -58,4 +62,24 @@ public CrawlingQuizDTO selectCrawlingQuizByID(int id) { return crawlingQuizDTO; } + + @Transactional + public List insertSelectedQuiz(List crawlingQuizDTOList) { + + mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); + List quizList = crawlingQuizDTOList.stream() + .map(crawlingQuizDTO -> mapper.map(crawlingQuizDTO, Quiz.class)) + .collect(Collectors.toList()); + + for (int i = 0; i < crawlingQuizDTOList.size(); i++) { + + CrawlingQuizDTO seletedQuiz = crawlingQuizDTOList.get(i); + + quizList.get(i).setNo(i+1); + quizList.get(i).setDate(LocalDate.now()); + quizList.get(i).setCategoryId(seletedQuiz.getCategory().getId()); + quizList.get(i).setOriginQuizId(seletedQuiz.getId()); + } + return quizRepository.saveAll(quizList); + } } diff --git a/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Quiz.java b/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Quiz.java index c87cd93..58fceac 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Quiz.java +++ b/src/main/java/org/triumers/newsnippetback/domain/aggregate/entity/Quiz.java @@ -1,11 +1,13 @@ package org.triumers.newsnippetback.domain.aggregate.entity; import jakarta.persistence.*; +import lombok.Data; import java.time.LocalDate; @Entity @Table(name = "tbl_quiz") +@Data public class Quiz { @Id diff --git a/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java b/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java index dbdfb75..71367df 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java +++ b/src/main/java/org/triumers/newsnippetback/domain/dto/QuizDTO.java @@ -1,10 +1,12 @@ package org.triumers.newsnippetback.domain.dto; import jakarta.persistence.*; +import lombok.Data; import org.triumers.newsnippetback.domain.aggregate.entity.Category; import java.time.LocalDate; +@Data public class QuizDTO { private int id; diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index f16c1db..d6bfd99 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -3,6 +3,7 @@ import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; import org.triumers.newsnippetback.domain.dto.CrawlingQuizDTO; import java.time.LocalDate; @@ -23,12 +24,23 @@ public ManageServiceTest(ManageService manageService) { @Test void selectCrawlingQuizListByDate(){ List quizDTOList = manageService.selectCrawlingQuizListByDate(LocalDate.of(2024,4,2)); + assertNotNull(quizDTOList); } @Test void selectCrawlingQuizById(){ CrawlingQuizDTO quizDTO = manageService.selectCrawlingQuizByID(1); + + assertNotNull(quizDTO); + } + + @Test + void addSelectedQuiz(){ + List selectedCrawlingQuizList = manageService.selectCrawlingQuizListByDate(LocalDate.of(2024,4,2)); + List savedQuizList = manageService.insertSelectedQuiz(selectedCrawlingQuizList); + + assertNotNull(savedQuizList); } } \ No newline at end of file From 80b8e5d1f57187a62b1099721dfd90ad77acaec9 Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 14:39:54 +0900 Subject: [PATCH 03/15] =?UTF-8?q?fix(Quiz)=20:=20=EC=B6=9C=EC=A0=9C=20?= =?UTF-8?q?=EB=82=A0=EC=A7=9C=20=EC=88=98=EC=A0=95=20=EC=B6=9C=EC=A0=9C?= =?UTF-8?q?=EB=90=A0=20=EB=82=A0=EC=A7=9C=20=EB=8B=B9=EC=9D=BC->=EB=8B=A4?= =?UTF-8?q?=EC=9D=8C=EB=82=A0=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../newsnippetback/Application/service/ManageService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index 739e6c2..a6e3dd5 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -57,7 +57,7 @@ public CrawlingQuizDTO selectCrawlingQuizByID(int id) { CrawlingQuizDTO crawlingQuizDTO = mapper.map(crawlingQuiz, CrawlingQuizDTO.class); Category category = categoryRepository.findById(crawlingQuiz.getCategoryId()) - .orElseThrow(IllegalAccessError::new); + .orElseThrow(IllegalAccessError::new); crawlingQuizDTO.setCategory(category); return crawlingQuizDTO; @@ -76,7 +76,7 @@ public List insertSelectedQuiz(List crawlingQuizDTOList) CrawlingQuizDTO seletedQuiz = crawlingQuizDTOList.get(i); quizList.get(i).setNo(i+1); - quizList.get(i).setDate(LocalDate.now()); + quizList.get(i).setDate(LocalDate.now().plusDays(1)); quizList.get(i).setCategoryId(seletedQuiz.getCategory().getId()); quizList.get(i).setOriginQuizId(seletedQuiz.getId()); } From 80658170b2a687c8de056edb619dde06498c45e3 Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 14:52:20 +0900 Subject: [PATCH 04/15] =?UTF-8?q?feat(Quiz)=20:=20=EC=B6=9C=EC=A0=9C?= =?UTF-8?q?=ED=95=A0=20=EB=AC=B8=EC=A0=9C=20=EC=A1=B0=ED=9A=8C=20=EB=82=A0?= =?UTF-8?q?=EC=A7=9C=EC=97=90=20=EB=94=B0=EB=9D=BC=20=EC=B6=9C=EC=A0=9C?= =?UTF-8?q?=ED=95=A0=20=EB=AC=B8=EC=A0=9C=20Quiz=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=EC=97=90=EC=84=9C=20=EC=A1=B0=ED=9A=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 16 ++++++++++++++++ .../domain/repository/QuizRepository.java | 4 ++++ .../Application/service/ManageServiceTest.java | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index a6e3dd5..bffc75d 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -82,4 +82,20 @@ public List insertSelectedQuiz(List crawlingQuizDTOList) } return quizRepository.saveAll(quizList); } + + public List selectQuizListByDate(LocalDate date) { + List quizList = quizRepository.findByDateOrderByNoAsc(date); + + List quizDTOList = quizList.stream() + .map(quiz -> mapper.map(quiz, QuizDTO.class)) + .collect(Collectors.toList()); + + for (int i = 0; i < quizList.size(); i++) { + Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) + .orElseThrow(IllegalAccessError::new); + quizDTOList.get(i).setCategory(category); + } + + return quizDTOList; + } } diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java index 2678f1f..b5e4574 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -3,5 +3,9 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; +import java.time.LocalDate; +import java.util.List; + public interface QuizRepository extends JpaRepository { + List findByDateOrderByNoAsc(LocalDate date); } diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index d6bfd99..af70fdd 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -5,6 +5,7 @@ import org.springframework.boot.test.context.SpringBootTest; import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; import org.triumers.newsnippetback.domain.dto.CrawlingQuizDTO; +import org.triumers.newsnippetback.domain.dto.QuizDTO; import java.time.LocalDate; import java.util.List; @@ -43,4 +44,10 @@ void addSelectedQuiz(){ assertNotNull(savedQuizList); } + @Test + void selectQuizListByDate(){ + List savedQuizList = manageService.selectQuizListByDate(LocalDate.of(2024,4,5)); + + } + } \ No newline at end of file From e839a1b66b8cf313553e94b059bf60d6c8f57ac4 Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 15:42:24 +0900 Subject: [PATCH 05/15] =?UTF-8?q?feat(Quiz)=20:=20=EC=B6=9C=EC=A0=9C=20?= =?UTF-8?q?=EB=AC=B8=EC=A0=9C=20=EC=82=AD=EC=A0=9C=20=EC=B6=9C=EC=A0=9C?= =?UTF-8?q?=EB=90=A0=20=EB=AC=B8=EC=A0=9C=20=EB=A6=AC=EC=8A=A4=ED=8A=B8?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=EC=B6=9C=EC=A0=9C=EB=90=A0=20=EB=AC=B8?= =?UTF-8?q?=EC=A0=9C=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 19 +++++++++++++++++++ .../domain/repository/QuizRepository.java | 4 ++++ .../service/ManageServiceTest.java | 9 ++++++++- 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index bffc75d..2689996 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -98,4 +98,23 @@ public List selectQuizListByDate(LocalDate date) { return quizDTOList; } + + @Transactional + public QuizDTO deleteQuizInListById(int id) { + Quiz deleteQuiz = quizRepository.findById(id).orElseThrow(IllegalAccessError::new); + + if (deleteQuiz != null) { + + quizRepository.deleteById(id); + List modifyQuizList = quizRepository + .findByDateAndNoGreaterThanOrderByNoAsc(LocalDate.now().plusDays(1), deleteQuiz.getNo()); + + for (Quiz modifyQuiz : modifyQuizList) { + modifyQuiz.setNo(modifyQuiz.getNo() - 1); + } + + return mapper.map(deleteQuiz, QuizDTO.class); + } + return null; + } } diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java index b5e4574..f73ed4e 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -8,4 +8,8 @@ public interface QuizRepository extends JpaRepository { List findByDateOrderByNoAsc(LocalDate date); + + List findByDateAndNoGreaterThan(LocalDate localDate, int no); + + List findByDateAndNoGreaterThanOrderByNoAsc(LocalDate localDate, int no); } diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index af70fdd..ed221bf 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -46,8 +46,15 @@ void addSelectedQuiz(){ @Test void selectQuizListByDate(){ - List savedQuizList = manageService.selectQuizListByDate(LocalDate.of(2024,4,5)); + List savedQuizList = manageService.selectQuizListByDate(LocalDate.now().plusDays(1)); + assertNotNull(savedQuizList); + } + @Test + void deleteQuizInList(){ + QuizDTO quizDTO = manageService.deleteQuizInListById(1); + assertNotNull(quizDTO); } + } \ No newline at end of file From ecb2c61c32ac9c812ba7ba2bc99f0de4b9de6641 Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 16:09:43 +0900 Subject: [PATCH 06/15] =?UTF-8?q?style(Quiz)=20:=20max=EC=9D=B8=EB=8D=B1?= =?UTF-8?q?=EC=8A=A4=EB=A1=9C=20=EC=9D=B8=EB=8D=B1=EC=8A=A4=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EC=B6=9C=EC=A0=9C=20=EB=AC=B8=EC=A0=9C=EB=A5=BC=20?= =?UTF-8?q?=EC=A0=80=EC=9E=A5=ED=95=98=EB=8A=94=20=EA=B2=BD=EC=9A=B0=20max?= =?UTF-8?q?=20=EC=9D=B8=EB=8D=B1=EC=8A=A4=20=EA=B0=92=20=EC=B0=BE=EC=95=84?= =?UTF-8?q?=EC=84=9C=20=EC=88=98=EC=A0=95=EB=8F=84=20=ED=95=A8=EA=BB=98=20?= =?UTF-8?q?=EA=B0=80=EB=8A=A5=ED=95=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 20 +++++++++++-------- .../domain/repository/QuizRepository.java | 4 ++++ .../service/ManageServiceTest.java | 5 +++++ 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index 2689996..a8b3e9f 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -35,7 +35,7 @@ public ManageService(QuizRepository quizRepository, CrawlingQuizRepository crawl this.mapper = mapper; } - public List selectCrawlingQuizListByDate(LocalDate date){ + public List selectCrawlingQuizListByDate(LocalDate date) { List crawlingQuizList = crawlingQuizRepository.findByNewsDate(date); @@ -45,7 +45,7 @@ public List selectCrawlingQuizListByDate(LocalDate date){ for (int i = 0; i < crawlingQuizList.size(); i++) { Category category = categoryRepository.findById(crawlingQuizList.get(i).getCategoryId()) - .orElseThrow(IllegalAccessError::new); + .orElseThrow(IllegalAccessError::new); crawlingQuizDTOList.get(i).setCategory(category); } @@ -57,7 +57,7 @@ public CrawlingQuizDTO selectCrawlingQuizByID(int id) { CrawlingQuizDTO crawlingQuizDTO = mapper.map(crawlingQuiz, CrawlingQuizDTO.class); Category category = categoryRepository.findById(crawlingQuiz.getCategoryId()) - .orElseThrow(IllegalAccessError::new); + .orElseThrow(IllegalAccessError::new); crawlingQuizDTO.setCategory(category); return crawlingQuizDTO; @@ -71,18 +71,24 @@ public List insertSelectedQuiz(List crawlingQuizDTOList) .map(crawlingQuizDTO -> mapper.map(crawlingQuizDTO, Quiz.class)) .collect(Collectors.toList()); + int index = getMaxIndex() + 1; for (int i = 0; i < crawlingQuizDTOList.size(); i++) { - + CrawlingQuizDTO seletedQuiz = crawlingQuizDTOList.get(i); - - quizList.get(i).setNo(i+1); + + quizList.get(i).setNo(index++); quizList.get(i).setDate(LocalDate.now().plusDays(1)); quizList.get(i).setCategoryId(seletedQuiz.getCategory().getId()); quizList.get(i).setOriginQuizId(seletedQuiz.getId()); } + return quizRepository.saveAll(quizList); } + public int getMaxIndex() { + return quizRepository.countByDate(LocalDate.now().plusDays(1)); + } + public List selectQuizListByDate(LocalDate date) { List quizList = quizRepository.findByDateOrderByNoAsc(date); @@ -95,7 +101,6 @@ public List selectQuizListByDate(LocalDate date) { .orElseThrow(IllegalAccessError::new); quizDTOList.get(i).setCategory(category); } - return quizDTOList; } @@ -112,7 +117,6 @@ public QuizDTO deleteQuizInListById(int id) { for (Quiz modifyQuiz : modifyQuizList) { modifyQuiz.setNo(modifyQuiz.getNo() - 1); } - return mapper.map(deleteQuiz, QuizDTO.class); } return null; diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java index f73ed4e..506a0d9 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -1,6 +1,8 @@ package org.triumers.newsnippetback.domain.repository; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; import java.time.LocalDate; @@ -12,4 +14,6 @@ public interface QuizRepository extends JpaRepository { List findByDateAndNoGreaterThan(LocalDate localDate, int no); List findByDateAndNoGreaterThanOrderByNoAsc(LocalDate localDate, int no); + + Integer countByDate(LocalDate localDate); } diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index ed221bf..123dca2 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -56,5 +56,10 @@ void deleteQuizInList(){ assertNotNull(quizDTO); } + @Test + void getMaxIndex(){ + int result = manageService.getMaxIndex(); + } + } \ No newline at end of file From 05fad53a5b1d18b6bb12d8310e50aabc43044166 Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 17:04:26 +0900 Subject: [PATCH 07/15] =?UTF-8?q?feat(controller)=20:=20manage=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80=20manage=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B4=80=EB=A0=A8=20=EC=BB=A8=ED=8A=B8?= =?UTF-8?q?=EB=A1=A4=EB=9F=AC=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ManageController.java | 60 +++++++++++++++++++ .../Application/service/ManageService.java | 8 +-- .../service/ManageServiceTest.java | 6 -- 3 files changed, 64 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java diff --git a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java new file mode 100644 index 0000000..9de5c9a --- /dev/null +++ b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java @@ -0,0 +1,60 @@ +package org.triumers.newsnippetback.Application.controller; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.triumers.newsnippetback.Application.service.ManageService; +import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; +import org.triumers.newsnippetback.domain.dto.CrawlingQuizDTO; +import org.triumers.newsnippetback.domain.dto.QuizDTO; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/manage") +public class ManageController { + + private final ManageService manageService; + + @Autowired + public ManageController(ManageService manageService) { + this.manageService = manageService; + } + + @PostMapping("/findCrawlingQuiz") + public List findCrawlingQuizList(@RequestBody Map params) { + return manageService.selectCrawlingQuizListByDate(LocalDate.parse(params.get("date"), + DateTimeFormatter.ISO_DATE)); + } + + @GetMapping("/findCrawlingQuiz/{id}") + public CrawlingQuizDTO findCrawlingQuizById(@PathVariable int id){ + return manageService.selectCrawlingQuizByID(id); + } + + @PostMapping("/addQuiz") + public ResponseEntity> addQuizInList(@RequestBody List crawlingQuizDTOList){ + + List savedQuizList = manageService.insertSelectedQuiz(crawlingQuizDTOList); + + return ResponseEntity.status(HttpStatus.OK).body(savedQuizList); + } + + @GetMapping("findSelectedQuiz") + public List findSelectedQuizList(){ + return manageService.selectQuizListByDate(LocalDate.now().plusDays(1)); + } + + @DeleteMapping("deleteQuiz/{id}") + public ResponseEntity deleteQuizInList(@PathVariable int id){ + + QuizDTO deletedQuiz = manageService.deleteQuizInListById(id); + + return ResponseEntity.status(HttpStatus.OK).body(deletedQuiz); + } + +} diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index a8b3e9f..a01f25a 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -21,10 +21,10 @@ @Service public class ManageService { - QuizRepository quizRepository; - CrawlingQuizRepository crawlingQuizRepository; - CategoryRepository categoryRepository; - ModelMapper mapper; + private final QuizRepository quizRepository; + private final CrawlingQuizRepository crawlingQuizRepository; + private final CategoryRepository categoryRepository; + private final ModelMapper mapper; @Autowired public ManageService(QuizRepository quizRepository, CrawlingQuizRepository crawlingQuizRepository, diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index 123dca2..3e722f0 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -56,10 +56,4 @@ void deleteQuizInList(){ assertNotNull(quizDTO); } - @Test - void getMaxIndex(){ - int result = manageService.getMaxIndex(); - } - - } \ No newline at end of file From aea0afeb0d8864e4116643fc2b79af23c56db22c Mon Sep 17 00:00:00 2001 From: hso Date: Thu, 4 Apr 2024 18:01:34 +0900 Subject: [PATCH 08/15] =?UTF-8?q?test(managerService)=20:=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20param=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C?= =?UTF-8?q?=EC=97=90=20ParameterizedTest=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 58 +++++++++++-------- .../domain/repository/QuizRepository.java | 2 - .../service/ManageServiceTest.java | 38 ++++++++---- 3 files changed, 63 insertions(+), 35 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index a01f25a..71ec15f 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -16,6 +16,7 @@ import java.time.LocalDate; import java.util.List; +import java.util.NoSuchElementException; import java.util.stream.Collectors; @Service @@ -39,28 +40,36 @@ public List selectCrawlingQuizListByDate(LocalDate date) { List crawlingQuizList = crawlingQuizRepository.findByNewsDate(date); - List crawlingQuizDTOList = crawlingQuizList.stream() - .map(crawlingQuiz -> mapper.map(crawlingQuiz, CrawlingQuizDTO.class)) - .collect(Collectors.toList()); + if (crawlingQuizList != null) { + List crawlingQuizDTOList = crawlingQuizList.stream() + .map(crawlingQuiz -> mapper.map(crawlingQuiz, CrawlingQuizDTO.class)) + .collect(Collectors.toList()); - for (int i = 0; i < crawlingQuizList.size(); i++) { - Category category = categoryRepository.findById(crawlingQuizList.get(i).getCategoryId()) - .orElseThrow(IllegalAccessError::new); - crawlingQuizDTOList.get(i).setCategory(category); + for (int i = 0; i < crawlingQuizList.size(); i++) { + Category category = categoryRepository.findById(crawlingQuizList.get(i).getCategoryId()) + .orElseThrow(IllegalAccessError::new); + crawlingQuizDTOList.get(i).setCategory(category); + } + return crawlingQuizDTOList; + } else { + // 이후에 크롤링 서버에 문제 생성 요청하기 + throw new IllegalAccessError("문제 정보를 불러올 수 없음"); } - - return crawlingQuizDTOList; } public CrawlingQuizDTO selectCrawlingQuizByID(int id) { CrawlingQuiz crawlingQuiz = crawlingQuizRepository.findById(id).orElseThrow(IllegalAccessError::new); - CrawlingQuizDTO crawlingQuizDTO = mapper.map(crawlingQuiz, CrawlingQuizDTO.class); - Category category = categoryRepository.findById(crawlingQuiz.getCategoryId()) - .orElseThrow(IllegalAccessError::new); - crawlingQuizDTO.setCategory(category); + if (crawlingQuiz != null) { + CrawlingQuizDTO crawlingQuizDTO = mapper.map(crawlingQuiz, CrawlingQuizDTO.class); + + Category category = categoryRepository.findById(crawlingQuiz.getCategoryId()) + .orElseThrow(IllegalAccessError::new); + crawlingQuizDTO.setCategory(category); - return crawlingQuizDTO; + return crawlingQuizDTO; + } + throw new IllegalAccessError("문제 정보를 불러올 수 없음"); } @Transactional @@ -92,16 +101,19 @@ public int getMaxIndex() { public List selectQuizListByDate(LocalDate date) { List quizList = quizRepository.findByDateOrderByNoAsc(date); - List quizDTOList = quizList.stream() - .map(quiz -> mapper.map(quiz, QuizDTO.class)) - .collect(Collectors.toList()); + if (quizList != null) { + List quizDTOList = quizList.stream() + .map(quiz -> mapper.map(quiz, QuizDTO.class)) + .collect(Collectors.toList()); - for (int i = 0; i < quizList.size(); i++) { - Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) - .orElseThrow(IllegalAccessError::new); - quizDTOList.get(i).setCategory(category); + for (int i = 0; i < quizList.size(); i++) { + Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) + .orElseThrow(IllegalAccessError::new); + quizDTOList.get(i).setCategory(category); + } + return quizDTOList; } - return quizDTOList; + throw new IllegalAccessError("문제 정보를 불러올 수 없음"); } @Transactional @@ -119,6 +131,6 @@ public QuizDTO deleteQuizInListById(int id) { } return mapper.map(deleteQuiz, QuizDTO.class); } - return null; + throw new IllegalAccessError("문제 정보를 불러올 수 없음"); } } diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java index 506a0d9..b98001d 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -11,8 +11,6 @@ public interface QuizRepository extends JpaRepository { List findByDateOrderByNoAsc(LocalDate date); - List findByDateAndNoGreaterThan(LocalDate localDate, int no); - List findByDateAndNoGreaterThanOrderByNoAsc(LocalDate localDate, int no); Integer countByDate(LocalDate localDate); diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index 3e722f0..7b8fa65 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -1,6 +1,9 @@ package org.triumers.newsnippetback.Application.service; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.junit.jupiter.params.provider.ValueSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.triumers.newsnippetback.domain.aggregate.entity.Quiz; @@ -8,7 +11,10 @@ import org.triumers.newsnippetback.domain.dto.QuizDTO; import java.time.LocalDate; +import java.util.Date; import java.util.List; +import java.util.NoSuchElementException; +import java.util.stream.Stream; import static org.junit.jupiter.api.Assertions.*; @@ -22,16 +28,28 @@ public ManageServiceTest(ManageService manageService) { this.manageService = manageService; } - @Test - void selectCrawlingQuizListByDate(){ - List quizDTOList = manageService.selectCrawlingQuizListByDate(LocalDate.of(2024,4,2)); + static Stream getDate() { + return Stream.of( + LocalDate.of(2024, 4, 1), + LocalDate.of(2024, 4, 2), + LocalDate.of(2024, 4, 3), + LocalDate.of(2024, 4, 4), + LocalDate.of(2024, 4, 5) + ); + } + + @ParameterizedTest + @MethodSource("getDate") + void selectCrawlingQuizListByDate(LocalDate date){ + List quizDTOList = manageService.selectCrawlingQuizListByDate(date); assertNotNull(quizDTOList); } - @Test - void selectCrawlingQuizById(){ - CrawlingQuizDTO quizDTO = manageService.selectCrawlingQuizByID(1); + @ParameterizedTest + @ValueSource(ints = {1, 2, 3, 4, 5}) + void selectCrawlingQuizById(int id){ + CrawlingQuizDTO quizDTO = manageService.selectCrawlingQuizByID(id); assertNotNull(quizDTO); } @@ -50,10 +68,10 @@ void selectQuizListByDate(){ assertNotNull(savedQuizList); } - @Test - void deleteQuizInList(){ - QuizDTO quizDTO = manageService.deleteQuizInListById(1); + @ParameterizedTest + @ValueSource(ints = {6, 7, 8}) + void deleteQuizInListSuccess(int id){ + QuizDTO quizDTO = manageService.deleteQuizInListById(id); assertNotNull(quizDTO); } - } \ No newline at end of file From 36d07756fa9bb9fa4971954dc0e5c5789a69e862 Mon Sep 17 00:00:00 2001 From: hso Date: Fri, 5 Apr 2024 09:05:31 +0900 Subject: [PATCH 09/15] =?UTF-8?q?test(Manage)=20:=20=EC=98=88=EC=99=B8=20?= =?UTF-8?q?=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EC=98=88=EC=99=B8=EC=B2=98=EB=A6=AC=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=88=98=EC=A0=95=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/service/ManageService.java | 8 ++-- .../service/ManageServiceTest.java | 41 ++++++++++++++++++- 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index 71ec15f..4a76d75 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -40,7 +40,7 @@ public List selectCrawlingQuizListByDate(LocalDate date) { List crawlingQuizList = crawlingQuizRepository.findByNewsDate(date); - if (crawlingQuizList != null) { + if (!crawlingQuizList.isEmpty()) { List crawlingQuizDTOList = crawlingQuizList.stream() .map(crawlingQuiz -> mapper.map(crawlingQuiz, CrawlingQuizDTO.class)) .collect(Collectors.toList()); @@ -53,7 +53,7 @@ public List selectCrawlingQuizListByDate(LocalDate date) { return crawlingQuizDTOList; } else { // 이후에 크롤링 서버에 문제 생성 요청하기 - throw new IllegalAccessError("문제 정보를 불러올 수 없음"); + throw new NoSuchElementException("문제 정보를 불러올 수 없음"); } } @@ -101,7 +101,7 @@ public int getMaxIndex() { public List selectQuizListByDate(LocalDate date) { List quizList = quizRepository.findByDateOrderByNoAsc(date); - if (quizList != null) { + if (!quizList.isEmpty()) { List quizDTOList = quizList.stream() .map(quiz -> mapper.map(quiz, QuizDTO.class)) .collect(Collectors.toList()); @@ -113,7 +113,7 @@ public List selectQuizListByDate(LocalDate date) { } return quizDTOList; } - throw new IllegalAccessError("문제 정보를 불러올 수 없음"); + throw new NoSuchElementException("문제 정보를 불러올 수 없음"); } @Transactional diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index 7b8fa65..593aa06 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -47,7 +47,7 @@ void selectCrawlingQuizListByDate(LocalDate date){ } @ParameterizedTest - @ValueSource(ints = {1, 2, 3, 4, 5}) + @ValueSource(ints = {11, 12, 13}) void selectCrawlingQuizById(int id){ CrawlingQuizDTO quizDTO = manageService.selectCrawlingQuizByID(id); @@ -69,9 +69,46 @@ void selectQuizListByDate(){ } @ParameterizedTest - @ValueSource(ints = {6, 7, 8}) + @ValueSource(ints = {13, 14, 15}) void deleteQuizInListSuccess(int id){ QuizDTO quizDTO = manageService.deleteQuizInListById(id); assertNotNull(quizDTO); } + + @Test + void deleteQuizInListException(){ + int id = -1; + + assertThrows(IllegalAccessError.class, () -> { + manageService.deleteQuizInListById(id); + } ); + } + + @Test + void selectCrawlingQuizByIdException(){ + int id = -1; + + assertThrows(IllegalAccessError.class, () -> { + manageService.selectCrawlingQuizByID(id); + } ); + } + + @Test + void selectCrawlingQuizListByDateException(){ + LocalDate date = LocalDate.of(2020, 1, 1); + + assertThrows(NoSuchElementException.class, () -> { + manageService.selectCrawlingQuizListByDate(date); + } ); + } + + @Test + void selectQuizListByDateException(){ + LocalDate date = LocalDate.of(2020, 1, 1); + + assertThrows(NoSuchElementException.class, () -> { + manageService.selectQuizListByDate(date); + } ); + } + } \ No newline at end of file From b9f921e821706dd705ac207c05c3fd81ec1460a7 Mon Sep 17 00:00:00 2001 From: hso Date: Fri, 5 Apr 2024 10:41:57 +0900 Subject: [PATCH 10/15] =?UTF-8?q?feat(CrawlingQuiz)=20:=20=EC=B6=9C?= =?UTF-8?q?=EC=A0=9C,=20=EB=AF=B8=EC=B6=9C=EC=A0=9C=20=ED=99=95=EC=9D=B8?= =?UTF-8?q?=EC=9D=84=20=EC=9C=84=ED=95=9C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=ED=81=AC=EB=A1=A4=EB=A7=81=20=EB=AC=B8=EC=A0=9C=20?= =?UTF-8?q?=ED=99=95=EC=9D=B8=20=EC=8B=9C,=20=ED=99=94=EB=A9=B4=EC=97=90?= =?UTF-8?q?=EC=84=9C=20=ED=99=95=EC=9D=B8=ED=95=A0=20=EC=88=98=20=EC=9E=88?= =?UTF-8?q?=EB=8F=84=EB=A1=9D=20=EB=B3=80=EC=88=98=20=EB=B0=8F=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../newsnippetback/Application/service/ManageService.java | 7 ++++++- .../newsnippetback/domain/dto/CrawlingQuizDTO.java | 2 ++ .../newsnippetback/domain/repository/QuizRepository.java | 2 ++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index 4a76d75..6cb9099 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -46,9 +46,14 @@ public List selectCrawlingQuizListByDate(LocalDate date) { .collect(Collectors.toList()); for (int i = 0; i < crawlingQuizList.size(); i++) { - Category category = categoryRepository.findById(crawlingQuizList.get(i).getCategoryId()) + CrawlingQuiz crawlingQuiz = crawlingQuizList.get(i); + Category category = categoryRepository.findById(crawlingQuiz.getCategoryId()) .orElseThrow(IllegalAccessError::new); crawlingQuizDTOList.get(i).setCategory(category); + + boolean isSelected = quizRepository.countByDateAndOriginQuizId + (LocalDate.now().plusDays(1), crawlingQuiz.getId()) != 0; + crawlingQuizDTOList.get(i).setSelected(isSelected); } return crawlingQuizDTOList; } else { diff --git a/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java b/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java index b931882..d4d8e77 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java +++ b/src/main/java/org/triumers/newsnippetback/domain/dto/CrawlingQuizDTO.java @@ -29,4 +29,6 @@ public class CrawlingQuizDTO { private LocalDate newsDate; private Category category; + + private boolean isSelected; } diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java index b98001d..e95f525 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -14,4 +14,6 @@ public interface QuizRepository extends JpaRepository { List findByDateAndNoGreaterThanOrderByNoAsc(LocalDate localDate, int no); Integer countByDate(LocalDate localDate); + + Integer countByDateAndOriginQuizId(LocalDate localDate, int id); } From 9921dbe4abe88b02a5521dcc641610bcf8714e9e Mon Sep 17 00:00:00 2001 From: hso Date: Fri, 5 Apr 2024 12:25:36 +0900 Subject: [PATCH 11/15] =?UTF-8?q?style(Quiz)=20:=20isSelected=20=EC=A1=B0?= =?UTF-8?q?=EA=B1=B4=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../newsnippetback/Application/service/ManageService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index 6cb9099..3f6350c 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -52,7 +52,7 @@ public List selectCrawlingQuizListByDate(LocalDate date) { crawlingQuizDTOList.get(i).setCategory(category); boolean isSelected = quizRepository.countByDateAndOriginQuizId - (LocalDate.now().plusDays(1), crawlingQuiz.getId()) != 0; + (LocalDate.now().plusDays(1), crawlingQuiz.getId()) > 0; crawlingQuizDTOList.get(i).setSelected(isSelected); } return crawlingQuizDTOList; From 17bee71514353afa8e5d2989f5fd7f94dc2d72ae Mon Sep 17 00:00:00 2001 From: hso Date: Sun, 7 Apr 2024 21:07:12 +0900 Subject: [PATCH 12/15] =?UTF-8?q?feat(Quiz)=20:=20param=20List->int=20id=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20=ED=94=84=EB=A1=A0?= =?UTF-8?q?=ED=8A=B8=EC=97=90=EC=84=9C=20=ED=9A=A8=EC=9C=A8=EC=A0=81?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=82=AC=EC=9A=A9=ED=95=98=EA=B8=B0=20?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20=EB=AC=B8=EC=A0=9C=20=EC=9E=90=EC=B2=B4?= =?UTF-8?q?=EB=A5=BC=20=EB=B3=B4=EB=82=B4=EB=8A=94=20=EA=B2=83=EC=9D=B4=20?= =?UTF-8?q?=EC=95=84=EB=8B=88=EB=9D=BC=20id=20=ED=95=98=EB=82=98=EC=94=A9?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=ED=95=98=EB=8A=94=20=EB=B0=A9=EC=8B=9D?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ManageController.java | 8 +- .../Application/service/ManageService.java | 85 +++++++++---------- .../service/ManageServiceTest.java | 12 +-- 3 files changed, 48 insertions(+), 57 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java index 9de5c9a..9ede9ba 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java +++ b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java @@ -36,12 +36,12 @@ public CrawlingQuizDTO findCrawlingQuizById(@PathVariable int id){ return manageService.selectCrawlingQuizByID(id); } - @PostMapping("/addQuiz") - public ResponseEntity> addQuizInList(@RequestBody List crawlingQuizDTOList){ + @PostMapping("/addQuiz/{id}") + public ResponseEntity addQuizInList(@PathVariable int id){ - List savedQuizList = manageService.insertSelectedQuiz(crawlingQuizDTOList); + Quiz savedQuiz = manageService.insertSelectedQuizById(id); - return ResponseEntity.status(HttpStatus.OK).body(savedQuizList); + return ResponseEntity.status(HttpStatus.OK).body(savedQuiz); } @GetMapping("findSelectedQuiz") diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index 3f6350c..b920fda 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -2,7 +2,6 @@ import jakarta.transaction.Transactional; import org.modelmapper.ModelMapper; -import org.modelmapper.convention.MatchingStrategies; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.triumers.newsnippetback.domain.aggregate.entity.Category; @@ -52,7 +51,7 @@ public List selectCrawlingQuizListByDate(LocalDate date) { crawlingQuizDTOList.get(i).setCategory(category); boolean isSelected = quizRepository.countByDateAndOriginQuizId - (LocalDate.now().plusDays(1), crawlingQuiz.getId()) > 0; + (LocalDate.now().plusDays(1), crawlingQuiz.getId()) > 0; crawlingQuizDTOList.get(i).setSelected(isSelected); } return crawlingQuizDTOList; @@ -78,64 +77,56 @@ public CrawlingQuizDTO selectCrawlingQuizByID(int id) { } @Transactional - public List insertSelectedQuiz(List crawlingQuizDTOList) { + public Quiz insertSelectedQuizById(int id) { - mapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT); - List quizList = crawlingQuizDTOList.stream() - .map(crawlingQuizDTO -> mapper.map(crawlingQuizDTO, Quiz.class)) - .collect(Collectors.toList()); - - int index = getMaxIndex() + 1; - for (int i = 0; i < crawlingQuizDTOList.size(); i++) { + CrawlingQuizDTO selectedQuiz = selectCrawlingQuizByID(id); + Quiz insertQuiz = mapper.map(selectedQuiz, Quiz.class); - CrawlingQuizDTO seletedQuiz = crawlingQuizDTOList.get(i); + insertQuiz.setNo(getMaxNo() + 1); + insertQuiz.setDate(LocalDate.now().plusDays(1)); + insertQuiz.setCategoryId(selectedQuiz.getCategory().getId()); + insertQuiz.setOriginQuizId(selectedQuiz.getId()); - quizList.get(i).setNo(index++); - quizList.get(i).setDate(LocalDate.now().plusDays(1)); - quizList.get(i).setCategoryId(seletedQuiz.getCategory().getId()); - quizList.get(i).setOriginQuizId(seletedQuiz.getId()); - } - - return quizRepository.saveAll(quizList); - } + return quizRepository.save(insertQuiz); +} - public int getMaxIndex() { - return quizRepository.countByDate(LocalDate.now().plusDays(1)); - } +public int getMaxNo() { + return quizRepository.countByDate(LocalDate.now().plusDays(1)); +} - public List selectQuizListByDate(LocalDate date) { - List quizList = quizRepository.findByDateOrderByNoAsc(date); +public List selectQuizListByDate(LocalDate date) { + List quizList = quizRepository.findByDateOrderByNoAsc(date); - if (!quizList.isEmpty()) { - List quizDTOList = quizList.stream() - .map(quiz -> mapper.map(quiz, QuizDTO.class)) - .collect(Collectors.toList()); + if (!quizList.isEmpty()) { + List quizDTOList = quizList.stream() + .map(quiz -> mapper.map(quiz, QuizDTO.class)) + .collect(Collectors.toList()); - for (int i = 0; i < quizList.size(); i++) { - Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) - .orElseThrow(IllegalAccessError::new); - quizDTOList.get(i).setCategory(category); - } - return quizDTOList; + for (int i = 0; i < quizList.size(); i++) { + Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) + .orElseThrow(IllegalAccessError::new); + quizDTOList.get(i).setCategory(category); } - throw new NoSuchElementException("문제 정보를 불러올 수 없음"); + return quizDTOList; } + throw new NoSuchElementException("문제 정보를 불러올 수 없음"); +} - @Transactional - public QuizDTO deleteQuizInListById(int id) { - Quiz deleteQuiz = quizRepository.findById(id).orElseThrow(IllegalAccessError::new); +@Transactional +public QuizDTO deleteQuizInListById(int id) { + Quiz deleteQuiz = quizRepository.findById(id).orElseThrow(IllegalAccessError::new); - if (deleteQuiz != null) { + if (deleteQuiz != null) { - quizRepository.deleteById(id); - List modifyQuizList = quizRepository - .findByDateAndNoGreaterThanOrderByNoAsc(LocalDate.now().plusDays(1), deleteQuiz.getNo()); + quizRepository.deleteById(id); + List modifyQuizList = quizRepository + .findByDateAndNoGreaterThanOrderByNoAsc(LocalDate.now().plusDays(1), deleteQuiz.getNo()); - for (Quiz modifyQuiz : modifyQuizList) { - modifyQuiz.setNo(modifyQuiz.getNo() - 1); - } - return mapper.map(deleteQuiz, QuizDTO.class); + for (Quiz modifyQuiz : modifyQuizList) { + modifyQuiz.setNo(modifyQuiz.getNo() - 1); } - throw new IllegalAccessError("문제 정보를 불러올 수 없음"); + return mapper.map(deleteQuiz, QuizDTO.class); } + throw new IllegalAccessError("문제 정보를 불러올 수 없음"); +} } diff --git a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java index 593aa06..cc5d2dc 100644 --- a/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java +++ b/src/test/java/org/triumers/newsnippetback/Application/service/ManageServiceTest.java @@ -11,7 +11,6 @@ import org.triumers.newsnippetback.domain.dto.QuizDTO; import java.time.LocalDate; -import java.util.Date; import java.util.List; import java.util.NoSuchElementException; import java.util.stream.Stream; @@ -54,17 +53,18 @@ void selectCrawlingQuizById(int id){ assertNotNull(quizDTO); } - @Test - void addSelectedQuiz(){ - List selectedCrawlingQuizList = manageService.selectCrawlingQuizListByDate(LocalDate.of(2024,4,2)); - List savedQuizList = manageService.insertSelectedQuiz(selectedCrawlingQuizList); + @ParameterizedTest + @ValueSource(ints = {1, 2, 3}) + void addSelectedQuiz(int id){ + Quiz savedQuiz = manageService.insertSelectedQuizById(id); - assertNotNull(savedQuizList); + assertNotNull(savedQuiz); } @Test void selectQuizListByDate(){ List savedQuizList = manageService.selectQuizListByDate(LocalDate.now().plusDays(1)); + assertNotNull(savedQuizList); } From f5947c9dcf13fc35a237a21cbb8ad04ff32dc9f0 Mon Sep 17 00:00:00 2001 From: hso Date: Tue, 9 Apr 2024 10:29:23 +0900 Subject: [PATCH 13/15] =?UTF-8?q?style(CrawlingQuiz)=20:=20post=20->=20get?= =?UTF-8?q?=20=EB=B3=80=EA=B2=BD=20=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=EC=97=90=20=EB=94=B0=EB=9D=BC=20postMapping=EC=9D=84?= =?UTF-8?q?=20getMapping=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../newsnippetback/Application/controller/ManageController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java index 9ede9ba..88872b8 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java +++ b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java @@ -36,7 +36,7 @@ public CrawlingQuizDTO findCrawlingQuizById(@PathVariable int id){ return manageService.selectCrawlingQuizByID(id); } - @PostMapping("/addQuiz/{id}") + @GetMapping("/addQuiz/{id}") public ResponseEntity addQuizInList(@PathVariable int id){ Quiz savedQuiz = manageService.insertSelectedQuizById(id); From 504dcb7a1ca479eaa116ea7658eec7acad676f2c Mon Sep 17 00:00:00 2001 From: hso Date: Tue, 9 Apr 2024 10:33:09 +0900 Subject: [PATCH 14/15] =?UTF-8?q?style(Controller)=20:=20=EA=B2=BD?= =?UTF-8?q?=EB=A1=9C=20=EC=8A=A4=ED=83=80=EC=9D=BC=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Application/controller/ManageController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java index 88872b8..e5cd378 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java +++ b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java @@ -44,12 +44,12 @@ public ResponseEntity addQuizInList(@PathVariable int id){ return ResponseEntity.status(HttpStatus.OK).body(savedQuiz); } - @GetMapping("findSelectedQuiz") + @GetMapping("/findSelectedQuiz") public List findSelectedQuizList(){ return manageService.selectQuizListByDate(LocalDate.now().plusDays(1)); } - @DeleteMapping("deleteQuiz/{id}") + @DeleteMapping("/deleteQuiz/{id}") public ResponseEntity deleteQuizInList(@PathVariable int id){ QuizDTO deletedQuiz = manageService.deleteQuizInListById(id); From 58c8235268c1488662db39ff0f69efc552f9e405 Mon Sep 17 00:00:00 2001 From: hso Date: Tue, 9 Apr 2024 12:18:09 +0900 Subject: [PATCH 15/15] =?UTF-8?q?fix(Quiz)=20:=20delete=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=20=EC=88=98=EC=A0=95=20originId=20=EB=B0=9B=EC=95=84?= =?UTF-8?q?=EC=84=9C=20=EC=82=AD=EC=A0=9C=ED=95=98=EB=8A=94=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=EC=9C=BC=EB=A1=9C=20deleteQuiz=ED=95=A8=EC=88=98=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ManageController.java | 2 - .../Application/service/ManageService.java | 65 ++++++++++--------- .../domain/repository/QuizRepository.java | 3 + 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java index e5cd378..272c7d0 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java +++ b/src/main/java/org/triumers/newsnippetback/Application/controller/ManageController.java @@ -38,7 +38,6 @@ public CrawlingQuizDTO findCrawlingQuizById(@PathVariable int id){ @GetMapping("/addQuiz/{id}") public ResponseEntity addQuizInList(@PathVariable int id){ - Quiz savedQuiz = manageService.insertSelectedQuizById(id); return ResponseEntity.status(HttpStatus.OK).body(savedQuiz); @@ -51,7 +50,6 @@ public List findSelectedQuizList(){ @DeleteMapping("/deleteQuiz/{id}") public ResponseEntity deleteQuizInList(@PathVariable int id){ - QuizDTO deletedQuiz = manageService.deleteQuizInListById(id); return ResponseEntity.status(HttpStatus.OK).body(deletedQuiz); diff --git a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java index b920fda..a84bddb 100644 --- a/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java +++ b/src/main/java/org/triumers/newsnippetback/Application/service/ManageService.java @@ -26,6 +26,8 @@ public class ManageService { private final CategoryRepository categoryRepository; private final ModelMapper mapper; + final static LocalDate nextDate = LocalDate.now().plusDays(1); + @Autowired public ManageService(QuizRepository quizRepository, CrawlingQuizRepository crawlingQuizRepository, CategoryRepository categoryRepository, ModelMapper mapper) { @@ -51,7 +53,7 @@ public List selectCrawlingQuizListByDate(LocalDate date) { crawlingQuizDTOList.get(i).setCategory(category); boolean isSelected = quizRepository.countByDateAndOriginQuizId - (LocalDate.now().plusDays(1), crawlingQuiz.getId()) > 0; + (nextDate, crawlingQuiz.getId()) > 0; crawlingQuizDTOList.get(i).setSelected(isSelected); } return crawlingQuizDTOList; @@ -83,50 +85,51 @@ public Quiz insertSelectedQuizById(int id) { Quiz insertQuiz = mapper.map(selectedQuiz, Quiz.class); insertQuiz.setNo(getMaxNo() + 1); - insertQuiz.setDate(LocalDate.now().plusDays(1)); + insertQuiz.setDate(nextDate); insertQuiz.setCategoryId(selectedQuiz.getCategory().getId()); insertQuiz.setOriginQuizId(selectedQuiz.getId()); return quizRepository.save(insertQuiz); -} + } -public int getMaxNo() { - return quizRepository.countByDate(LocalDate.now().plusDays(1)); -} + public int getMaxNo() { + return quizRepository.countByDate(nextDate); + } -public List selectQuizListByDate(LocalDate date) { - List quizList = quizRepository.findByDateOrderByNoAsc(date); + public List selectQuizListByDate(LocalDate date) { + List quizList = quizRepository.findByDateOrderByNoAsc(date); - if (!quizList.isEmpty()) { - List quizDTOList = quizList.stream() - .map(quiz -> mapper.map(quiz, QuizDTO.class)) - .collect(Collectors.toList()); + if (!quizList.isEmpty()) { + List quizDTOList = quizList.stream() + .map(quiz -> mapper.map(quiz, QuizDTO.class)) + .collect(Collectors.toList()); - for (int i = 0; i < quizList.size(); i++) { - Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) - .orElseThrow(IllegalAccessError::new); - quizDTOList.get(i).setCategory(category); + for (int i = 0; i < quizList.size(); i++) { + Category category = categoryRepository.findById(quizList.get(i).getCategoryId()) + .orElseThrow(IllegalAccessError::new); + quizDTOList.get(i).setCategory(category); + } + return quizDTOList; } - return quizDTOList; + throw new NoSuchElementException("문제 정보를 불러올 수 없음"); } - throw new NoSuchElementException("문제 정보를 불러올 수 없음"); -} -@Transactional -public QuizDTO deleteQuizInListById(int id) { - Quiz deleteQuiz = quizRepository.findById(id).orElseThrow(IllegalAccessError::new); + @Transactional + public QuizDTO deleteQuizInListById(int id) { + + Quiz deleteQuiz = quizRepository.findByOriginQuizIdAndDate(id, nextDate); - if (deleteQuiz != null) { + if (deleteQuiz != null) { - quizRepository.deleteById(id); - List modifyQuizList = quizRepository - .findByDateAndNoGreaterThanOrderByNoAsc(LocalDate.now().plusDays(1), deleteQuiz.getNo()); + quizRepository.deleteById(deleteQuiz.getId()); + List modifyQuizList = quizRepository + .findByDateAndNoGreaterThanOrderByNoAsc(nextDate, deleteQuiz.getNo()); - for (Quiz modifyQuiz : modifyQuizList) { - modifyQuiz.setNo(modifyQuiz.getNo() - 1); + for (Quiz modifyQuiz : modifyQuizList) { + modifyQuiz.setNo(modifyQuiz.getNo() - 1); + } + return mapper.map(deleteQuiz, QuizDTO.class); } - return mapper.map(deleteQuiz, QuizDTO.class); + throw new IllegalAccessError("문제 정보를 불러올 수 없음"); } - throw new IllegalAccessError("문제 정보를 불러올 수 없음"); -} } diff --git a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java index e95f525..6a69868 100644 --- a/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java +++ b/src/main/java/org/triumers/newsnippetback/domain/repository/QuizRepository.java @@ -7,6 +7,7 @@ import java.time.LocalDate; import java.util.List; +import java.util.Optional; public interface QuizRepository extends JpaRepository { List findByDateOrderByNoAsc(LocalDate date); @@ -16,4 +17,6 @@ public interface QuizRepository extends JpaRepository { Integer countByDate(LocalDate localDate); Integer countByDateAndOriginQuizId(LocalDate localDate, int id); + + Quiz findByOriginQuizIdAndDate(int id, LocalDate date); }