更新了CourseController
This commit is contained in:
parent
dafdf82f5d
commit
f6d5112ad5
@ -1,67 +1,78 @@
|
|||||||
package org.cmh.backend.CourseManagement.controller;
|
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.model.Course;
|
||||||
import org.cmh.backend.CourseManagement.service.CourseService;
|
import org.cmh.backend.CourseManagement.service.CourseService;
|
||||||
|
import org.cmh.backend.Utils.JwtVerify;
|
||||||
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;
|
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("/course")
|
@RequestMapping("/courses")
|
||||||
public class CourseController {
|
public class CourseController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CourseService courseService;
|
private CourseService courseService;
|
||||||
|
|
||||||
@PostMapping("/create")
|
@GetMapping
|
||||||
public ResponseEntity<String> createCourse(
|
@JwtVerify
|
||||||
@RequestParam("name") String name,
|
public ResponseEntity<GetCourseListResponse> getCoursesByRange(@RequestParam Integer start, @RequestParam Integer end, @RequestParam String token) {
|
||||||
@RequestParam("description") String description,
|
if (start >= end) {
|
||||||
@RequestParam("author") String author,
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
@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);
|
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>("课程创建成功", HttpStatus.OK);
|
long courseCount = courseService.getCourseCount();
|
||||||
|
return new ResponseEntity<>(new GetCourseListResponse(courseCount, courseService.getCoursesByRange(start, end)), HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/edit/{id}")
|
@GetMapping("/{id}")
|
||||||
public ResponseEntity<String> updateCourse(
|
@JwtVerify
|
||||||
@PathVariable Long id,
|
public ResponseEntity<Course> getCoursePage(@PathVariable Long id, @RequestParam String token) {
|
||||||
@RequestParam("name") String name,
|
try {
|
||||||
@RequestParam("description") String description,
|
return new ResponseEntity<>(courseService.getCourseById(id), HttpStatus.OK);
|
||||||
@RequestParam("author") String author,
|
} catch (EntityNotFoundException e) {
|
||||||
@RequestParam("sortOrder") String sortOrder,
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
@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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping("/delete/{id}")
|
@PostMapping
|
||||||
public ResponseEntity<String> deleteCourse(@PathVariable Long id) {
|
@JwtVerify
|
||||||
boolean isDeleted = courseService.deleteCourse(id);
|
public ResponseEntity<MessageResponse> createCourse(@RequestBody CourseRequest request) {
|
||||||
if (!isDeleted) {
|
try {
|
||||||
return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
|
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")
|
@PutMapping("/{id}")
|
||||||
public ResponseEntity<List<Course>> getAllCourses() {
|
@JwtVerify
|
||||||
List<Course> courses = courseService.getAllCourses();
|
public ResponseEntity<MessageResponse> updateCourse(@PathVariable Long id, @RequestBody CourseRequest request) {
|
||||||
return new ResponseEntity<>(courses, HttpStatus.OK);
|
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