|
|
@@ -0,0 +1,79 @@
|
|
|
+package nju.seec.helper.logic.service.impl;
|
|
|
+
|
|
|
+import nju.seec.helper.data.dao.SlideDAO;
|
|
|
+import nju.seec.helper.data.entity.Slide;
|
|
|
+import nju.seec.helper.logic.convert.DTOConvertFactory;
|
|
|
+import nju.seec.helper.logic.convert.EntityConvertFactory;
|
|
|
+import nju.seec.helper.logic.service.SlideService;
|
|
|
+import nju.seec.helper.logic.vo.Page;
|
|
|
+import nju.seec.helper.logic.vo.PageInfo;
|
|
|
+import nju.seec.helper.logic.vo.SlideOutlineVO;
|
|
|
+import nju.seec.helper.logic.vo.SlideVO;
|
|
|
+import nju.seec.helper.util.enums.SlideState;
|
|
|
+import nju.seec.helper.util.exception.HelperException;
|
|
|
+import nju.seec.helper.web.dto.SlideDTO;
|
|
|
+import org.springframework.cache.annotation.CacheConfig;
|
|
|
+import org.springframework.cache.annotation.CachePut;
|
|
|
+import org.springframework.cache.annotation.Cacheable;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author cst
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@CacheConfig(cacheNames = "slide")
|
|
|
+public class SlideServiceImpl implements SlideService {
|
|
|
+ private final SlideDAO slideDAO;
|
|
|
+
|
|
|
+ public SlideServiceImpl(SlideDAO slideDAO) {
|
|
|
+ this.slideDAO = slideDAO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Page<SlideOutlineVO> getSlides(String courseId, int page, int size, String key) {
|
|
|
+ org.springframework.data.domain.Page<Slide> slidePage = slideDAO.findByNameContains(key, PageRequest.of(page - 1, size));
|
|
|
+
|
|
|
+ List<SlideOutlineVO> slideOutlineVOs = slidePage.getContent().stream()
|
|
|
+ .map(slide -> EntityConvertFactory.convert(slide, SlideOutlineVO.class))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+
|
|
|
+ PageInfo pageInfo = PageInfo.of(slidePage.getNumber() + 1, slidePage.getNumberOfElements());
|
|
|
+
|
|
|
+ return Page.of(slideOutlineVOs, pageInfo);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Cacheable(key = "#slideId")
|
|
|
+ @Override
|
|
|
+ public SlideVO getOneSlide(int slideId) {
|
|
|
+ Slide slide = findSlideById(slideId);
|
|
|
+ return EntityConvertFactory.convert(slide, SlideVO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public SlideVO create(SlideDTO slideDTO) {
|
|
|
+ Slide slide = new Slide();
|
|
|
+ slide.setCourseId(slideDTO.getCourseId());
|
|
|
+ slide.setName(slideDTO.getName());
|
|
|
+ slide.setState(SlideState.DRAFT);
|
|
|
+ slide = slideDAO.save(slide);
|
|
|
+ return EntityConvertFactory.convert(slide, SlideVO.class);
|
|
|
+ }
|
|
|
+
|
|
|
+ @CachePut(key = "#slideDTO.id")
|
|
|
+ @Override
|
|
|
+ public SlideVO update(SlideDTO slideDTO) {
|
|
|
+ Slide slide = DTOConvertFactory.convert(slideDTO);
|
|
|
+ slideDAO.save(slide);
|
|
|
+ return getOneSlide(slideDTO.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Slide findSlideById(int slideId) {
|
|
|
+ return slideDAO.findById(slideId).orElseThrow(() -> HelperException.of(
|
|
|
+ HelperException.ExceptionType.NOT_FOUND, "找不到幻灯片")
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|