统一投票/悬赏/抽奖截止与状态展示,抽奖支持多奖品行;发帖页悬赏积分对齐表格输入,并在余额不足时于底部就地提示。 Co-authored-by: Cursor <cursoragent@cursor.com>
164 lines
4.6 KiB
Go
164 lines
4.6 KiB
Go
package handler
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/freefire/jiang13-bbs/middleware"
|
|
"github.com/freefire/jiang13-bbs/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func (h *Handlers) respondInteractError(c *gin.Context, err error) {
|
|
switch {
|
|
case errors.Is(err, service.ErrPostNotFound):
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
case errors.Is(err, service.ErrPostForbidden):
|
|
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
|
|
case errors.Is(err, service.ErrInsufficientPoints):
|
|
c.JSON(http.StatusPaymentRequired, gin.H{"error": err.Error()})
|
|
case errors.Is(err, service.ErrAlreadyVoted),
|
|
errors.Is(err, service.ErrPollClosed),
|
|
errors.Is(err, service.ErrPollOptionsLocked),
|
|
errors.Is(err, service.ErrLotteryDrawn),
|
|
errors.Is(err, service.ErrBountySettled):
|
|
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
|
|
default:
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
}
|
|
}
|
|
|
|
// VotePoll 投票
|
|
func (h *Handlers) VotePoll(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
var req struct {
|
|
Options []int `json:"options"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
|
|
return
|
|
}
|
|
post, err := h.Post.VotePoll(claims.ID, uint(id), req.Options)
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|
|
|
|
// ClosePoll 结束投票
|
|
func (h *Handlers) ClosePoll(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
post, err := h.Post.ClosePoll(h.loadActor(claims.ID), claims.ID, uint(id))
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|
|
|
|
// AcceptQuestion 采纳问答答案
|
|
func (h *Handlers) AcceptQuestion(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
var req struct {
|
|
CommentID uint `json:"comment_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "请指定评论"})
|
|
return
|
|
}
|
|
post, err := h.Post.AcceptQuestion(h.loadActor(claims.ID), claims.ID, uint(id), req.CommentID)
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|
|
|
|
// SolveQuestion 手动标已解决
|
|
func (h *Handlers) SolveQuestion(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
post, err := h.Post.SolveQuestion(h.loadActor(claims.ID), claims.ID, uint(id))
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|
|
|
|
// ReopenQuestion 重新打开问答
|
|
func (h *Handlers) ReopenQuestion(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
post, err := h.Post.ReopenQuestion(h.loadActor(claims.ID), claims.ID, uint(id))
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|
|
|
|
// AcceptBounty 采纳评论并发放悬赏
|
|
func (h *Handlers) AcceptBounty(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
var req struct {
|
|
CommentID uint `json:"comment_id" binding:"required"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "请指定评论"})
|
|
return
|
|
}
|
|
post, err := h.Post.AcceptBounty(h.loadActor(claims.ID), claims.ID, uint(id), req.CommentID)
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|
|
|
|
// DrawLottery 开奖
|
|
func (h *Handlers) DrawLottery(c *gin.Context) {
|
|
claims := middleware.CurrentUser(c)
|
|
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "无效的帖子 ID"})
|
|
return
|
|
}
|
|
post, err := h.Post.DrawLottery(h.loadActor(claims.ID), claims.ID, uint(id))
|
|
if err != nil {
|
|
h.respondInteractError(c, err)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"post": post})
|
|
}
|