代码拉取完成,页面将自动刷新
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)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。