Skip to content

Commit

Permalink
[hotfix] fix tag search sql
Browse files Browse the repository at this point in the history
  • Loading branch information
runs committed Mar 5, 2023
1 parent f128e2b commit 3543cb7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
8 changes: 8 additions & 0 deletions internal/app/api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
//图片
Expand Down
2 changes: 2 additions & 0 deletions internal/app/api/service/bilbil_video.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
18 changes: 10 additions & 8 deletions internal/repository/bilbil_video_pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down

0 comments on commit 3543cb7

Please sign in to comment.