优化
This commit is contained in:
@@ -8,6 +8,7 @@ import com.emotion.dto.request.MessageCreateRequest;
|
||||
import com.emotion.dto.response.MessageResponse;
|
||||
import com.emotion.entity.Message;
|
||||
import com.emotion.service.MessageService;
|
||||
import com.emotion.util.CurrentUserUtil;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@@ -126,36 +127,76 @@ public class MessageController {
|
||||
/**
|
||||
* 根据用户ID分页查询消息
|
||||
*/
|
||||
@GetMapping("/user/{userId}/page")
|
||||
public Result<PageResult<MessageResponse>> getPageByUserId(@PathVariable String userId,
|
||||
@Valid PageRequest request) {
|
||||
IPage<Message> page = messageService.getByUserIdWithPage(userId, Math.toIntExact(request.getCurrent()), Math.toIntExact(request.getSize()));
|
||||
List<MessageResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
@GetMapping("/user/page")
|
||||
public Result<PageResult<MessageResponse>> getPageByUserId(@Valid PageRequest request) {
|
||||
|
||||
PageResult<MessageResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
return Result.success(pageResult);
|
||||
IPage<Message> page = messageService.getByUserIdWithPage(userId, Math.toIntExact(request.getCurrent()),
|
||||
Math.toIntExact(request.getSize()));
|
||||
List<MessageResponse> responses = page.getRecords().stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
PageResult<MessageResponse> pageResult = new PageResult<>();
|
||||
pageResult.setCurrent(page.getCurrent());
|
||||
pageResult.setSize(page.getSize());
|
||||
pageResult.setTotal(page.getTotal());
|
||||
pageResult.setPages(page.getPages());
|
||||
pageResult.setRecords(responses);
|
||||
|
||||
return Result.success(pageResult);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据用户ID和关键词搜索消息
|
||||
*/
|
||||
@GetMapping("/user/{userId}/search")
|
||||
public Result<List<MessageResponse>> searchByUserId(@PathVariable String userId,
|
||||
@GetMapping("/user/search")
|
||||
public Result<List<MessageResponse>> searchByUserId(
|
||||
@RequestParam String keyword,
|
||||
@RequestParam(defaultValue = "50") Integer limit) {
|
||||
List<Message> messages = messageService.searchByUserIdAndKeyword(userId, keyword, limit);
|
||||
List<MessageResponse> responses = messages.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
List<Message> messages = messageService.searchByUserIdAndKeyword(userId, keyword, limit);
|
||||
List<MessageResponse> responses = messages.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户最近的聊天记录
|
||||
*/
|
||||
@GetMapping("/user/recent")
|
||||
public Result<List<MessageResponse>> getRecentMessages(
|
||||
@RequestParam(defaultValue = "10") Integer limit) {
|
||||
|
||||
try {
|
||||
// 从上下文中获取当前用户ID
|
||||
String userId = CurrentUserUtil.requireCurrentUserId();
|
||||
|
||||
List<Message> messages = messageService.getRecentByUserId(userId, limit);
|
||||
List<MessageResponse> responses = messages.stream()
|
||||
.map(this::convertToResponse)
|
||||
.collect(Collectors.toList());
|
||||
return Result.success(responses);
|
||||
|
||||
} catch (IllegalStateException e) {
|
||||
return Result.error(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user