Files
hldrCenter/server/internal/article/handler/article/updatearticlehandler.go
2025-10-08 20:00:30 +08:00

66 lines
1.8 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.
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.1
package article
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/JACKYMYPERSON/hldrCenter/config"
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/model"
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"github.com/zeromicro/go-zero/rest/httpx"
)
func UpdateArticleHandler(cfg *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UpdateArticleReq
pathParts := strings.Split(r.URL.Path, "/")
// 确保路径格式正确(至少包含 4 个部分:["", "api", "articles", "12"]
if len(pathParts) < 4 {
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("无效的 URL 路径"))
return
}
idStr := pathParts[3] // 取路径中最后一个部分(如 "12"
id, err := strconv.ParseInt(idStr, 10, 64)
fmt.Println("访问路径:", id)
req.Id = id
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
// 新增打印绑定后的req.Id确认是否为12
fmt.Println("Handler中获取的文章ID", req.Id)
mysqlCfg := cfg.MySQL
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=true&loc=Local",
mysqlCfg.Username,
mysqlCfg.Password,
mysqlCfg.Host,
mysqlCfg.Port,
mysqlCfg.Database,
mysqlCfg.Charset,
)
conn := sqlx.NewSqlConn("mysql", dsn)
articleModel := model.NewArticleModel(conn)
l := article.NewUpdateArticleLogic(r.Context(), cfg, articleModel)
resp, err := l.UpdateArticle(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}