Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.yqz.openblog.forum.controller;

import com.yqz.openblog.common.ApiResponse;
import com.yqz.openblog.common.PageResult;
import com.yqz.openblog.forum.dto.ForumTopicResponse;
import com.yqz.openblog.forum.service.ForumService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/admin/forum")
@CrossOrigin(origins = "*")
public class ForumAdminController {

private final ForumService forumService;

public ForumAdminController(ForumService forumService) {
this.forumService = forumService;
}

@GetMapping("/topics")
@PreAuthorize("hasAnyRole('ADMIN','AUTHOR')")
public ApiResponse<PageResult<ForumTopicResponse>> list(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(required = false) String status) {
return ApiResponse.ok(forumService.listAllTopics(page, size, status));
}

@PutMapping("/topics/{topicId}/hide")
@PreAuthorize("hasAnyRole('ADMIN','AUTHOR')")
public ApiResponse<Void> hide(@PathVariable Long topicId) {
forumService.hideTopic(topicId);
return ApiResponse.ok();
}

@PutMapping("/topics/{topicId}/publish")
@PreAuthorize("hasAnyRole('ADMIN','AUTHOR')")
public ApiResponse<Void> publish(@PathVariable Long topicId) {
forumService.publishTopic(topicId);
return ApiResponse.ok();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.yqz.openblog.forum.controller;

import com.yqz.openblog.common.ApiResponse;
import com.yqz.openblog.common.BizException;
import com.yqz.openblog.common.PageResult;
import com.yqz.openblog.forum.dto.ForumCommentRequest;
import com.yqz.openblog.forum.dto.ForumCommentResponse;
import com.yqz.openblog.forum.dto.ForumTopicCreateRequest;
import com.yqz.openblog.forum.dto.ForumTopicResponse;
import com.yqz.openblog.forum.service.ForumService;
import com.yqz.openblog.security.CurrentUser;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/forum")
@CrossOrigin(origins = "*")
public class ForumController {

private final ForumService forumService;
private final CurrentUser currentUser;

public ForumController(ForumService forumService, CurrentUser currentUser) {
this.forumService = forumService;
this.currentUser = currentUser;
}

@GetMapping("/topics")
public ApiResponse<PageResult<ForumTopicResponse>> listTopics(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
return ApiResponse.ok(forumService.listTopics(page, size));
}

@GetMapping("/topics/{topicId}")
public ApiResponse<ForumTopicResponse> getTopic(@PathVariable Long topicId) {
return ApiResponse.ok(forumService.getTopic(topicId));
}

@PostMapping("/topics")
public ApiResponse<ForumTopicResponse> createTopic(@Valid @RequestBody ForumTopicCreateRequest req) {
Long uid = currentUser.userId();
if (uid == null) {
throw new BizException(4011, "请先登录");
}
return ApiResponse.ok(forumService.createTopic(uid, req));
}

@GetMapping("/topics/{topicId}/comments")
public ApiResponse<PageResult<ForumCommentResponse>> listComments(
@PathVariable Long topicId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "50") int size) {
return ApiResponse.ok(forumService.listComments(topicId, page, size));
}

@PostMapping("/topics/{topicId}/comments")
public ApiResponse<ForumCommentResponse> createComment(
@PathVariable Long topicId,
@Valid @RequestBody ForumCommentRequest req) {
Long uid = currentUser.userId();
if (uid == null) {
throw new BizException(4011, "请先登录");
}
return ApiResponse.ok(forumService.createComment(topicId, uid, req));
}

@DeleteMapping("/comments/{commentId}")
public ApiResponse<Void> deleteComment(@PathVariable Long commentId) {
Long uid = currentUser.userId();
if (uid == null) {
throw new BizException(4011, "请先登录");
}
forumService.deleteComment(commentId, uid, currentUser.isAdmin());
return ApiResponse.ok();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yqz.openblog.forum.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public class ForumCommentRequest {

@NotBlank
@Size(max = 2000)
private String content;

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.yqz.openblog.forum.dto;

import java.time.Instant;

public class ForumCommentResponse {

private Long id;
private Long topicId;
private String content;
private Long authorId;
private String authorName;
private String authorAvatar;
private Instant createdAt;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Long getTopicId() {
return topicId;
}

public void setTopicId(Long topicId) {
this.topicId = topicId;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public Long getAuthorId() {
return authorId;
}

public void setAuthorId(Long authorId) {
this.authorId = authorId;
}

public String getAuthorName() {
return authorName;
}

public void setAuthorName(String authorName) {
this.authorName = authorName;
}

public String getAuthorAvatar() {
return authorAvatar;
}

public void setAuthorAvatar(String authorAvatar) {
this.authorAvatar = authorAvatar;
}

public Instant getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yqz.openblog.forum.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public class ForumTopicCreateRequest {

@NotBlank
@Size(max = 200)
private String title;

@NotBlank
@Size(max = 20000)
private String content;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.yqz.openblog.forum.dto;

import java.time.Instant;

public class ForumTopicResponse {

private Long id;
private String title;
private String content;
private String status;
private Long authorId;
private String authorName;
private String authorAvatar;
private Long viewCount;
private Long commentCount;
private Instant createdAt;
private Instant updatedAt;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Long getAuthorId() {
return authorId;
}

public void setAuthorId(Long authorId) {
this.authorId = authorId;
}

public String getAuthorName() {
return authorName;
}

public void setAuthorName(String authorName) {
this.authorName = authorName;
}

public String getAuthorAvatar() {
return authorAvatar;
}

public void setAuthorAvatar(String authorAvatar) {
this.authorAvatar = authorAvatar;
}

public Long getViewCount() {
return viewCount;
}

public void setViewCount(Long viewCount) {
this.viewCount = viewCount;
}

public Long getCommentCount() {
return commentCount;
}

public void setCommentCount(Long commentCount) {
this.commentCount = commentCount;
}

public Instant getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Instant createdAt) {
this.createdAt = createdAt;
}

public Instant getUpdatedAt() {
return updatedAt;
}

public void setUpdatedAt(Instant updatedAt) {
this.updatedAt = updatedAt;
}
}
Loading