From fe58248efc3579645ce3baacb56e658f284d8b67 Mon Sep 17 00:00:00 2001 From: "Chester.X" <2931709855@qq.com> Date: Thu, 4 Jul 2024 22:49:05 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86Course=E7=9A=84?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CourseManagement/model/Course.java | 20 +- .../repository/CourseRepository.java | 5 +- .../service/CourseService.java | 232 ++++++++++++------ 3 files changed, 176 insertions(+), 81 deletions(-) diff --git a/src/main/java/org/cmh/backend/CourseManagement/model/Course.java b/src/main/java/org/cmh/backend/CourseManagement/model/Course.java index caa9186..84847b1 100644 --- a/src/main/java/org/cmh/backend/CourseManagement/model/Course.java +++ b/src/main/java/org/cmh/backend/CourseManagement/model/Course.java @@ -2,21 +2,21 @@ package org.cmh.backend.CourseManagement.model; import jakarta.persistence.*; import lombok.Data; - +import lombok.Getter; +import lombok.Setter; @Data @Entity -@Table(name = "courses") +@Getter +@Setter public class Course { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - private String name; - private String description; - private String author; - private String sortOrder; - private String coverImagePath; - private String videoPath; - // getters and setters + private String title; + private String description; + private String order; + private String author; + private String videoPath; + private String imagePath; } \ No newline at end of file diff --git a/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java b/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java index ce25b61..d65909a 100644 --- a/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java +++ b/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java @@ -1,9 +1,10 @@ package org.cmh.backend.CourseManagement.repository; import org.cmh.backend.CourseManagement.model.Course; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; -import java.util.Optional; public interface CourseRepository extends JpaRepository { - Optional findByName(String name); + Page findAllByOrderByIdDesc(Pageable pageable); } \ No newline at end of file diff --git a/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java b/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java index f49676f..448c175 100644 --- a/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java +++ b/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java @@ -1,96 +1,190 @@ +//package org.cmh.backend.CourseManagement.service; +// +//import org.cmh.backend.CourseManagement.model.Course; +//import org.cmh.backend.CourseManagement.repository.CourseRepository; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.stereotype.Service; +//import org.springframework.web.multipart.MultipartFile; +// +//import java.io.IOException; +//import java.nio.file.Files; +//import java.nio.file.Path; +//import java.nio.file.Paths; +//import java.util.List; +//import java.util.Optional; +// +//@Service +//public class CourseService { +// +// @Autowired +// private CourseRepository courseRepository; +// +// public Course createCourse(String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) { +// Optional existingCourse = courseRepository.findByName(name); +// if (existingCourse.isPresent()) { +// return null; +// } +// +// Course course = new Course(); +// course.setName(name); +// course.setDescription(description); +// course.setAuthor(author); +// course.setSortOrder(sortOrder); +// +// try { +// if (coverImage != null && !coverImage.isEmpty()) { +// String coverImagePath = saveFile(coverImage); +// course.setCoverImagePath(coverImagePath); +// } +// if (video != null && !video.isEmpty()) { +// String videoPath = saveFile(video); +// course.setVideoPath(videoPath); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// return null; +// } +// +// return courseRepository.save(course); +// } +// +// public Course updateCourse(Long id, String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) { +// return courseRepository.findById(id) +// .map(course -> { +// course.setName(name); +// course.setDescription(description); +// course.setAuthor(author); +// course.setSortOrder(sortOrder); +// +// try { +// if (coverImage != null && !coverImage.isEmpty()) { +// String coverImagePath = saveFile(coverImage); +// course.setCoverImagePath(coverImagePath); +// } +// if (video != null && !video.isEmpty()) { +// String videoPath = saveFile(video); +// course.setVideoPath(videoPath); +// } +// } catch (IOException e) { +// e.printStackTrace(); +// return null; +// } +// +// return courseRepository.save(course); +// }).orElse(null); +// } +// +// private String saveFile(MultipartFile file) throws IOException { +// String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename(); +// Path filePath = Paths.get("uploads", filename); +// Files.createDirectories(filePath.getParent()); +// Files.write(filePath, file.getBytes()); +// return filePath.toString(); +// } +// +// public boolean deleteCourse(Long id) { +// return courseRepository.findById(id) +// .map(course -> { +// courseRepository.delete(course); +// return true; +// }).orElse(false); +// } +// +// public List getAllCourses() { +// return courseRepository.findAll(); +// } +//} package org.cmh.backend.CourseManagement.service; +import jakarta.persistence.EntityNotFoundException; +import org.cmh.backend.CourseManagement.dto.CourseRequest; import org.cmh.backend.CourseManagement.model.Course; import org.cmh.backend.CourseManagement.repository.CourseRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.util.ArrayList; import java.util.List; -import java.util.Optional; @Service public class CourseService { - @Autowired private CourseRepository courseRepository; - public Course createCourse(String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) { - Optional existingCourse = courseRepository.findByName(name); - if (existingCourse.isPresent()) { - return null; + public List getCoursesByRange(int start, int end) { + if (start < 0 || end <= start) { + throw new IllegalArgumentException("Invalid start or end range"); } + int pageSize = end - start; // 计算每页的大小 + int startPageNumber = start / pageSize; // 计算起始页码 + int endPageNumber = (end - 1) / pageSize; // 计算结束页码 + + List result = new ArrayList<>(); + + for (int pageNumber = startPageNumber; pageNumber <= endPageNumber; pageNumber++) { + Pageable pageable = PageRequest.of(pageNumber, pageSize); + Page coursePage = courseRepository.findAllByOrderByIdDesc(pageable); + + if (coursePage.hasContent()) { + result.addAll(coursePage.getContent()); + } else { + break; // 如果没有更多内容,提前退出 + } + } + + int startIndex = start % pageSize; + int endIndex = startIndex + (end - start); + + if (endIndex > result.size()) { + endIndex = result.size(); + } + + return result.subList(startIndex, endIndex); + } + + public void createCourse(CourseRequest request) { Course course = new Course(); - course.setName(name); - course.setDescription(description); - course.setAuthor(author); - course.setSortOrder(sortOrder); + course.setTitle(request.getTitle()); + course.setDescription(request.getDescription()); + course.setOrder(request.getOrder()); + course.setAuthor(request.getAuthor()); + course.setVideoPath(request.getVideoPath()); + course.setImagePath(request.getImagePath()); + courseRepository.save(course); + } - try { - if (coverImage != null && !coverImage.isEmpty()) { - String coverImagePath = saveFile(coverImage); - course.setCoverImagePath(coverImagePath); - } - if (video != null && !video.isEmpty()) { - String videoPath = saveFile(video); - course.setVideoPath(videoPath); - } - } catch (IOException e) { - e.printStackTrace(); - return null; + public void updateCourse(Long id, CourseRequest request) { + Course course = courseRepository.findById(id).orElse(null); + if (course != null) { + course.setTitle(request.getTitle()); + course.setDescription(request.getDescription()); + course.setOrder(request.getOrder()); + course.setAuthor(request.getAuthor()); + course.setVideoPath(request.getVideoPath()); + course.setImagePath(request.getImagePath()); + courseRepository.save(course); } - - return courseRepository.save(course); } - public Course updateCourse(Long id, String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) { - return courseRepository.findById(id) - .map(course -> { - course.setName(name); - course.setDescription(description); - course.setAuthor(author); - course.setSortOrder(sortOrder); - - try { - if (coverImage != null && !coverImage.isEmpty()) { - String coverImagePath = saveFile(coverImage); - course.setCoverImagePath(coverImagePath); - } - if (video != null && !video.isEmpty()) { - String videoPath = saveFile(video); - course.setVideoPath(videoPath); - } - } catch (IOException e) { - e.printStackTrace(); - return null; - } - - return courseRepository.save(course); - }).orElse(null); + public void deleteCourse(Long id) { + if (!courseRepository.existsById(id)) { + throw new EntityNotFoundException(); + } + courseRepository.deleteById(id); } - private String saveFile(MultipartFile file) throws IOException { - String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename(); - Path filePath = Paths.get("uploads", filename); - Files.createDirectories(filePath.getParent()); - Files.write(filePath, file.getBytes()); - return filePath.toString(); + public Course getCourseById(Long id) { + if (!courseRepository.existsById(id)) { + throw new EntityNotFoundException(); + } + return courseRepository.findById(id).orElse(null); } - public boolean deleteCourse(Long id) { - return courseRepository.findById(id) - .map(course -> { - courseRepository.delete(course); - return true; - }).orElse(false); - } - - public List getAllCourses() { - return courseRepository.findAll(); + public long getCourseCount() { + return courseRepository.count(); } } \ No newline at end of file