Compare commits
10 Commits
fe58248efc
...
ba886c7191
| Author | SHA1 | Date | |
|---|---|---|---|
| ba886c7191 | |||
| 05f3af0c77 | |||
| c42a3a55ec | |||
| d027e26f8c | |||
| f9272335f2 | |||
| 5885f365db | |||
| 780e4793de | |||
| f6d5112ad5 | |||
| dafdf82f5d | |||
| aae8147143 |
@ -1,67 +1,138 @@
|
||||
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.service.CourseService;
|
||||
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.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
||||
import java.util.List;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/course")
|
||||
@RequestMapping("/courses")
|
||||
public class CourseController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(CourseController.class);
|
||||
|
||||
@Autowired
|
||||
private CourseService courseService;
|
||||
|
||||
@PostMapping("/create")
|
||||
public ResponseEntity<String> createCourse(
|
||||
@RequestParam("name") String name,
|
||||
@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);
|
||||
@GetMapping
|
||||
public ResponseEntity<GetCourseListResponse> getCoursesByRange(@RequestParam Integer start, @RequestParam Integer end, @RequestParam String token) {
|
||||
if (start >= end) {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>("课程创建成功", HttpStatus.OK);
|
||||
long courseCount = courseService.getCourseCount();
|
||||
return new ResponseEntity<>(new GetCourseListResponse(courseCount, courseService.getCoursesByRange(start, end)), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping("/edit/{id}")
|
||||
public ResponseEntity<String> updateCourse(
|
||||
@PathVariable Long id,
|
||||
@RequestParam("name") String name,
|
||||
@RequestParam("description") String description,
|
||||
@RequestParam("author") String author,
|
||||
@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);
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Course> getCoursePage(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
return new ResponseEntity<>(courseService.getCourseById(id), HttpStatus.OK);
|
||||
} catch (EntityNotFoundException e) {
|
||||
logger.error("Course not found with id: {}", id, e);
|
||||
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||
}
|
||||
return new ResponseEntity<>("课程修改成功", HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/delete/{id}")
|
||||
public ResponseEntity<String> deleteCourse(@PathVariable Long id) {
|
||||
boolean isDeleted = courseService.deleteCourse(id);
|
||||
if (!isDeleted) {
|
||||
return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
|
||||
@PostMapping
|
||||
public ResponseEntity<MessageResponse> createCourse(@RequestBody CourseRequest request) {
|
||||
try {
|
||||
courseService.createCourse(request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
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<>("课程删除成功", HttpStatus.OK);
|
||||
return new ResponseEntity<>(new MessageResponse("创建成功"), HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@GetMapping("/getAll")
|
||||
public ResponseEntity<List<Course>> getAllCourses() {
|
||||
List<Course> courses = courseService.getAllCourses();
|
||||
return new ResponseEntity<>(courses, HttpStatus.OK);
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<MessageResponse> updateCourse(@PathVariable Long id, @RequestBody CourseRequest request) {
|
||||
try {
|
||||
courseService.updateCourse(id, request);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
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;
|
||||
}
|
||||
@ -0,0 +1,16 @@
|
||||
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;
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
package org.cmh.backend.CourseManagement.dto;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
public class MessageResponse {
|
||||
String message;
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
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;
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
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,10 +1,9 @@
|
||||
package org.cmh.backend.CourseManagement.model;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
@Data
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@ -15,7 +14,7 @@ public class Course {
|
||||
|
||||
private String title;
|
||||
private String description;
|
||||
private String order;
|
||||
private String orderNo;
|
||||
private String author;
|
||||
private String videoPath;
|
||||
private String imagePath;
|
||||
|
||||
@ -4,7 +4,10 @@ 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.stereotype.Repository;
|
||||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
|
||||
|
||||
public interface CourseRepository extends JpaRepository<Course, Long> {
|
||||
@Repository
|
||||
public interface CourseRepository extends JpaRepository<Course, Long>, JpaSpecificationExecutor<Course> {
|
||||
Page<Course> findAllByOrderByIdDesc(Pageable pageable);
|
||||
}
|
||||
@ -1,190 +1,82 @@
|
||||
//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.dto.SearchCourseRequest;
|
||||
import org.cmh.backend.CourseManagement.model.Course;
|
||||
import org.cmh.backend.CourseManagement.repository.CourseRepository;
|
||||
import org.cmh.backend.CourseManagement.specification.CourseSpecification;
|
||||
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 java.util.ArrayList;
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class CourseService {
|
||||
|
||||
@Autowired
|
||||
private CourseRepository courseRepository;
|
||||
|
||||
public long getCourseCount() {
|
||||
return courseRepository.count();
|
||||
}
|
||||
|
||||
public List<Course> getCoursesByRange(int start, int end) {
|
||||
if (start < 0 || end <= start) {
|
||||
throw new IllegalArgumentException("Invalid start or end range");
|
||||
Pageable pageable = PageRequest.of(start, end - start);
|
||||
return courseRepository.findAll(pageable).getContent();
|
||||
}
|
||||
|
||||
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 Course getCourseById(Long id) {
|
||||
return courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
|
||||
}
|
||||
|
||||
public void createCourse(CourseRequest request) {
|
||||
Course course = new Course();
|
||||
course.setTitle(request.getTitle());
|
||||
course.setDescription(request.getDescription());
|
||||
course.setOrder(request.getOrder());
|
||||
course.setAuthor(request.getAuthor());
|
||||
course.setVideoPath(request.getVideoPath());
|
||||
course.setImagePath(request.getImagePath());
|
||||
course.setDescription(request.getDescription());
|
||||
course.setOrderNo(request.getOrderNo());
|
||||
course.setVideoPath(request.getVideoPath()); // 确保保存视频路径
|
||||
course.setImagePath(request.getImagePath()); // 确保保存图片路径
|
||||
courseRepository.save(course);
|
||||
}
|
||||
|
||||
public void updateCourse(Long id, CourseRequest request) {
|
||||
Course course = courseRepository.findById(id).orElse(null);
|
||||
if (course != null) {
|
||||
Course course = courseRepository.findById(id).orElseThrow(() -> new EntityNotFoundException("Course not found"));
|
||||
course.setTitle(request.getTitle());
|
||||
course.setDescription(request.getDescription());
|
||||
course.setOrder(request.getOrder());
|
||||
course.setAuthor(request.getAuthor());
|
||||
course.setVideoPath(request.getVideoPath());
|
||||
course.setImagePath(request.getImagePath());
|
||||
course.setDescription(request.getDescription());
|
||||
course.setOrderNo(request.getOrderNo());
|
||||
course.setVideoPath(request.getVideoPath()); // 确保更新视频路径
|
||||
course.setImagePath(request.getImagePath()); // 确保更新图片路径
|
||||
courseRepository.save(course);
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteCourse(Long id) {
|
||||
if (!courseRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException();
|
||||
}
|
||||
courseRepository.deleteById(id);
|
||||
}
|
||||
|
||||
public Course getCourseById(Long id) {
|
||||
if (!courseRepository.existsById(id)) {
|
||||
throw new EntityNotFoundException();
|
||||
public List<Course> searchCourses(SearchCourseRequest request) {
|
||||
Pageable pageable = PageRequest.of(request.getStart(), request.getEnd() - request.getStart());
|
||||
return courseRepository.findAll(
|
||||
CourseSpecification.searchCourses(
|
||||
request.getTitle(),
|
||||
request.getAuthor(),
|
||||
request.getDescription(),
|
||||
request.getSortOrder()
|
||||
),
|
||||
pageable
|
||||
).getContent();
|
||||
}
|
||||
return courseRepository.findById(id).orElse(null);
|
||||
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();
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,44 @@
|
||||
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,5 +20,9 @@ spring.datasource.hikari.connection-timeout=30000
|
||||
server.servlet.encoding.enabled=true
|
||||
server.servlet.encoding.force=true
|
||||
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