wx-server/internal/server/post/draft.go

114 lines
2.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package post
import (
"fmt"
"mini_server/internal/dao"
"mini_server/internal/server/help"
"mini_server/internal/server/httpreply"
"net/http"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"gorm.io/datatypes"
)
// 草稿处理逻辑。
type DraftInfo struct {
UserId int32 `json:"user_id"` // 发帖者
Title string `json:"title"`
Content string `json:"content"`
RoleIds datatypes.JSON `json:"role_ids"` // 帖子关联的角色id列表
}
type QueryDraftReply struct {
DraftInfo *DraftInfo `json:"draft_info"`
}
// 上传草稿
func SaveDraft(c *gin.Context) {
reply := httpreply.NewDefaultReplyData()
defer httpreply.Reply(c, reply)
userId, ok := help.GetUserId(c)
if !ok {
reply.Status = http.StatusNotFound
reply.Message = "无法获取用户信息"
return
}
var request UploadRequest
if err := c.ShouldBindJSON(&request); err != nil {
reply.Status = http.StatusBadRequest
reply.Message = fmt.Sprintf("bad request, err:%v", err)
return
}
// 将数据写入数据库中
data := dao.Draft{
UserId: userId,
Title: request.Title,
Content: request.Content,
RoleIds: request.RoleIds,
}
if err := dao.Draft_Save(&data); err != nil {
logrus.Errorf("保存草稿失败userId:%d,err:%v", userId, err)
reply.Status = http.StatusInternalServerError
reply.Message = "保存草稿失败"
return
}
}
// 删除草稿
func DeleteDraft(c *gin.Context) {
reply := httpreply.NewDefaultReplyData()
defer httpreply.Reply(c, reply)
userId, ok := help.GetUserId(c)
if !ok {
reply.Status = http.StatusNotFound
reply.Message = "无法获取用户信息"
return
}
if err := dao.Draft_Delete(userId); err != nil {
logrus.Errorf("删除草稿失败userId:%d,err:%v", userId, err)
reply.Status = http.StatusInternalServerError
reply.Message = "删除草稿失败"
return
}
}
// 查询草稿
func QueryDraft(c *gin.Context) {
reply := httpreply.NewDefaultReplyData()
defer httpreply.Reply(c, reply)
userId, ok := help.GetUserId(c)
if !ok {
reply.Status = http.StatusNotFound
reply.Message = "无法获取用户信息"
return
}
data, err := dao.Draft_Query(userId)
if err != nil {
logrus.Errorf("查询草稿失败userId:%d,err:%v", userId, err)
reply.Status = http.StatusInternalServerError
reply.Message = "查询草稿失败"
return
}
queryDraftReply := QueryDraftReply{DraftInfo: nil}
if data != nil {
queryDraftReply.DraftInfo = &DraftInfo{
UserId: data.UserId,
Title: data.Title,
Content: data.Content,
RoleIds: data.RoleIds,
}
}
reply.Data = queryDraftReply
}