添加首页文章页面
This commit is contained in:
@@ -4,23 +4,57 @@
|
||||
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 DeleteArticleHandler(cfg *config.Config) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeleteArticleReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
|
||||
// 1. 从URL路径提取id(核心步骤)
|
||||
pathParts := strings.Split(r.URL.Path, "/")
|
||||
// 完整路径是 "/api/articles/12",split后应为 ["", "api", "articles", "12"]
|
||||
if len(pathParts) < 4 {
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("无效的 URL 路径"))
|
||||
return
|
||||
}
|
||||
idStr := pathParts[3]
|
||||
id, err := strconv.ParseInt(idStr, 10, 64)
|
||||
if err != nil || id <= 0 { // 增加id有效性校验
|
||||
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("无效的文章ID:%s", idStr))
|
||||
return
|
||||
}
|
||||
req.Id = id // 只赋值一次,确保Id正确
|
||||
|
||||
l := article.NewDeleteArticleLogic(r.Context(), cfg)
|
||||
// 验证:打印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,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
articleModel := model.NewArticleModel(conn)
|
||||
|
||||
l := article.NewDeleteArticleLogic(r.Context(), cfg, articleModel)
|
||||
resp, err := l.DeleteArticle(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
|
||||
@@ -33,7 +33,7 @@ func ListArticleHandler(cfg *config.Config) http.HandlerFunc {
|
||||
mysqlCfg.Database,
|
||||
mysqlCfg.Charset,
|
||||
)
|
||||
fmt.Println("接收到articlePost请求")
|
||||
fmt.Println("接收到获取文章列表请求:", req)
|
||||
|
||||
conn := sqlx.NewSqlConn("mysql", dsn)
|
||||
articleModel := model.NewArticleModel(conn)
|
||||
|
||||
@@ -4,23 +4,57 @@
|
||||
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
|
||||
}
|
||||
|
||||
l := article.NewUpdateArticleLogic(r.Context(), cfg)
|
||||
// 新增:打印绑定后的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)
|
||||
|
||||
Reference in New Issue
Block a user