From ef100673450f8df4614eff7ac9ae840702d5075a Mon Sep 17 00:00:00 2001
From: "Chester.X" <2931709855@qq.com>
Date: Thu, 4 Jul 2024 21:21:46 +0800
Subject: [PATCH] 2
---
pom.xml | 5 -
.../controller/CourseController.java | 67 +++++++++++++
.../controller/FileController.java | 35 +++++++
.../CourseManagement/model/Course.java | 22 +++++
.../repository/CourseRepository.java | 9 ++
.../service/CourseService.java | 96 +++++++++++++++++++
.../controller/AuthenticationController.java | 45 ---------
.../controller/UserController.java | 51 ++++++----
.../authentication/dto/LoginRequest.java | 11 +++
.../authentication/dto/LoginResponse.java | 13 +++
.../authentication/dto/ProfileResponse.java | 19 ++++
.../authentication/dto/RegisterResponse.java | 12 +++
.../authentication/dto/UpdateRequest.java | 15 +++
.../repository/UserRepository.java | 2 -
.../authentication/service/UserService.java | 72 ++++++++++++--
.../service/UserServiceImpl.java | 58 -----------
src/test/application-test.properties | 7 --
.../cmh/backend/BackendApplicationTests.java | 12 ---
.../cmh/backend/TestBackendApplication.java | 12 ---
.../backend/TestcontainersConfiguration.java | 25 -----
.../backend/Utils/JwtVerifyAspectTest.java | 88 -----------------
21 files changed, 394 insertions(+), 282 deletions(-)
create mode 100644 src/main/java/org/cmh/backend/CourseManagement/controller/CourseController.java
create mode 100644 src/main/java/org/cmh/backend/CourseManagement/controller/FileController.java
create mode 100644 src/main/java/org/cmh/backend/CourseManagement/model/Course.java
create mode 100644 src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java
create mode 100644 src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java
delete mode 100644 src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java
create mode 100644 src/main/java/org/cmh/backend/authentication/dto/LoginRequest.java
create mode 100644 src/main/java/org/cmh/backend/authentication/dto/LoginResponse.java
create mode 100644 src/main/java/org/cmh/backend/authentication/dto/ProfileResponse.java
create mode 100644 src/main/java/org/cmh/backend/authentication/dto/RegisterResponse.java
create mode 100644 src/main/java/org/cmh/backend/authentication/dto/UpdateRequest.java
delete mode 100644 src/main/java/org/cmh/backend/authentication/service/UserServiceImpl.java
delete mode 100644 src/test/application-test.properties
delete mode 100644 src/test/java/org/cmh/backend/BackendApplicationTests.java
delete mode 100644 src/test/java/org/cmh/backend/TestBackendApplication.java
delete mode 100644 src/test/java/org/cmh/backend/TestcontainersConfiguration.java
delete mode 100644 src/test/java/org/cmh/backend/Utils/JwtVerifyAspectTest.java
diff --git a/pom.xml b/pom.xml
index 39cc9bb..9c4b479 100644
--- a/pom.xml
+++ b/pom.xml
@@ -93,11 +93,6 @@
runtime
true
-
-
-
-
-
com.mysql
mysql-connector-j
diff --git a/src/main/java/org/cmh/backend/CourseManagement/controller/CourseController.java b/src/main/java/org/cmh/backend/CourseManagement/controller/CourseController.java
new file mode 100644
index 0000000..3602384
--- /dev/null
+++ b/src/main/java/org/cmh/backend/CourseManagement/controller/CourseController.java
@@ -0,0 +1,67 @@
+package org.cmh.backend.CourseManagement.controller;
+
+import org.cmh.backend.CourseManagement.model.Course;
+import org.cmh.backend.CourseManagement.service.CourseService;
+import org.springframework.beans.factory.annotation.Autowired;
+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")
+public class CourseController {
+
+ @Autowired
+ private CourseService courseService;
+
+ @PostMapping("/create")
+ public ResponseEntity 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);
+ }
+ return new ResponseEntity<>("课程创建成功", HttpStatus.OK);
+ }
+
+ @PutMapping("/edit/{id}")
+ public ResponseEntity 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);
+ }
+ return new ResponseEntity<>("课程修改成功", HttpStatus.OK);
+ }
+
+ @DeleteMapping("/delete/{id}")
+ public ResponseEntity deleteCourse(@PathVariable Long id) {
+ boolean isDeleted = courseService.deleteCourse(id);
+ if (!isDeleted) {
+ return new ResponseEntity<>("课程未找到", HttpStatus.NOT_FOUND);
+ }
+ return new ResponseEntity<>("课程删除成功", HttpStatus.OK);
+ }
+
+ @GetMapping("/getAll")
+ public ResponseEntity> getAllCourses() {
+ List courses = courseService.getAllCourses();
+ return new ResponseEntity<>(courses, HttpStatus.OK);
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/cmh/backend/CourseManagement/controller/FileController.java b/src/main/java/org/cmh/backend/CourseManagement/controller/FileController.java
new file mode 100644
index 0000000..99d9350
--- /dev/null
+++ b/src/main/java/org/cmh/backend/CourseManagement/controller/FileController.java
@@ -0,0 +1,35 @@
+package org.cmh.backend.CourseManagement.controller;
+
+import org.springframework.core.io.UrlResource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.core.io.Resource;
+
+
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+public class FileController {
+ private static final String UPLOAD_DIR = "uploads/";
+
+ @GetMapping("/files/{filename}")
+ public ResponseEntity getFile(@PathVariable String filename) {
+ try {
+ Path filePath = Paths.get(UPLOAD_DIR).resolve(filename).normalize();
+ Resource resource = new UrlResource(filePath.toUri());
+
+ if (resource.exists() && resource.isReadable()) {
+ return ResponseEntity.ok()
+ .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
+ .body(resource);
+ } else {
+ return new ResponseEntity<>(HttpStatus.NOT_FOUND);
+ }
+ } catch (Exception e) {
+ return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+}
diff --git a/src/main/java/org/cmh/backend/CourseManagement/model/Course.java b/src/main/java/org/cmh/backend/CourseManagement/model/Course.java
new file mode 100644
index 0000000..caa9186
--- /dev/null
+++ b/src/main/java/org/cmh/backend/CourseManagement/model/Course.java
@@ -0,0 +1,22 @@
+package org.cmh.backend.CourseManagement.model;
+
+import jakarta.persistence.*;
+import lombok.Data;
+
+@Data
+@Entity
+@Table(name = "courses")
+public class Course {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+ private String name;
+ private String description;
+ private String author;
+ private String sortOrder;
+ private String coverImagePath;
+ private String videoPath;
+
+ // getters and setters
+}
\ No newline at end of file
diff --git a/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java b/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java
new file mode 100644
index 0000000..ce25b61
--- /dev/null
+++ b/src/main/java/org/cmh/backend/CourseManagement/repository/CourseRepository.java
@@ -0,0 +1,9 @@
+package org.cmh.backend.CourseManagement.repository;
+
+import org.cmh.backend.CourseManagement.model.Course;
+import org.springframework.data.jpa.repository.JpaRepository;
+import java.util.Optional;
+
+public interface CourseRepository extends JpaRepository {
+ Optional findByName(String name);
+}
\ No newline at end of file
diff --git a/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java b/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java
new file mode 100644
index 0000000..f49676f
--- /dev/null
+++ b/src/main/java/org/cmh/backend/CourseManagement/service/CourseService.java
@@ -0,0 +1,96 @@
+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 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 getAllCourses() {
+ return courseRepository.findAll();
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java b/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java
deleted file mode 100644
index 974eed0..0000000
--- a/src/main/java/org/cmh/backend/authentication/controller/AuthenticationController.java
+++ /dev/null
@@ -1,45 +0,0 @@
-package org.cmh.backend.authentication.controller;
-
-import org.cmh.backend.authentication.model.User;
-import org.cmh.backend.authentication.service.UserService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.*;
-
-import java.util.HashMap;
-import java.util.Map;
-
-@RestController
-@RequestMapping("/api/auth")
-public class AuthenticationController {
-
- @Autowired
- private UserService userService;
-
- @PostMapping("/register")
- public User register(@RequestBody User user) {
- return userService.register(user);
- }
-
- @PostMapping("/login")
- public ResponseEntity