This commit is contained in:
2025-09-09 11:13:36 +08:00
parent cf50a9f1fa
commit fcd35c78e5
60 changed files with 2753 additions and 2196 deletions
@@ -10,16 +10,16 @@ import com.emotion.dto.response.MessageResponse;
import com.emotion.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
/**
* 消息控制器
*
* @author emotion-museum
* @date 2025-07-23
* @date 2025-09-08
*/
@RestController
@RequestMapping("/message")
@@ -32,104 +32,72 @@ public class MessageController {
/**
* 创建消息
*/
@PostMapping
@PostMapping(value = "/create")
public Result<MessageResponse> create(@Valid @RequestBody MessageCreateRequest request) {
log.info("创建消息: conversationId={}", request.getConversationId());
try {
MessageResponse response = messageService.createMessageFromRequest(request);
log.info("创建消息成功: messageId={}", response.getId());
return Result.success(response);
} catch (IllegalStateException e) {
log.error("用户未认证: {}", e.getMessage());
return Result.error(401, "用户未登录或认证失败");
} catch (Exception e) {
log.error("创建消息失败", e);
return Result.error(500, "创建消息失败,请稍后重试");
}
MessageResponse response = messageService.createMessageFromRequest(request);
return Result.success(response);
}
/**
* 根据ID获取消息
*/
@GetMapping("/{id}")
public Result<MessageResponse> getById(@PathVariable String id) {
log.info("获取消息详情: id={}", id);
try {
MessageResponse response = messageService.getMessageById(id);
if (response == null) {
return Result.error(404, "消息不存在");
}
return Result.success(response);
} catch (Exception e) {
log.error("获取消息详情失败", e);
return Result.error(500, "获取消息详情失败,请稍后重试");
@GetMapping(value = "/detail")
public Result<MessageResponse> getById(@RequestParam String id) {
MessageResponse response = messageService.getMessageById(id);
if (response == null) {
return Result.notFound("消息不存在");
}
return Result.success(response);
}
/**
* 根据用户ID分页查询消息
* 分页查询消息
*/
@GetMapping("/user/page")
public Result<PageResult<MessageResponse>> getPageByUserId(
@RequestParam(defaultValue = "1") Long current,
@RequestParam(defaultValue = "20") Long size) {
log.info("获取用户消息分页: current={}, size={}", current, size);
// 构建请求对象
MessagePageRequest request = new MessagePageRequest();
request.setCurrent(current);
request.setSize(size);
PageResult<MessageResponse> pageResult = messageService.getUserMessagesWithPage(request);
log.info("获取用户消息分页成功: total={}", pageResult.getTotal());
@GetMapping(value = "/page")
public Result<PageResult<MessageResponse>> getPage(@Validated MessagePageRequest request) {
PageResult<MessageResponse> pageResult = messageService.getPageWithResponse(request);
return Result.success(pageResult);
}
/**
* 根据用户ID和关键词搜索消息
* 搜索消息
*/
@PostMapping("/user/search")
public Result<List<MessageResponse>> searchByUserId(@Valid @RequestBody MessageSearchRequest request) {
log.info("搜索用户消息: keyword={}, limit={}", request.getKeyword(), request.getLimit());
try {
List<MessageResponse> responses = messageService.searchUserMessages(request);
log.info("搜索用户消息成功: {} 条消息", responses.size());
return Result.success(responses);
} catch (IllegalStateException e) {
log.error("用户未认证: {}", e.getMessage());
return Result.error(401, "用户未登录或认证失败");
} catch (Exception e) {
log.error("搜索用户消息失败", e);
return Result.error(500, "搜索失败,请稍后重试");
}
@PostMapping(value = "/search")
public Result<PageResult<MessageResponse>> search(@Valid @RequestBody MessageSearchRequest request) {
PageResult<MessageResponse> pageResult = messageService.searchWithResponse(request);
return Result.success(pageResult);
}
/**
* 获取用户最近的聊天记录
* 获取最近的消息
*/
@PostMapping("/user/recent")
public Result<List<MessageResponse>> getRecentMessages(@Valid @RequestBody MessageRecentRequest request) {
log.info("获取用户最近消息: limit={}", request.getLimit());
try {
List<MessageResponse> responses = messageService.getUserRecentMessages(request);
log.info("获取用户最近消息成功: {} 条消息", responses.size());
return Result.success(responses);
} catch (IllegalStateException e) {
log.error("用户未认证: {}", e.getMessage());
return Result.error(401, "用户未登录或认证失败");
} catch (Exception e) {
log.error("获取最近消息失败", e);
return Result.error(500, "获取最近消息失败,请稍后重试");
}
@PostMapping(value = "/recent")
public Result<PageResult<MessageResponse>> getRecentMessages(@Valid @RequestBody MessageRecentRequest request) {
PageResult<MessageResponse> pageResult = messageService.getRecentWithResponse(request);
return Result.success(pageResult);
}
/**
* 更新消息
*/
@PutMapping(value = "/update")
public Result<MessageResponse> update(@RequestParam String id, @RequestParam String content) {
MessageResponse response = messageService.updateMessage(id, content);
if (response == null) {
return Result.notFound("消息不存在");
}
return Result.success(response);
}
/**
* 删除消息
*/
@DeleteMapping(value = "/delete")
public Result<Void> delete(@RequestParam String id) {
boolean deleted = messageService.deleteMessage(id);
if (!deleted) {
return Result.error("删除失败");
}
return Result.success();
}
}