feat: 增强情绪博物馆项目功能 - 新增用户评论和帖子功能,优化前端架构和WebSocket通信 - 更新文档和部署配置
This commit is contained in:
@@ -0,0 +1,190 @@
|
||||
package com.emotionmuseum.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.IPage;
|
||||
import com.emotionmuseum.dto.Result;
|
||||
import com.emotionmuseum.dto.diary.DiaryPostRequest;
|
||||
import com.emotionmuseum.entity.DiaryPost;
|
||||
import com.emotionmuseum.service.AuthService;
|
||||
import com.emotionmuseum.service.DiaryPostService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 日记控制器
|
||||
*
|
||||
* @author emotion-museum
|
||||
* @version 1.0.0
|
||||
* @since 2024-01-01
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/diary")
|
||||
@Tag(name = "日记管理", description = "日记相关接口")
|
||||
@Slf4j
|
||||
public class DiaryPostController {
|
||||
|
||||
@Autowired
|
||||
private DiaryPostService diaryPostService;
|
||||
|
||||
@Autowired
|
||||
private AuthService authService;
|
||||
|
||||
/**
|
||||
* 创建日记
|
||||
*/
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建日记", description = "创建新的日记")
|
||||
public Result<DiaryPost> createDiary(HttpServletRequest request, @Valid @RequestBody DiaryPostRequest diaryRequest) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
log.info("用户创建日记: {}", userId);
|
||||
return diaryPostService.createDiary(userId, diaryRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新日记
|
||||
*/
|
||||
@PutMapping("/{diaryId}")
|
||||
@Operation(summary = "更新日记", description = "更新指定的日记")
|
||||
public Result<String> updateDiary(HttpServletRequest request,
|
||||
@PathVariable String diaryId,
|
||||
@Valid @RequestBody DiaryPostRequest diaryRequest) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
log.info("用户更新日记: {}, 日记: {}", userId, diaryId);
|
||||
return diaryPostService.updateDiary(userId, diaryId, diaryRequest);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取日记详情
|
||||
*/
|
||||
@GetMapping("/{diaryId}")
|
||||
@Operation(summary = "获取日记详情", description = "获取指定日记的详细信息")
|
||||
public Result<DiaryPost> getDiaryById(@PathVariable String diaryId) {
|
||||
return diaryPostService.getDiaryById(diaryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户日记列表
|
||||
*/
|
||||
@GetMapping("/user/list")
|
||||
@Operation(summary = "获取用户日记列表", description = "获取当前用户的日记列表")
|
||||
public Result<IPage<DiaryPost>> getUserDiaries(HttpServletRequest request,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "10") int size) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
return diaryPostService.getUserDiaries(userId, page, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取公开日记列表
|
||||
*/
|
||||
@GetMapping("/public/list")
|
||||
@Operation(summary = "获取公开日记列表", description = "获取所有公开的日记列表")
|
||||
public Result<IPage<DiaryPost>> getPublicDiaries(@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "10") int size) {
|
||||
return diaryPostService.getPublicDiaries(page, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据情绪标签查询日记
|
||||
*/
|
||||
@GetMapping("/emotion/{emotionTag}")
|
||||
@Operation(summary = "根据情绪标签查询日记", description = "根据情绪标签查询公开日记")
|
||||
public Result<IPage<DiaryPost>> getDiariesByEmotionTag(@PathVariable String emotionTag,
|
||||
@RequestParam(defaultValue = "1") int page,
|
||||
@RequestParam(defaultValue = "10") int size) {
|
||||
return diaryPostService.getDiariesByEmotionTag(emotionTag, page, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除日记
|
||||
*/
|
||||
@DeleteMapping("/{diaryId}")
|
||||
@Operation(summary = "删除日记", description = "删除指定的日记")
|
||||
public Result<String> deleteDiary(HttpServletRequest request, @PathVariable String diaryId) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
log.info("用户删除日记: {}, 日记: {}", userId, diaryId);
|
||||
return diaryPostService.deleteDiary(userId, diaryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 点赞日记
|
||||
*/
|
||||
@PostMapping("/{diaryId}/like")
|
||||
@Operation(summary = "点赞日记", description = "对指定日记进行点赞")
|
||||
public Result<String> likeDiary(HttpServletRequest request, @PathVariable String diaryId) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
log.info("用户点赞日记: {}, 日记: {}", userId, diaryId);
|
||||
return diaryPostService.likeDiary(userId, diaryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消点赞
|
||||
*/
|
||||
@PostMapping("/{diaryId}/unlike")
|
||||
@Operation(summary = "取消点赞", description = "取消对指定日记的点赞")
|
||||
public Result<String> unlikeDiary(HttpServletRequest request, @PathVariable String diaryId) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
log.info("用户取消点赞: {}, 日记: {}", userId, diaryId);
|
||||
return diaryPostService.unlikeDiary(userId, diaryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取AI点评
|
||||
*/
|
||||
@GetMapping("/{diaryId}/ai-comment")
|
||||
@Operation(summary = "获取AI点评", description = "获取指定日记的AI点评")
|
||||
public Result<String> getAiComment(HttpServletRequest request, @PathVariable String diaryId) {
|
||||
String token = extractToken(request);
|
||||
String userId = authService.getUserIdFromToken(token);
|
||||
if (userId == null) {
|
||||
return Result.unauthorized();
|
||||
}
|
||||
|
||||
return diaryPostService.getAiComment(userId, diaryId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从请求中提取令牌
|
||||
*/
|
||||
private String extractToken(HttpServletRequest request) {
|
||||
String bearerToken = request.getHeader("Authorization");
|
||||
if (bearerToken != null && bearerToken.startsWith("Bearer ")) {
|
||||
return bearerToken.substring(7);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user