Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Manager 문제 관리 기능 추가 #18

Merged
merged 16 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
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<CrawlingQuizDTO> findCrawlingQuizList(@RequestBody Map<String, String> 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);
}

@GetMapping("/addQuiz/{id}")
public ResponseEntity<Quiz> addQuizInList(@PathVariable int id){
Quiz savedQuiz = manageService.insertSelectedQuizById(id);

return ResponseEntity.status(HttpStatus.OK).body(savedQuiz);
}

@GetMapping("/findSelectedQuiz")
public List<QuizDTO> findSelectedQuizList(){
return manageService.selectQuizListByDate(LocalDate.now().plusDays(1));
}

@DeleteMapping("/deleteQuiz/{id}")
public ResponseEntity<QuizDTO> deleteQuizInList(@PathVariable int id){
QuizDTO deletedQuiz = manageService.deleteQuizInListById(id);

return ResponseEntity.status(HttpStatus.OK).body(deletedQuiz);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package org.triumers.newsnippetback.Application.service;

import jakarta.transaction.Transactional;
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.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;

import java.time.LocalDate;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@Service
public class ManageService {

private final QuizRepository quizRepository;
private final CrawlingQuizRepository crawlingQuizRepository;
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) {
this.quizRepository = quizRepository;
this.crawlingQuizRepository = crawlingQuizRepository;
this.categoryRepository = categoryRepository;
this.mapper = mapper;
}

public List<CrawlingQuizDTO> selectCrawlingQuizListByDate(LocalDate date) {

List<CrawlingQuiz> crawlingQuizList = crawlingQuizRepository.findByNewsDate(date);

if (!crawlingQuizList.isEmpty()) {
List<CrawlingQuizDTO> crawlingQuizDTOList = crawlingQuizList.stream()
.map(crawlingQuiz -> mapper.map(crawlingQuiz, CrawlingQuizDTO.class))
.collect(Collectors.toList());

for (int i = 0; i < crawlingQuizList.size(); i++) {
CrawlingQuiz crawlingQuiz = crawlingQuizList.get(i);
Category category = categoryRepository.findById(crawlingQuiz.getCategoryId())
.orElseThrow(IllegalAccessError::new);
crawlingQuizDTOList.get(i).setCategory(category);

boolean isSelected = quizRepository.countByDateAndOriginQuizId
(nextDate, crawlingQuiz.getId()) > 0;
crawlingQuizDTOList.get(i).setSelected(isSelected);
}
return crawlingQuizDTOList;
} else {
// 이후에 크롤링 서버에 문제 생성 요청하기
throw new NoSuchElementException("문제 정보를 불러올 수 없음");
}
}

public CrawlingQuizDTO selectCrawlingQuizByID(int id) {
CrawlingQuiz crawlingQuiz = crawlingQuizRepository.findById(id).orElseThrow(IllegalAccessError::new);

if (crawlingQuiz != null) {
CrawlingQuizDTO crawlingQuizDTO = mapper.map(crawlingQuiz, CrawlingQuizDTO.class);

Category category = categoryRepository.findById(crawlingQuiz.getCategoryId())
.orElseThrow(IllegalAccessError::new);
crawlingQuizDTO.setCategory(category);

return crawlingQuizDTO;
}
throw new IllegalAccessError("문제 정보를 불러올 수 없음");
}

@Transactional
public Quiz insertSelectedQuizById(int id) {

CrawlingQuizDTO selectedQuiz = selectCrawlingQuizByID(id);
Quiz insertQuiz = mapper.map(selectedQuiz, Quiz.class);

insertQuiz.setNo(getMaxNo() + 1);
insertQuiz.setDate(nextDate);
insertQuiz.setCategoryId(selectedQuiz.getCategory().getId());
insertQuiz.setOriginQuizId(selectedQuiz.getId());

return quizRepository.save(insertQuiz);
}

public int getMaxNo() {
return quizRepository.countByDate(nextDate);
}

public List<QuizDTO> selectQuizListByDate(LocalDate date) {
List<Quiz> quizList = quizRepository.findByDateOrderByNoAsc(date);

if (!quizList.isEmpty()) {
List<QuizDTO> 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;
}
throw new NoSuchElementException("문제 정보를 불러올 수 없음");
}

@Transactional
public QuizDTO deleteQuizInListById(int id) {

Quiz deleteQuiz = quizRepository.findByOriginQuizIdAndDate(id, nextDate);

if (deleteQuiz != null) {

quizRepository.deleteById(deleteQuiz.getId());
List<Quiz> modifyQuizList = quizRepository
.findByDateAndNoGreaterThanOrderByNoAsc(nextDate, deleteQuiz.getNo());

for (Quiz modifyQuiz : modifyQuizList) {
modifyQuiz.setNo(modifyQuiz.getNo() - 1);
}
return mapper.map(deleteQuiz, QuizDTO.class);
}
throw new IllegalAccessError("문제 정보를 불러올 수 없음");
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -10,4 +12,8 @@ public static void main(String[] args) {
SpringApplication.run(NewsnippetBackApplication.class, args);
}

@Bean
public ModelMapper modelMapper() {
return new ModelMapper();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@NoArgsConstructor
@AllArgsConstructor
@ToString

public class Category {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDate;

@Entity
@Table(name = "tbl_crawling_quiz")
@Getter
@Setter
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package org.triumers.newsnippetback.domain.aggregate.entity;

import jakarta.persistence.*;
import lombok.*;

import java.time.LocalDate;

@Entity
@Table(name = "tbl_quiz")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString

public class Quiz {

@Id
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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;

private boolean isSelected;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.triumers.newsnippetback.domain.dto;

import jakarta.persistence.*;
import lombok.Data;
import org.triumers.newsnippetback.domain.aggregate.entity.Category;
import org.triumers.newsnippetback.domain.aggregate.entity.CrawlingQuiz;

import java.time.LocalDate;

@Data
public class QuizDTO {
private int id;

Expand Down
Original file line number Diff line number Diff line change
@@ -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<Category, Integer> {
}
Original file line number Diff line number Diff line change
@@ -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<CrawlingQuiz, Integer> {
List<CrawlingQuiz> findByNewsDate(LocalDate date);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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;
import java.util.List;
import java.util.Optional;

public interface QuizRepository extends JpaRepository<Quiz, Integer> {
List<Quiz> findByDateOrderByNoAsc(LocalDate date);

List<Quiz> findByDateAndNoGreaterThanOrderByNoAsc(LocalDate localDate, int no);

Integer countByDate(LocalDate localDate);

Integer countByDateAndOriginQuizId(LocalDate localDate, int id);

Quiz findByOriginQuizIdAndDate(int id, LocalDate date);
}
Loading
Loading