yl-backend/src/main/java/com/guwan/backend/controller/ReadingNoteController.java

127 lines
4.6 KiB
Java
Raw Normal View History

package com.guwan.backend.controller;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.guwan.backend.common.Result;
import com.guwan.backend.entity.ReadingNote;
import com.guwan.backend.service.ReadingNoteService;
import com.guwan.backend.util.SecurityUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@Slf4j
@Tag(name = "阅读笔记", description = "阅读笔记相关接口")
@RestController
@RequestMapping("/api/reading-notes")
@RequiredArgsConstructor
public class ReadingNoteController {
private final ReadingNoteService noteService;
private final SecurityUtil securityUtil;
@Operation(summary = "添加笔记")
@PostMapping
public Result<ReadingNote> addNote(@RequestBody ReadingNote note) {
try {
note.setUserId(securityUtil.getCurrentUserId());
return Result.success(noteService.addNote(note));
} catch (Exception e) {
log.error("添加笔记失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "更新笔记")
@PutMapping("/{id}")
public Result<ReadingNote> updateNote(@PathVariable Long id, @RequestBody ReadingNote note) {
try {
note.setId(id);
note.setUserId(securityUtil.getCurrentUserId());
return Result.success(noteService.updateNote(note));
} catch (Exception e) {
log.error("更新笔记失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "删除笔记")
@DeleteMapping("/{id}")
public Result<Void> deleteNote(@PathVariable Long id) {
try {
noteService.deleteNote(id);
return Result.success();
} catch (Exception e) {
log.error("删除笔记失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "获取笔记详情")
@GetMapping("/{id}")
public Result<ReadingNote> getNote(@PathVariable Long id) {
try {
return Result.success(noteService.getNoteById(id));
} catch (Exception e) {
log.error("获取笔记详情失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "获取用户的所有笔记")
@GetMapping
public Result<IPage<ReadingNote>> getUserNotes(
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
try {
Long userId = securityUtil.getCurrentUserId();
return Result.success(noteService.getUserNotes(userId, pageNum, pageSize));
} catch (Exception e) {
log.error("获取用户笔记失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "获取书籍的所有公开笔记")
@GetMapping("/book/{bookId}")
public Result<IPage<ReadingNote>> getBookNotes(
@PathVariable Long bookId,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
try {
return Result.success(noteService.getBookNotes(bookId, pageNum, pageSize));
} catch (Exception e) {
log.error("获取书籍笔记失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "获取用户在特定书籍上的笔记")
@GetMapping("/book/{bookId}/my")
public Result<IPage<ReadingNote>> getUserBookNotes(
@PathVariable Long bookId,
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
try {
Long userId = securityUtil.getCurrentUserId();
return Result.success(noteService.getUserBookNotes(userId, bookId, pageNum, pageSize));
} catch (Exception e) {
log.error("获取用户书籍笔记失败", e);
return Result.error(e.getMessage());
}
}
@Operation(summary = "获取所有公开笔记")
@GetMapping("/public")
public Result<IPage<ReadingNote>> getPublicNotes(
@RequestParam(defaultValue = "1") Integer pageNum,
@RequestParam(defaultValue = "10") Integer pageSize) {
try {
return Result.success(noteService.getPublicNotes(pageNum, pageSize));
} catch (Exception e) {
log.error("获取公开笔记失败", e);
return Result.error(e.getMessage());
}
}
}