FeatureController.java
4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package com.aigeo.ai.controller;
import com.aigeo.ai.entity.Feature;
import com.aigeo.ai.service.FeatureService;
import com.aigeo.common.Result;
import com.aigeo.common.ResultPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
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.web.bind.annotation.*;
import java.util.List;
/**
* AI功能模块控制器
*/
@Slf4j
@RestController
@RequestMapping("/api/features")
@Tag(name = "AI功能管理", description = "AI功能模块相关接口")
public class FeatureController {
@Autowired
private FeatureService featureService;
@PostMapping
@Operation(summary = "创建AI功能", description = "创建新的AI功能")
public Result<Feature> createFeature(@RequestBody Feature feature) {
try {
Feature savedFeature = featureService.save(feature);
return Result.success("功能创建成功", savedFeature);
} catch (Exception e) {
log.error("创建AI功能失败", e);
return Result.error("功能创建失败");
}
}
@GetMapping("/{id}")
@Operation(summary = "获取AI功能详情", description = "根据ID获取AI功能详情")
public Result<Feature> getFeatureById(@PathVariable Integer id) {
try {
return featureService.findById(id)
.map(feature -> Result.success("查询成功", feature))
.orElse(Result.error("功能不存在"));
} catch (Exception e) {
log.error("获取AI功能详情失败, id: {}", id, e);
return Result.error("查询失败");
}
}
@GetMapping
@Operation(summary = "获取AI功能列表", description = "获取所有AI功能列表")
public Result<List<Feature>> getAllFeatures() {
try {
List<Feature> features = featureService.findAll();
return Result.success("查询成功", features);
} catch (Exception e) {
log.error("获取AI功能列表失败", e);
return Result.error("查询失败");
}
}
@GetMapping("/active")
@Operation(summary = "获取启用的AI功能列表", description = "获取所有启用的AI功能列表")
public Result<List<Feature>> getActiveFeatures() {
try {
List<Feature> features = featureService.findActiveFeatures();
return Result.success("查询成功", features);
} catch (Exception e) {
log.error("获取启用的AI功能列表失败", e);
return Result.error("查询失败");
}
}
@GetMapping("/category/{category}")
@Operation(summary = "根据分类获取AI功能列表", description = "根据分类获取AI功能列表")
public Result<List<Feature>> getFeaturesByCategory(@PathVariable String category) {
try {
List<Feature> features = featureService.findByCategory(category);
return Result.success("查询成功", features);
} catch (Exception e) {
log.error("根据分类获取AI功能列表失败, category: {}", category, e);
return Result.error("查询失败");
}
}
@PutMapping("/{id}")
@Operation(summary = "更新AI功能", description = "更新AI功能信息")
public Result<Feature> updateFeature(@PathVariable Integer id, @RequestBody Feature feature) {
try {
if (!featureService.findById(id).isPresent()) {
return Result.error("功能不存在");
}
feature.setId(id);
Feature updatedFeature = featureService.save(feature);
return Result.success("功能更新成功", updatedFeature);
} catch (Exception e) {
log.error("更新AI功能失败, id: {}", id, e);
return Result.error("功能更新失败");
}
}
@DeleteMapping("/{id}")
@Operation(summary = "删除AI功能", description = "删除指定ID的AI功能")
public Result<String> deleteFeature(@PathVariable Integer id) {
try {
if (!featureService.findById(id).isPresent()) {
return Result.error("功能不存在");
}
featureService.deleteById(id);
return Result.success("功能删除成功");
} catch (Exception e) {
log.error("删除AI功能失败, id: {}", id, e);
return Result.error("功能删除失败");
}
}
}