Compare commits
No commits in common. "ba886c719137b85efa1800928bed7ed91c704371" and "fe58248efc3579645ce3baacb56e658f284d8b67" have entirely different histories.
ba886c7191
...
fe58248efc
@ -1,138 +1,67 @@
|
|||||||
package org.cmh.backend.CourseManagement.controller;
|
package org.cmh.backend.CourseManagement.controller;
|
||||||
|
|
||||||
import org.cmh.backend.CourseManagement.dto.GetCourseListResponse;
|
|
||||||
import org.cmh.backend.CourseManagement.dto.MessageResponse;
|
|
||||||
import org.cmh.backend.CourseManagement.dto.CourseRequest;
|
|
||||||
import org.cmh.backend.CourseManagement.dto.SearchCourseRequest;
|
|
||||||
import org.cmh.backend.CourseManagement.model.Course;
|
import org.cmh.backend.CourseManagement.model.Course;
|
||||||
import org.cmh.backend.CourseManagement.service.CourseService;
|
import org.cmh.backend.CourseManagement.service.CourseService;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.dao.DataIntegrityViolationException;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/courses")
|
@RequestMapping("/course")
|
||||||
public class CourseController {
|
public class CourseController {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(CourseController.class);
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CourseService courseService;
|
private CourseService courseService;
|
||||||
|
|
||||||
@GetMapping
|
@PostMapping("/create")
|
||||||
public ResponseEntity<GetCourseListResponse> getCoursesByRange(@RequestParam Integer start, @RequestParam Integer end, @RequestParam String token) {
|
public ResponseEntity<String> createCourse(
|
||||||
if (start >= end) {
|
@RequestParam("name") String name,
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
@RequestParam("description") String description,
|
||||||
|
@RequestParam("author") String author,
|
||||||
|
@RequestParam("sortOrder") String sortOrder,
|
||||||
|
@RequestParam("coverImage") MultipartFile coverImage,
|
||||||
|
@RequestParam("video") MultipartFile video) {
|
||||||
|
|
||||||
|
Course createdCourse = courseService.createCourse(name, description, author, sortOrder, coverImage, video);
|
||||||
|
if (createdCourse == null) {
|
||||||
|
return new ResponseEntity<>("课程已存在", HttpStatus.BAD_REQUEST);
|
||||||
}
|
}
|
||||||
long courseCount = courseService.getCourseCount();
|
return new ResponseEntity<>("课程创建成功", HttpStatus.OK);
|
||||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, courseService.getCoursesByRange(start, end)), HttpStatus.OK);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/{id}")
|
@PutMapping("/edit/{id}")
|
||||||
public ResponseEntity<Course> getCoursePage(@PathVariable Long id, @RequestParam String token) {
|
public ResponseEntity<String> updateCourse(
|
||||||
try {
|
@PathVariable Long id,
|
||||||
return new ResponseEntity<>(courseService.getCourseById(id), HttpStatus.OK);
|
@RequestParam("name") String name,
|
||||||
} catch (EntityNotFoundException e) {
|
@RequestParam("description") String description,
|
||||||
logger.error("Course not found with id: {}", id, e);
|
@RequestParam("author") String author,
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
@RequestParam("sortOrder") String sortOrder,
|
||||||
|
@RequestParam(value = "coverImage", required = false) MultipartFile coverImage,
|
||||||
|
@RequestParam(value = "video", required = false) MultipartFile video) {
|
||||||
|
|
||||||
|
Course updatedCourse = courseService.updateCourse(id, name, description, author, sortOrder, coverImage, video);
|
||||||
|
if (updatedCourse == null) {
|
||||||
|
return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
return new ResponseEntity<>("课程修改成功", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@DeleteMapping("/delete/{id}")
|
||||||
public ResponseEntity<MessageResponse> createCourse(@RequestBody CourseRequest request) {
|
public ResponseEntity<String> deleteCourse(@PathVariable Long id) {
|
||||||
try {
|
boolean isDeleted = courseService.deleteCourse(id);
|
||||||
courseService.createCourse(request);
|
if (!isDeleted) {
|
||||||
} catch (DataIntegrityViolationException e) {
|
return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
|
||||||
logger.error("Create course failed: Data integrity violation", e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("创建失败,课程已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Create course failed: {}", e.getMessage(), e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("创建失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>(new MessageResponse("创建成功"), HttpStatus.CREATED);
|
return new ResponseEntity<>("课程删除成功", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@GetMapping("/getAll")
|
||||||
public ResponseEntity<MessageResponse> updateCourse(@PathVariable Long id, @RequestBody CourseRequest request) {
|
public ResponseEntity<List<Course>> getAllCourses() {
|
||||||
try {
|
List<Course> courses = courseService.getAllCourses();
|
||||||
courseService.updateCourse(id, request);
|
return new ResponseEntity<>(courses, HttpStatus.OK);
|
||||||
} catch (DataIntegrityViolationException e) {
|
|
||||||
logger.error("Update course failed: Data integrity violation", e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("修改失败,新标题已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
|
||||||
} catch (EntityNotFoundException e) {
|
|
||||||
logger.error("Course not found with id: {}", id, e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("修改失败: 课程不存在"), HttpStatus.NOT_FOUND);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Update course failed: {}", e.getMessage(), e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("修改失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@DeleteMapping("/{id}")
|
|
||||||
public ResponseEntity<MessageResponse> deleteCourse(@PathVariable Long id, @RequestParam String token) {
|
|
||||||
try {
|
|
||||||
courseService.deleteCourse(id);
|
|
||||||
} catch (EntityNotFoundException e) {
|
|
||||||
logger.error("Course not found with id: {}", id, e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("删除失败,课程不存在"), HttpStatus.BAD_REQUEST);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Delete course failed: {}", e.getMessage(), e);
|
|
||||||
return new ResponseEntity<>(new MessageResponse("删除失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
|
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping("/search")
|
|
||||||
public ResponseEntity<GetCourseListResponse> searchCourses(@RequestParam String token,
|
|
||||||
@RequestParam(required = false) String title,
|
|
||||||
@RequestParam(required = false) String author,
|
|
||||||
@RequestParam(required = false) String description,
|
|
||||||
@RequestParam(required = false) String sortOrder,
|
|
||||||
@RequestParam Integer start,
|
|
||||||
@RequestParam Integer end) {
|
|
||||||
if (start >= end) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
SearchCourseRequest request = new SearchCourseRequest(token, title, author, description, sortOrder, start, end);
|
|
||||||
List<Course> courses = courseService.searchCourses(request);
|
|
||||||
long courseCount = courseService.getCourseCountByCriteria(title, author, description, sortOrder);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, courses), HttpStatus.OK);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Search courses failed with parameters: title={}, author={}, description={}, sortOrder={}, start={}, end={}",
|
|
||||||
title, author, description, sortOrder, start, end, e);
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@GetMapping("/sort")
|
|
||||||
public ResponseEntity<GetCourseListResponse> sortCourses(@RequestParam String token,
|
|
||||||
@RequestParam String sortField,
|
|
||||||
@RequestParam String sortDirection,
|
|
||||||
@RequestParam Integer start,
|
|
||||||
@RequestParam Integer end) {
|
|
||||||
if (start >= end) {
|
|
||||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
long courseCount = courseService.getCourseCount();
|
|
||||||
List<Course> sortedCourses = courseService.sortCourses(sortField, sortDirection, start, end);
|
|
||||||
|
|
||||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, sortedCourses), HttpStatus.OK);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Sort courses failed with parameters: sortField={}, sortDirection={}, start={}, end={}",
|
|
||||||
sortField, sortDirection, start, end, e);
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,16 +0,0 @@
|
|||||||
package org.cmh.backend.CourseManagement.dto;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.cmh.backend.Utils.JwtRequest;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class CourseRequest extends JwtRequest {
|
|
||||||
private String title;
|
|
||||||
private String description;
|
|
||||||
private String orderNo;
|
|
||||||
private String author;
|
|
||||||
private String videoPath;
|
|
||||||
private String imagePath;
|
|
||||||
}
|
|
||||||
@ -1,16 +0,0 @@
|
|||||||
package org.cmh.backend.CourseManagement.dto;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
import org.cmh.backend.CourseManagement.model.Course;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class GetCourseListResponse {
|
|
||||||
Long courseCount;
|
|
||||||
List<Course> courseList;
|
|
||||||
}
|
|
||||||
@ -1,12 +0,0 @@
|
|||||||
package org.cmh.backend.CourseManagement.dto;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class MessageResponse {
|
|
||||||
String message;
|
|
||||||
}
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
package org.cmh.backend.CourseManagement.dto;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
@AllArgsConstructor
|
|
||||||
public class SearchCourseRequest {
|
|
||||||
private String token;
|
|
||||||
private String title;
|
|
||||||
private String author;
|
|
||||||
private String description;
|
|
||||||
private String sortOrder;
|
|
||||||
private int start;
|
|
||||||
private int end;
|
|
||||||
}
|
|
||||||
@ -1,15 +0,0 @@
|
|||||||
package org.cmh.backend.CourseManagement.dto;
|
|
||||||
|
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.Setter;
|
|
||||||
|
|
||||||
@Getter
|
|
||||||
@Setter
|
|
||||||
public class UploadFileResponse extends MessageResponse {
|
|
||||||
private String url;
|
|
||||||
|
|
||||||
public UploadFileResponse(String message, String url) {
|
|
||||||
super(message);
|
|
||||||
this.url = url;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,9 +1,10 @@
|
|||||||
package org.cmh.backend.CourseManagement.model;
|
package org.cmh.backend.CourseManagement.model;
|
||||||
|
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
|
import lombok.Data;
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
@Data
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@Setter
|
@Setter
|
||||||
@ -14,7 +15,7 @@ public class Course {
|
|||||||
|
|
||||||
private String title;
|
private String title;
|
||||||
private String description;
|
private String description;
|
||||||
private String orderNo;
|
private String order;
|
||||||
private String author;
|
private String author;
|
||||||
private String videoPath;
|
private String videoPath;
|
||||||
private String imagePath;
|
private String imagePath;
|
||||||
|
|||||||
@ -4,10 +4,7 @@ import org.cmh.backend.CourseManagement.model.Course;
|
|||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
|
||||||
|
|
||||||
@Repository
|
public interface CourseRepository extends JpaRepository<Course, Long> {
|
||||||
public interface CourseRepository extends JpaRepository<Course, Long>, JpaSpecificationExecutor<Course> {
|
|
||||||
Page<Course> findAllByOrderByIdDesc(Pageable pageable);
|
Page<Course> findAllByOrderByIdDesc(Pageable pageable);
|
||||||
}
|
}
|
||||||
@ -1,82 +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.dto.CourseRequest;
|
||||||
import org.cmh.backend.CourseManagement.dto.SearchCourseRequest;
|
|
||||||
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.cmh.backend.CourseManagement.specification.CourseSpecification;
|
|
||||||
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.PageRequest;
|
||||||
import org.springframework.data.domain.Pageable;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import jakarta.persistence.EntityNotFoundException;
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class CourseService {
|
public class CourseService {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CourseRepository courseRepository;
|
private CourseRepository courseRepository;
|
||||||
|
|
||||||
public long getCourseCount() {
|
|
||||||
return courseRepository.count();
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Course> getCoursesByRange(int start, int end) {
|
public List<Course> getCoursesByRange(int start, int end) {
|
||||||
Pageable pageable = PageRequest.of(start, end - start);
|
if (start < 0 || end <= start) {
|
||||||
return courseRepository.findAll(pageable).getContent();
|
throw new IllegalArgumentException("Invalid start or end range");
|
||||||
}
|
}
|
||||||
|
|
||||||
public Course getCourseById(Long id) {
|
int pageSize = end - start; // 计算每页的大小
|
||||||
return courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
|
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) {
|
public void createCourse(CourseRequest request) {
|
||||||
Course course = new Course();
|
Course course = new Course();
|
||||||
course.setTitle(request.getTitle());
|
course.setTitle(request.getTitle());
|
||||||
course.setAuthor(request.getAuthor());
|
|
||||||
course.setDescription(request.getDescription());
|
course.setDescription(request.getDescription());
|
||||||
course.setOrderNo(request.getOrderNo());
|
course.setOrder(request.getOrder());
|
||||||
course.setVideoPath(request.getVideoPath()); // 确保保存视频路径
|
course.setAuthor(request.getAuthor());
|
||||||
course.setImagePath(request.getImagePath()); // 确保保存图片路径
|
course.setVideoPath(request.getVideoPath());
|
||||||
|
course.setImagePath(request.getImagePath());
|
||||||
courseRepository.save(course);
|
courseRepository.save(course);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateCourse(Long id, CourseRequest request) {
|
public void updateCourse(Long id, CourseRequest request) {
|
||||||
Course course = courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
|
Course course = courseRepository.findById(id).orElse(null);
|
||||||
course.setTitle(request.getTitle());
|
if (course != null) {
|
||||||
course.setAuthor(request.getAuthor());
|
course.setTitle(request.getTitle());
|
||||||
course.setDescription(request.getDescription());
|
course.setDescription(request.getDescription());
|
||||||
course.setOrderNo(request.getOrderNo());
|
course.setOrder(request.getOrder());
|
||||||
course.setVideoPath(request.getVideoPath()); // 确保更新视频路径
|
course.setAuthor(request.getAuthor());
|
||||||
course.setImagePath(request.getImagePath()); // 确保更新图片路径
|
course.setVideoPath(request.getVideoPath());
|
||||||
courseRepository.save(course);
|
course.setImagePath(request.getImagePath());
|
||||||
|
courseRepository.save(course);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deleteCourse(Long id) {
|
public void deleteCourse(Long id) {
|
||||||
|
if (!courseRepository.existsById(id)) {
|
||||||
|
throw new EntityNotFoundException();
|
||||||
|
}
|
||||||
courseRepository.deleteById(id);
|
courseRepository.deleteById(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Course> searchCourses(SearchCourseRequest request) {
|
public Course getCourseById(Long id) {
|
||||||
Pageable pageable = PageRequest.of(request.getStart(), request.getEnd() - request.getStart());
|
if (!courseRepository.existsById(id)) {
|
||||||
return courseRepository.findAll(
|
throw new EntityNotFoundException();
|
||||||
CourseSpecification.searchCourses(
|
}
|
||||||
request.getTitle(),
|
return courseRepository.findById(id).orElse(null);
|
||||||
request.getAuthor(),
|
|
||||||
request.getDescription(),
|
|
||||||
request.getSortOrder()
|
|
||||||
),
|
|
||||||
pageable
|
|
||||||
).getContent();
|
|
||||||
}
|
}
|
||||||
public long getCourseCountByCriteria(String title, String author, String description, String sortOrder) {
|
|
||||||
return courseRepository.count(CourseSpecification.searchCourses(title, author, description, sortOrder));
|
public long getCourseCount() {
|
||||||
}
|
return courseRepository.count();
|
||||||
public List<Course> sortCourses(String sortField, String sortDirection, int start, int end) {
|
|
||||||
Pageable pageable = PageRequest.of(start, end - start);
|
|
||||||
return courseRepository.findAll(
|
|
||||||
CourseSpecification.sortCourses(sortField, sortDirection),
|
|
||||||
pageable
|
|
||||||
).getContent();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,44 +0,0 @@
|
|||||||
package org.cmh.backend.CourseManagement.specification;
|
|
||||||
|
|
||||||
import org.cmh.backend.CourseManagement.model.Course;
|
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
|
||||||
|
|
||||||
import jakarta.persistence.criteria.Predicate;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class CourseSpecification {
|
|
||||||
|
|
||||||
public static Specification<Course> searchCourses(
|
|
||||||
String title, String author, String description, String sortOrder) {
|
|
||||||
|
|
||||||
return (root, query, criteriaBuilder) -> {
|
|
||||||
List<Predicate> predicates = new ArrayList<>();
|
|
||||||
|
|
||||||
if (title != null && !title.isEmpty()) {
|
|
||||||
predicates.add(criteriaBuilder.like(root.get("title"), "%" + title + "%"));
|
|
||||||
}
|
|
||||||
if (author != null && !author.isEmpty()) {
|
|
||||||
predicates.add(criteriaBuilder.like(root.get("author"), "%" + author + "%"));
|
|
||||||
}
|
|
||||||
if (description != null && !description.isEmpty()) {
|
|
||||||
predicates.add(criteriaBuilder.like(root.get("description"), "%" + description + "%"));
|
|
||||||
}
|
|
||||||
if (sortOrder != null && !sortOrder.isEmpty()) {
|
|
||||||
predicates.add(criteriaBuilder.like(root.get("orderNo"), "%" + sortOrder+ "%"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
|
|
||||||
};
|
|
||||||
}
|
|
||||||
public static Specification<Course> sortCourses(String sortField, String sortDirection) {
|
|
||||||
return (root, query, cb) -> {
|
|
||||||
if ("asc".equalsIgnoreCase(sortDirection)) {
|
|
||||||
query.orderBy(cb.asc(root.get(sortField)));
|
|
||||||
} else if ("desc".equalsIgnoreCase(sortDirection)) {
|
|
||||||
query.orderBy(cb.desc(root.get(sortField)));
|
|
||||||
}
|
|
||||||
return cb.conjunction();
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -20,9 +20,5 @@ spring.datasource.hikari.connection-timeout=30000
|
|||||||
server.servlet.encoding.enabled=true
|
server.servlet.encoding.enabled=true
|
||||||
server.servlet.encoding.force=true
|
server.servlet.encoding.force=true
|
||||||
server.servlet.encoding.charset=utf-8
|
server.servlet.encoding.charset=utf-8
|
||||||
# set the max size of a single file
|
|
||||||
spring.servlet.multipart.max-file-size=500MB
|
|
||||||
# set the max size of the total request
|
|
||||||
spring.servlet.multipart.max-request-size=500MB
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user