完成了Course的相关支持

This commit is contained in:
Chester.X 2024-07-04 22:49:05 +08:00
parent 9569a30a8a
commit fe58248efc
3 changed files with 176 additions and 81 deletions

View File

@ -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;
}

View File

@ -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<Course, Long> {
Optional<Course> findByName(String name);
Page<Course> findAllByOrderByIdDesc(Pageable pageable);
}

View File

@ -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<Course> 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<Course> 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<Course> existingCourse = courseRepository.findByName(name);
if (existingCourse.isPresent()) {
return null;
public List<Course> 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<Course> result = new ArrayList<>();
for (int pageNumber = startPageNumber; pageNumber <= endPageNumber; pageNumber++) {
Pageable pageable = PageRequest.of(pageNumber, pageSize);
Page<Course> 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<Course> getAllCourses() {
return courseRepository.findAll();
public long getCourseCount() {
return courseRepository.count();
}
}