Skip to content

Commit

Permalink
fix: reduce tag query functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Rorical committed Nov 30, 2023
1 parent c44217b commit c3635a3
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
16 changes: 5 additions & 11 deletions common/database/operations/illust.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,20 +373,14 @@ func (ops *DatabaseOperations) SearchIllust(ctx context.Context, keyword string,
return nil, 0, nil, nil, err
}

func (ops *DatabaseOperations) QueryIllustsByTags(ctx context.Context, musttags []string, shouldtags []string, page int64, limit int64, sortpopularity bool, sortdate bool, resultbanned bool) ([]models.Illust, error) {
func (ops *DatabaseOperations) QueryIllustsByTags(ctx context.Context, tags []string, page int64, limit int64, sortpopularity bool, sortdate bool, resultbanned bool) ([]models.Illust, error) {
var results []models.Illust

filter := bson.M{}
if len(shouldtags) > 0 {
filter["$in"] = shouldtags
}
if len(musttags) > 0 {
filter["$all"] = musttags
}

query := bson.M{
"tags.name": filter,
"banned": false,
"tags.name": bson.M{
"$in": tags,
},
"banned": false,
}
if resultbanned {
query["banned"] = true
Expand Down
4 changes: 2 additions & 2 deletions modules/responser/reader/searchreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ func (r *Reader) SearchTagsSuggestResponse(ctx context.Context, keyword string)
}, nil
}

func (r *Reader) SearchIllustsByTagsResponse(ctx context.Context, musttags []string, shouldtags []string, perfectmatch bool, page int, limit int, sortpopularity bool, sortdate bool) (*models.IllustsResponse, error) {
func (r *Reader) SearchIllustsByTagsResponse(ctx context.Context, tags []string, perfectmatch bool, page int, limit int, sortpopularity bool, sortdate bool) (*models.IllustsResponse, error) {
if perfectmatch {
illusts, err := r.dbops.QueryIllustsByTags(ctx, musttags, shouldtags, int64(page), int64(limit), sortpopularity, sortdate, false)
illusts, err := r.dbops.QueryIllustsByTags(ctx, tags, int64(page), int64(limit), sortpopularity, sortdate, false)
if err != nil {
return nil, err
}
Expand Down
21 changes: 2 additions & 19 deletions modules/responser/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,24 +408,7 @@ func (r *Router) SearchIllustByTagHandler(c *gin.Context) {
if keywords == "" {
return
}
twotags := strings.Split(keywords, "|")
var musttags []string
var shouldtags []string
if len(twotags) > 0 {
if twotags[0] != "" {
musttags = strings.Split(twotags[0], ",")
}
}
if len(twotags) > 1 {
if twotags[1] != "" {
shouldtags = strings.Split(twotags[1], ",")
}
}

if len(musttags) == 0 && len(shouldtags) == 0 {
c.JSON(400, fail(fmt.Sprintf("%s", models.ErrorNoResult)))
return
}
tags := strings.Split(keywords, ",")

page := utils.Atoi(c.Query("page"))
if page < 0 {
Expand All @@ -452,7 +435,7 @@ func (r *Router) SearchIllustByTagHandler(c *gin.Context) {
perfectmatch = false
}

illusts, err := r.reader.SearchIllustsByTagsResponse(ctx, musttags, shouldtags, perfectmatch, int(page), int(limit), sortpop, sortdate)
illusts, err := r.reader.SearchIllustsByTagsResponse(ctx, tags, perfectmatch, int(page), int(limit), sortpop, sortdate)

if err != nil {
if err == models.ErrorNoResult {
Expand Down

0 comments on commit c3635a3

Please sign in to comment.