修改文章查询实现

This commit is contained in:
2025-11-04 11:42:22 +08:00
parent 7c72434dfb
commit 0b03178feb
2 changed files with 74 additions and 8 deletions

View File

@@ -4,23 +4,53 @@
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 DetailArticleHandler(cfg *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.DetailArticleReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
pathParts := strings.Split(r.URL.Path, "/")
if len(pathParts) < 3 { // 确保路径格式正确
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("invalid path format"))
return
}
idStr := pathParts[3]
id, err := strconv.ParseInt(idStr, 10, 64)
if err != nil {
httpx.ErrorCtx(r.Context(), w, fmt.Errorf("invalid meeting ID"))
return
}
fmt.Println("idStr:", idStr)
req.Id = id
l := article.NewDetailArticleLogic(r.Context(), cfg)
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.NewDetailArticleLogic(r.Context(), cfg, ArticleModel)
resp, err := l.DetailArticle(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)