修改文章查询实现
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -5,8 +5,10 @@ package article
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/JACKYMYPERSON/hldrCenter/config"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/model"
|
||||
"github.com/JACKYMYPERSON/hldrCenter/internal/article/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
@@ -16,18 +18,52 @@ type DetailArticleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
cfg *config.Config
|
||||
model model.ArticleModel
|
||||
}
|
||||
|
||||
func NewDetailArticleLogic(ctx context.Context, cfg *config.Config) *DetailArticleLogic {
|
||||
func NewDetailArticleLogic(ctx context.Context, cfg *config.Config, model model.ArticleModel) *DetailArticleLogic {
|
||||
return &DetailArticleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
cfg: cfg,
|
||||
model: model,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DetailArticleLogic) DetailArticle(req *types.DetailArticleReq) (resp *types.DetailArticleResp, err error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return
|
||||
// 1. 调用数据层查询文章
|
||||
article, err := l.model.FindOne(l.ctx, req.Id)
|
||||
if err != nil {
|
||||
// 处理查询错误:区分"未找到"和"数据库异常"
|
||||
if err == errors.New("文章不存在") {
|
||||
// 文章不存在时,可返回成功但数据为空(或直接返回错误,根据业务需求)
|
||||
return &types.DetailArticleResp{
|
||||
Success: false,
|
||||
Article: types.Article{}, // 空结构体
|
||||
}, nil
|
||||
}
|
||||
// 数据库错误(如连接失败)返回原始错误
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. 转换时间格式(假设数据库查询的是time.Time类型,需转为字符串)
|
||||
// 若数据库存储的是字符串则可省略此步骤
|
||||
typesArticle := types.Article{
|
||||
Id: article.Id,
|
||||
Title: article.Title,
|
||||
Content: article.Content,
|
||||
Cover: article.Cover,
|
||||
CreateAt: article.CreateAt.Format("2006-01-02 15:04:05"), // 转换为"年-月-日 时:分:秒"
|
||||
UpdateAt: article.UpdateAt.Format("2006-01-02 15:04:05"),
|
||||
IsDelete: int(article.IsDelete),
|
||||
Topic: article.Topic,
|
||||
Excerpt: article.Excerpt,
|
||||
}
|
||||
|
||||
// 3. 构造响应
|
||||
resp = &types.DetailArticleResp{
|
||||
Article: typesArticle,
|
||||
Success: true,
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user