完成了Course的相关支持
This commit is contained in:
parent
9569a30a8a
commit
fe58248efc
@ -2,21 +2,21 @@ package org.cmh.backend.CourseManagement.model;
|
|||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
@Data
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "courses")
|
@Getter
|
||||||
|
@Setter
|
||||||
public class Course {
|
public class Course {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
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;
|
||||||
}
|
}
|
||||||
@ -1,9 +1,10 @@
|
|||||||
package org.cmh.backend.CourseManagement.repository;
|
package org.cmh.backend.CourseManagement.repository;
|
||||||
|
|
||||||
import org.cmh.backend.CourseManagement.model.Course;
|
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 org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public interface CourseRepository extends JpaRepository<Course, Long> {
|
public interface CourseRepository extends JpaRepository<Course, Long> {
|
||||||
Optional<Course> findByName(String name);
|
Page<Course> findAllByOrderByIdDesc(Pageable pageable);
|
||||||
}
|
}
|
||||||
@ -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;
|
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.model.Course;
|
||||||
import org.cmh.backend.CourseManagement.repository.CourseRepository;
|
import org.cmh.backend.CourseManagement.repository.CourseRepository;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
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.stereotype.Service;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.util.ArrayList;
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CourseService {
|
public class CourseService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CourseRepository courseRepository;
|
private CourseRepository courseRepository;
|
||||||
|
|
||||||
public Course createCourse(String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) {
|
public List<Course> getCoursesByRange(int start, int end) {
|
||||||
Optional<Course> existingCourse = courseRepository.findByName(name);
|
if (start < 0 || end <= start) {
|
||||||
if (existingCourse.isPresent()) {
|
throw new IllegalArgumentException("Invalid start or end range");
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 course = new Course();
|
||||||
course.setName(name);
|
course.setTitle(request.getTitle());
|
||||||
course.setDescription(description);
|
course.setDescription(request.getDescription());
|
||||||
course.setAuthor(author);
|
course.setOrder(request.getOrder());
|
||||||
course.setSortOrder(sortOrder);
|
course.setAuthor(request.getAuthor());
|
||||||
|
course.setVideoPath(request.getVideoPath());
|
||||||
try {
|
course.setImagePath(request.getImagePath());
|
||||||
if (coverImage != null && !coverImage.isEmpty()) {
|
courseRepository.save(course);
|
||||||
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 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course updateCourse(Long id, String name, String description, String author, String sortOrder, MultipartFile coverImage, MultipartFile video) {
|
public void deleteCourse(Long id) {
|
||||||
return courseRepository.findById(id)
|
if (!courseRepository.existsById(id)) {
|
||||||
.map(course -> {
|
throw new EntityNotFoundException();
|
||||||
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()) {
|
courseRepository.deleteById(id);
|
||||||
String videoPath = saveFile(video);
|
|
||||||
course.setVideoPath(videoPath);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return courseRepository.save(course);
|
public Course getCourseById(Long id) {
|
||||||
}).orElse(null);
|
if (!courseRepository.existsById(id)) {
|
||||||
|
throw new EntityNotFoundException();
|
||||||
|
}
|
||||||
|
return courseRepository.findById(id).orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String saveFile(MultipartFile file) throws IOException {
|
public long getCourseCount() {
|
||||||
String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
|
return courseRepository.count();
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user