1 Star 1 Fork 4

bf/ms-go-blog 码神之路

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
post.go 2.07 KB
一键复制 编辑 原始数据 按行查看 历史
bftwq 提交于 2022-07-26 13:21 . 提交代码
package api
import (
"errors"
"fmt"
"go_blog/common"
"go_blog/dao"
"go_blog/models"
"go_blog/service"
"go_blog/utils"
"net/http"
"strconv"
"strings"
"time"
)
func (*Api) SaveAndUpdatePost(w http.ResponseWriter, r *http.Request) {
//获取用户id,判断用户是否登录
token := r.Header.Get("Authorization")
_, claim, err := utils.ParseToken(token)
if err != nil {
fmt.Println("错误信息", err)
common.Error(w, errors.New("登录已过期"))
return
}
uid := claim.Uid
//POST save
method := r.Method
switch method {
case http.MethodPost:
params := common.GetRequestJsonParam(r)
cId := params["categoryId"].(string)
categoryId, _ := strconv.Atoi(cId)
content := params["content"].(string)
markdown := params["markdown"].(string)
slug := params["slug"].(string)
title := params["title"].(string)
postType := params["type"].(float64)
pType := int(postType)
post := &models.Post{
-1,
title,
slug,
content,
markdown,
categoryId,
uid,
0,
pType,
time.Now(),
time.Now(),
}
service.SavePost(post)
common.Success(w, post)
case http.MethodPut:
// update
params := common.GetRequestJsonParam(r)
cId := params["categoryId"].(string)
categoryId, _ := strconv.Atoi(cId)
content := params["content"].(string)
markdown := params["markdown"].(string)
slug := params["slug"].(string)
title := params["title"].(string)
postType := params["type"].(float64)
pidFloat := params["pid"].(float64)
pType := int(postType)
pid := int(pidFloat)
post := &models.Post{
pid,
title,
slug,
content,
markdown,
categoryId,
uid,
0,
pType,
time.Now(),
time.Now(),
}
service.UpdatePost(post)
common.Success(w, post)
}
}
func (*Api) GetPost(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
pIdStr := strings.TrimPrefix(path, "/api/v1/post/")
pid, err := strconv.Atoi(pIdStr)
if err != nil {
common.Error(w, errors.New("不识别此请求路径"))
return
}
post, err := dao.GetPostById(pid)
if err != nil {
common.Error(w, err)
return
}
common.Success(w, post)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/bftwq/ms-go-blog.git
git@gitee.com:bftwq/ms-go-blog.git
bftwq
ms-go-blog
ms-go-blog 码神之路
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385