feat: 新增request和response包结构,优化Controller层代码规范
- 创建统一的BaseRequest和BaseResponse基础类 - 新增全局异常处理机制 - 重构所有Controller层,移除业务逻辑到Service层 - 统一接口入参和出参格式 - 移除try-catch,使用全局异常处理 - 完善接口文档和参数校验 主要变更: 1. 新增request和response包结构 2. 创建全局异常处理器GlobalExceptionHandler 3. 重构AiChatController、AuthController、UserController等 4. 优化代码规范,提升维护性
This commit is contained in:
@@ -36,7 +36,7 @@ import java.util.Map;
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/api/ai")
|
||||
@RequestMapping("/ai")
|
||||
public class AiChatController {
|
||||
|
||||
@Autowired
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.entity.GrowthTopic;
|
||||
import com.emotion.service.GrowthTopicService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/growth-topic")
|
||||
public class GrowthTopicController {
|
||||
|
||||
@Autowired
|
||||
private GrowthTopicService growthTopicService;
|
||||
|
||||
/**
|
||||
* 分页查询成长话题
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public Result<IPage<GrowthTopic>> getPage(@Validated PageRequest request) {
|
||||
IPage<GrowthTopic> page = growthTopicService.getPage(request);
|
||||
return Result.success(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建成长话题
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
public Result<GrowthTopic> createGrowthTopic(@RequestBody @Validated GrowthTopicCreateRequest request) {
|
||||
GrowthTopic topic = growthTopicService.createGrowthTopic(
|
||||
request.getTitle(),
|
||||
request.getDescription(),
|
||||
request.getCategory(),
|
||||
request.getDifficultyLevel(),
|
||||
request.getTags(),
|
||||
request.getEndTime()
|
||||
);
|
||||
return Result.success(topic);
|
||||
}
|
||||
|
||||
// 可根据GrowthTopicService接口继续补充其他接口...
|
||||
|
||||
public static class GrowthTopicCreateRequest {
|
||||
private String title;
|
||||
private String description;
|
||||
private String category;
|
||||
private String difficultyLevel;
|
||||
private String tags;
|
||||
private LocalDateTime endTime;
|
||||
// getter/setter
|
||||
public String getTitle() { return title; }
|
||||
public void setTitle(String title) { this.title = title; }
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
public String getCategory() { return category; }
|
||||
public void setCategory(String category) { this.category = category; }
|
||||
public String getDifficultyLevel() { return difficultyLevel; }
|
||||
public void setDifficultyLevel(String difficultyLevel) { this.difficultyLevel = difficultyLevel; }
|
||||
public String getTags() { return tags; }
|
||||
public void setTags(String tags) { this.tags = tags; }
|
||||
public LocalDateTime getEndTime() { return endTime; }
|
||||
public void setEndTime(LocalDateTime endTime) { this.endTime = endTime; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.entity.GuestUser;
|
||||
import com.emotion.service.GuestUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/guest-user")
|
||||
public class GuestUserController {
|
||||
|
||||
@Autowired
|
||||
private GuestUserService guestUserService;
|
||||
|
||||
/**
|
||||
* 分页查询访客用户
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public Result<IPage<GuestUser>> getPage(@Validated PageRequest request) {
|
||||
IPage<GuestUser> page = guestUserService.getPage(request);
|
||||
return Result.success(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建访客用户
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
public Result<GuestUser> createGuestUser(@RequestBody @Validated GuestUserCreateRequest request) {
|
||||
GuestUser guestUser = guestUserService.createGuestUser(
|
||||
request.getDeviceId(),
|
||||
request.getIpAddress(),
|
||||
request.getUserAgent(),
|
||||
request.getLocation());
|
||||
return Result.success(guestUser);
|
||||
}
|
||||
|
||||
// 可根据GuestUserService接口继续补充其他接口...
|
||||
|
||||
public static class GuestUserCreateRequest {
|
||||
private String deviceId;
|
||||
private String ipAddress;
|
||||
private String userAgent;
|
||||
private String location;
|
||||
|
||||
// getter/setter
|
||||
public String getDeviceId() {
|
||||
return deviceId;
|
||||
}
|
||||
|
||||
public void setDeviceId(String deviceId) {
|
||||
this.deviceId = deviceId;
|
||||
}
|
||||
|
||||
public String getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
public void setIpAddress(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
public String getUserAgent() {
|
||||
return userAgent;
|
||||
}
|
||||
|
||||
public void setUserAgent(String userAgent) {
|
||||
this.userAgent = userAgent;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.dto.request.IdRequest;
|
||||
import com.emotion.dto.response.BaseResponse;
|
||||
import com.emotion.entity.Reward;
|
||||
import com.emotion.service.RewardService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/reward")
|
||||
public class RewardController {
|
||||
|
||||
@Autowired
|
||||
private RewardService rewardService;
|
||||
|
||||
/**
|
||||
* 分页查询奖励
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public Result<IPage<Reward>> getPage(@Validated PageRequest request) {
|
||||
IPage<Reward> page = rewardService.getPage(request);
|
||||
return Result.success(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID分页查询奖励
|
||||
*/
|
||||
@GetMapping("/page/user/{userId}")
|
||||
public Result<IPage<Reward>> getPageByUserId(@Validated PageRequest request, @PathVariable String userId) {
|
||||
IPage<Reward> page = rewardService.getPageByUserId(request, userId);
|
||||
return Result.success(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID查询奖励
|
||||
*/
|
||||
@GetMapping("/user/{userId}")
|
||||
public Result<List<Reward>> getByUserId(@PathVariable String userId) {
|
||||
List<Reward> list = rewardService.getByUserId(userId);
|
||||
return Result.success(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建奖励
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
public Result<Reward> createReward(@RequestBody @Validated RewardCreateRequest request) {
|
||||
Reward reward = rewardService.createReward(
|
||||
request.getUserId(),
|
||||
request.getRewardType(),
|
||||
request.getPoints(),
|
||||
request.getSource(),
|
||||
request.getDescription(),
|
||||
request.getExpiredTime()
|
||||
);
|
||||
return Result.success(reward);
|
||||
}
|
||||
|
||||
// 可根据RewardService接口继续补充其他接口...
|
||||
|
||||
public static class RewardCreateRequest {
|
||||
private String userId;
|
||||
private String rewardType;
|
||||
private Integer points;
|
||||
private String source;
|
||||
private String description;
|
||||
private LocalDateTime expiredTime;
|
||||
// getter/setter
|
||||
public String getUserId() { return userId; }
|
||||
public void setUserId(String userId) { this.userId = userId; }
|
||||
public String getRewardType() { return rewardType; }
|
||||
public void setRewardType(String rewardType) { this.rewardType = rewardType; }
|
||||
public Integer getPoints() { return points; }
|
||||
public void setPoints(Integer points) { this.points = points; }
|
||||
public String getSource() { return source; }
|
||||
public void setSource(String source) { this.source = source; }
|
||||
public String getDescription() { return description; }
|
||||
public void setDescription(String description) { this.description = description; }
|
||||
public LocalDateTime getExpiredTime() { return expiredTime; }
|
||||
public void setExpiredTime(LocalDateTime expiredTime) { this.expiredTime = expiredTime; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.TokenRequest;
|
||||
import com.emotion.dto.response.UserInfoResponse;
|
||||
import com.emotion.service.TokenService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/token")
|
||||
public class TokenController {
|
||||
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
|
||||
/**
|
||||
* 通过token获取用户信息
|
||||
*/
|
||||
@PostMapping("/user-info")
|
||||
public Result<UserInfoResponse> getUserInfoByToken(@RequestBody @Validated TokenRequest request) {
|
||||
UserInfoResponse userInfo = tokenService.getUserInfoByToken(request.getToken());
|
||||
return Result.success(userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过token获取用户名
|
||||
*/
|
||||
@PostMapping("/username")
|
||||
public Result<String> getUsernameByToken(@RequestBody @Validated TokenRequest request) {
|
||||
String username = tokenService.getUsernameByToken(request.getToken());
|
||||
return Result.success(username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证token并返回用户ID
|
||||
*/
|
||||
@PostMapping("/validate")
|
||||
public Result<String> validateTokenAndGetUserId(@RequestBody @Validated TokenRequest request) {
|
||||
String userId = tokenService.validateTokenAndGetUserId(request.getToken());
|
||||
return Result.success(userId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package com.emotion.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotion.common.Result;
|
||||
import com.emotion.dto.request.PageRequest;
|
||||
import com.emotion.entity.TopicInteraction;
|
||||
import com.emotion.service.TopicInteractionService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/topic-interaction")
|
||||
public class TopicInteractionController {
|
||||
|
||||
@Autowired
|
||||
private TopicInteractionService topicInteractionService;
|
||||
|
||||
/**
|
||||
* 分页查询话题互动
|
||||
*/
|
||||
@GetMapping("/page")
|
||||
public Result<IPage<TopicInteraction>> getPage(@Validated PageRequest request) {
|
||||
IPage<TopicInteraction> page = topicInteractionService.getPage(request);
|
||||
return Result.success(page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建话题互动
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
public Result<TopicInteraction> createTopicInteraction(@RequestBody @Validated TopicInteractionCreateRequest request) {
|
||||
TopicInteraction interaction = topicInteractionService.createTopicInteraction(
|
||||
request.getTopicId(),
|
||||
request.getUserId(),
|
||||
request.getInteractionType(),
|
||||
request.getContent(),
|
||||
request.getAttachments()
|
||||
);
|
||||
return Result.success(interaction);
|
||||
}
|
||||
|
||||
// 可根据TopicInteractionService接口继续补充其他接口...
|
||||
|
||||
public static class TopicInteractionCreateRequest {
|
||||
private String topicId;
|
||||
private String userId;
|
||||
private String interactionType;
|
||||
private String content;
|
||||
private String attachments;
|
||||
// getter/setter
|
||||
public String getTopicId() { return topicId; }
|
||||
public void setTopicId(String topicId) { this.topicId = topicId; }
|
||||
public String getUserId() { return userId; }
|
||||
public void setUserId(String userId) { this.userId = userId; }
|
||||
public String getInteractionType() { return interactionType; }
|
||||
public void setInteractionType(String interactionType) { this.interactionType = interactionType; }
|
||||
public String getContent() { return content; }
|
||||
public void setContent(String content) { this.content = content; }
|
||||
public String getAttachments() { return attachments; }
|
||||
public void setAttachments(String attachments) { this.attachments = attachments; }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user