更新了CourseController
This commit is contained in:
parent
dafdf82f5d
commit
f6d5112ad5
@ -1,67 +1,78 @@
|
||||
package org.cmh.backend.CourseManagement.controller;
|
||||
|
||||
import jakarta.persistence.EntityNotFoundException;
|
||||
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.model.Course;
|
||||
import org.cmh.backend.CourseManagement.service.CourseService;
|
||||
import org.cmh.backend.Utils.JwtVerify;
|
||||
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;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/course")
|
||||
@RequestMapping("/courses")
|
||||
public class CourseController {
|
||||
|
||||
@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
|
||||
@JwtVerify
|
||||
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}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<Course> getCoursePage(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
return new ResponseEntity<>(courseService.getCourseById(id), HttpStatus.OK);
|
||||
} catch (EntityNotFoundException 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
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> createCourse(@RequestBody CourseRequest request) {
|
||||
try {
|
||||
courseService.createCourse(request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("创建失败,课程已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception 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}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> updateCourse(@PathVariable Long id, @RequestBody CourseRequest request) {
|
||||
try {
|
||||
courseService.updateCourse(id, request);
|
||||
} catch (DataIntegrityViolationException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("修改失败,新标题已存在或缺少字段"), HttpStatus.BAD_REQUEST);
|
||||
} catch (Exception e) {
|
||||
return new ResponseEntity<>(new MessageResponse("修改失败:" + e.getMessage()), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("修改成功"), HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
@JwtVerify
|
||||
public ResponseEntity<MessageResponse> deleteCourse(@PathVariable Long id, @RequestParam String token) {
|
||||
try {
|
||||
courseService.deleteCourse(id);
|
||||
} catch (EntityNotFoundException e) {
|
||||
return new ResponseEntity<>(new MessageResponse("删除失败,课程不存在"), HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
return new ResponseEntity<>(new MessageResponse("删除成功"), HttpStatus.OK);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user