From 3543cb7d29764d1652779a0bb88c90c9ce508a57 Mon Sep 17 00:00:00 2001 From: RunsTp Date: Sun, 5 Mar 2023 08:56:11 +0800 Subject: [PATCH] [hotfix] fix tag search sql --- internal/app/api/router/router.go | 8 ++++++++ internal/app/api/service/bilbil_video.go | 2 ++ internal/repository/bilbil_video_pg.go | 18 ++++++++++-------- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/internal/app/api/router/router.go b/internal/app/api/router/router.go index de83842..77fee78 100644 --- a/internal/app/api/router/router.go +++ b/internal/app/api/router/router.go @@ -2,11 +2,14 @@ package router import ( "git.vtb.link/eoefans/internal/app/api/handler" + "git.vtb.link/eoefans/internal/app/api/help" "git.vtb.link/eoefans/internal/app/api/middlewares" "git.vtb.link/eoefans/internal/app/api/service" "git.vtb.link/eoefans/internal/pkg/httpserver" "github.com/gin-gonic/gin" "go.uber.org/fx" + "net/http" + "time" ) func Provide() fx.Option { @@ -26,6 +29,11 @@ func InitRouters( // http 异常处理 r.Use(errMiddlewares.Handler) + // 健康检查 + r.GET("/health-check", func(ctx *gin.Context) { + ctx.JSON(http.StatusOK, help.SuccessJson(map[string]string{"now_time": time.Now().Format(time.RFC3339)})) + }) + // 视频搜索 r.GET("/v1/video-interface/advanced-search", handler.BilibiliVideoSearch(bvService)) //图片 diff --git a/internal/app/api/service/bilbil_video.go b/internal/app/api/service/bilbil_video.go index fbf25a0..f690184 100644 --- a/internal/app/api/service/bilbil_video.go +++ b/internal/app/api/service/bilbil_video.go @@ -74,6 +74,8 @@ func (b *BilbilVideo) Search(ctx context.Context, req idl.BilibiliVideoSearchReq list, total, err := bvRepository.Search(queryItems, req.Order, req.Page, defaultQuerySize) if err != nil { return nil, err + } else if list == nil { + list = make([]*idl.BilibiliVideo, 0) } return &idl.BilibiliVideoSearchResp{ diff --git a/internal/repository/bilbil_video_pg.go b/internal/repository/bilbil_video_pg.go index f958773..25fd7cd 100644 --- a/internal/repository/bilbil_video_pg.go +++ b/internal/repository/bilbil_video_pg.go @@ -66,20 +66,22 @@ func (impl *BilibiliVideoPostgresImpl) Search(queryItems []query_parser.QueryIte var newList []*idl.NewBilibiliVideo resp := impl.builderQueryItems(impl.tx, queryItems).Table(bilibiliVideoTableName). Where(fmt.Sprintf("%s.status = ?", bilibiliVideoTableName), idl.BilibiliVideoEnabledStatus). - Order(fmt.Sprintf("%s DESC", order)). - Offset(int((page - 1) * size)).Limit(int(size)). - Find(&newList) + Count(&total) if resp.Error != nil { - return nil, 0, errors.Wrap(resp.Error, fmt.Sprintf("select %s error", bilibiliVideoTableName)) + return nil, 0, errors.Wrap(resp.Error, fmt.Sprintf("count from %s error", bilibiliVideoTableName)) + } else if total == 0 { + return nil, 0, nil } resp = impl.builderQueryItems(impl.tx, queryItems).Table(bilibiliVideoTableName). Where(fmt.Sprintf("%s.status = ?", bilibiliVideoTableName), idl.BilibiliVideoEnabledStatus). - Count(&total) + Order(fmt.Sprintf("%s DESC", order)). + Offset(int((page - 1) * size)).Limit(int(size)). + Find(&newList) if resp.Error != nil { - return nil, 0, errors.Wrap(resp.Error, fmt.Sprintf("count from %s error", bilibiliVideoTableName)) + return nil, 0, errors.Wrap(resp.Error, fmt.Sprintf("select %s error", bilibiliVideoTableName)) } return toBilibiliVideoList(newList), total, nil @@ -90,9 +92,9 @@ func (impl *BilibiliVideoPostgresImpl) builderQueryItems(tx *gorm.DB, queryItems if strings.ToLower(item.Key) == "tag" { switch item.Type { case query_parser.TypeAND: - tx = tx.Where("tag_list @> ARRAY[?]::varchar(64)[]", pq.StringArray(item.Values)) + tx = tx.Where("tag_list @> ?", pq.StringArray(item.Values)) case query_parser.TypeOR: - tx = tx.Where("tag_list && ARRAY[?]::varchar(64)[]", pq.StringArray(item.Values)) + tx = tx.Where("tag_list && ?", pq.StringArray(item.Values)) } continue }