2025-01-13 00:22:12 +08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
import (
|
2025-01-13 16:32:03 +08:00
|
|
|
"go_redis_learn2/common/req"
|
|
|
|
response "go_redis_learn2/common/resp"
|
2025-01-13 00:22:12 +08:00
|
|
|
"go_redis_learn2/domain"
|
2025-01-13 16:32:03 +08:00
|
|
|
"go_redis_learn2/pkg"
|
2025-01-13 00:22:12 +08:00
|
|
|
"go_redis_learn2/service"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IPostController interface {
|
|
|
|
CreatePost(c *gin.Context)
|
|
|
|
DeletePost(c *gin.Context)
|
|
|
|
UpdatePost(c *gin.Context)
|
2025-01-13 16:32:03 +08:00
|
|
|
SearchAllPost(c *gin.Context)
|
2025-01-13 00:22:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type PostHandler struct {
|
|
|
|
srv *service.PostService
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ IPostController = (*PostHandler)(nil)
|
|
|
|
|
|
|
|
func NewPostHandler(srv *service.PostService) *PostHandler {
|
|
|
|
return &PostHandler{
|
|
|
|
srv: srv,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (p *PostHandler) CreatePost(c *gin.Context) {
|
2025-01-13 16:32:03 +08:00
|
|
|
var postReq req.PostRequest
|
|
|
|
if c.ShouldBindJSON(&postReq) != nil {
|
2025-01-13 00:22:12 +08:00
|
|
|
response.FailWithMessage("参数错误", c)
|
|
|
|
return
|
|
|
|
}
|
2025-01-13 16:32:03 +08:00
|
|
|
post := domain.Post{
|
|
|
|
Title: postReq.Title,
|
|
|
|
Text: postReq.Text,
|
|
|
|
}
|
2025-01-13 00:22:12 +08:00
|
|
|
err := p.srv.CreatePost(post)
|
|
|
|
if err != nil {
|
|
|
|
response.FailWithMessage("创建文章失败", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response.OkWithMessage("创建文章成功", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostHandler) DeletePost(c *gin.Context) {
|
|
|
|
id := c.Param("id")
|
2025-01-13 16:32:03 +08:00
|
|
|
idVal, err := pkg.StringToUint(id)
|
|
|
|
if err != nil {
|
|
|
|
response.FailWithMessage("id参数不合法", c)
|
2025-01-13 00:22:12 +08:00
|
|
|
return
|
|
|
|
}
|
2025-01-13 16:32:03 +08:00
|
|
|
if p.srv.DeletePost(idVal) != nil {
|
2025-01-13 00:22:12 +08:00
|
|
|
response.FailWithMessage("删除文章失败", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response.OkWithMessage("删除文章成功", c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PostHandler) UpdatePost(c *gin.Context) {
|
2025-01-13 16:32:03 +08:00
|
|
|
var postReq req.PostRequest
|
|
|
|
id := c.Param("id")
|
|
|
|
if c.ShouldBindJSON(&postReq) != nil {
|
2025-01-13 00:22:12 +08:00
|
|
|
response.FailWithMessage("绑定post失败", c)
|
|
|
|
return
|
|
|
|
}
|
2025-01-13 16:32:03 +08:00
|
|
|
idVal, err := pkg.StringToUint(id)
|
2025-01-13 00:22:12 +08:00
|
|
|
if err != nil {
|
2025-01-13 16:32:03 +08:00
|
|
|
response.FailWithMessage("id参数不合法", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
post := domain.Post{
|
|
|
|
ID: idVal,
|
|
|
|
Title: postReq.Title,
|
|
|
|
Text: postReq.Text,
|
|
|
|
}
|
|
|
|
if p.srv.UpdatePost(post) != nil {
|
2025-01-13 00:22:12 +08:00
|
|
|
response.FailWithMessage("更新文章失败", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response.OkWithMessage("更新文章成功", c)
|
|
|
|
}
|
|
|
|
|
2025-01-13 16:32:03 +08:00
|
|
|
func (p *PostHandler) SearchAllPost(c *gin.Context) {
|
2025-01-13 00:22:12 +08:00
|
|
|
posts, err := p.srv.GetAllPosts()
|
|
|
|
if err != nil {
|
|
|
|
response.FailWithMessage("文章获取失败", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
response.OkWithData(posts, c)
|
|
|
|
}
|