You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
187 lines
6.6 KiB
187 lines
6.6 KiB
package com.gym.controller;
|
|
|
|
import com.gym.model.Course;
|
|
import com.gym.service.CourseService;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.web.bind.annotation.*;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@RestController
|
|
@RequestMapping("/api/courses")
|
|
public class CourseController {
|
|
|
|
@Autowired
|
|
private CourseService courseService;
|
|
|
|
// 创建课程
|
|
@PostMapping
|
|
public Map<String, Object> createCourse(@RequestBody Course course, @RequestParam Long coachId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Course createdCourse = courseService.createCourse(course, coachId);
|
|
response.put("code", 200);
|
|
response.put("message", "课程创建成功");
|
|
response.put("data", createdCourse);
|
|
} catch (Exception e) {
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 获取所有课程
|
|
@GetMapping
|
|
public Map<String, Object> getAllCourses() {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<Course> courses = courseService.getAllCourses();
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", courses);
|
|
response.put("count", courses.size());
|
|
return response;
|
|
}
|
|
|
|
// 获取可用课程
|
|
@GetMapping("/available")
|
|
public Map<String, Object> getAvailableCourses() {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<Course> courses = courseService.getAvailableCourses();
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", courses);
|
|
return response;
|
|
}
|
|
|
|
// 获取单个课程
|
|
@GetMapping("/{id}")
|
|
public Map<String, Object> getCourseById(@PathVariable Long id) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
return courseService.getCourseById(id)
|
|
.map(course -> {
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", course);
|
|
return response;
|
|
})
|
|
.orElseGet(() -> {
|
|
response.put("code", 404);
|
|
response.put("message", "课程不存在");
|
|
return response;
|
|
});
|
|
}
|
|
|
|
// 更新课程信息
|
|
@PutMapping("/{id}")
|
|
public Map<String, Object> updateCourse(@PathVariable Long id, @RequestBody Course course) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Course updatedCourse = courseService.updateCourse(id, course);
|
|
response.put("code", 200);
|
|
response.put("message", "更新成功");
|
|
response.put("data", updatedCourse);
|
|
} catch (Exception e) {
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 删除课程
|
|
@DeleteMapping("/{id}")
|
|
public Map<String, Object> deleteCourse(@PathVariable Long id) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
courseService.deleteCourse(id);
|
|
response.put("code", 200);
|
|
response.put("message", "删除成功");
|
|
} catch (Exception e) {
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 预约课程
|
|
@PostMapping("/{id}/enroll")
|
|
public Map<String, Object> enrollCourse(@PathVariable Long id) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Course course = courseService.enrollCourse(id);
|
|
response.put("code", 200);
|
|
response.put("message", "预约成功");
|
|
response.put("data", course);
|
|
} catch (Exception e) {
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 取消预约
|
|
@PostMapping("/{id}/cancel")
|
|
public Map<String, Object> cancelEnrollment(@PathVariable Long id) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
Course course = courseService.cancelEnrollment(id);
|
|
response.put("code", 200);
|
|
response.put("message", "取消预约成功");
|
|
response.put("data", course);
|
|
} catch (Exception e) {
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 设置课程状态
|
|
@PutMapping("/{id}/status")
|
|
public Map<String, Object> setCourseStatus(@PathVariable Long id, @RequestBody Map<String, String> data) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
try {
|
|
String status = data.get("status");
|
|
Course course = courseService.setCourseStatus(id, status);
|
|
response.put("code", 200);
|
|
response.put("message", "状态更新成功");
|
|
response.put("data", course);
|
|
} catch (Exception e) {
|
|
response.put("code", 400);
|
|
response.put("message", e.getMessage());
|
|
}
|
|
return response;
|
|
}
|
|
|
|
// 搜索课程
|
|
@GetMapping("/search")
|
|
public Map<String, Object> searchCourses(@RequestParam String keyword) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<Course> courses = courseService.searchCourses(keyword);
|
|
response.put("code", 200);
|
|
response.put("message", "搜索成功");
|
|
response.put("data", courses);
|
|
return response;
|
|
}
|
|
|
|
// 获取教练的课程
|
|
@GetMapping("/coach/{coachId}")
|
|
public Map<String, Object> getCoachCourses(@PathVariable Long coachId) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<Course> courses = courseService.getCoachCourses(coachId);
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", courses);
|
|
return response;
|
|
}
|
|
|
|
// 按类型获取课程
|
|
@GetMapping("/type/{type}")
|
|
public Map<String, Object> getCoursesByType(@PathVariable String type) {
|
|
Map<String, Object> response = new HashMap<>();
|
|
List<Course> courses = courseService.getCoursesByType(type);
|
|
response.put("code", 200);
|
|
response.put("message", "获取成功");
|
|
response.put("data", courses);
|
|
return response;
|
|
}
|
|
} |