2025-10-05 01:46:48 +08:00
|
|
|
|
// Code scaffolded by goctl. Safe to edit.
|
|
|
|
|
|
// goctl 1.9.1
|
|
|
|
|
|
|
|
|
|
|
|
package article
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
2025-10-08 20:00:30 +08:00
|
|
|
|
"fmt"
|
2025-10-05 01:46:48 +08:00
|
|
|
|
"net/http"
|
2025-10-08 20:00:30 +08:00
|
|
|
|
"strconv"
|
|
|
|
|
|
"strings"
|
2025-10-05 01:46:48 +08:00
|
|
|
|
|
2025-10-06 01:32:16 +08:00
|
|
|
|
"github.com/JACKYMYPERSON/hldrCenter/config"
|
2025-10-05 01:46:48 +08:00
|
|
|
|
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/logic/article"
|
2025-10-08 20:00:30 +08:00
|
|
|
|
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/model"
|
2025-10-05 01:46:48 +08:00
|
|
|
|
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
2025-10-08 20:00:30 +08:00
|
|
|
|
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
2025-10-05 01:46:48 +08:00
|
|
|
|
"github.com/zeromicro/go-zero/rest/httpx"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-10-06 01:32:16 +08:00
|
|
|
|
func DeleteArticleHandler(cfg *config.Config) http.HandlerFunc {
|
2025-10-05 01:46:48 +08:00
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
var req types.DeleteArticleReq
|
2025-10-08 20:00:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 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))
|
2025-10-05 01:46:48 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-10-08 20:00:30 +08:00
|
|
|
|
req.Id = id // 只赋值一次,确保Id正确
|
|
|
|
|
|
|
|
|
|
|
|
// 验证:打印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)
|
2025-10-05 01:46:48 +08:00
|
|
|
|
|
2025-10-08 20:00:30 +08:00
|
|
|
|
l := article.NewDeleteArticleLogic(r.Context(), cfg, articleModel)
|
2025-10-05 01:46:48 +08:00
|
|
|
|
resp, err := l.DeleteArticle(&req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
httpx.ErrorCtx(r.Context(), w, err)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|