Skip to content

Commit

Permalink
- 文章列表接口 MongoDB 排序条件优化,如果不是按照创建时间排序,则新增创建时间排序为第二个优先级排序
Browse files Browse the repository at this point in the history
- 代码优化
  • Loading branch information
chenmingyong0423 committed Jan 27, 2024
1 parent fe5bdab commit 87ddd15
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/spf13/viper v1.18.2
go.mongodb.org/mongo-driver v1.13.1
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc
gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)
Expand Down Expand Up @@ -57,7 +58,6 @@ require (
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.18.0 // indirect
golang.org/x/exp v0.0.0-20240103183307-be819d1f06fc // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.16.0 // indirect
Expand Down
6 changes: 5 additions & 1 deletion server/internal/post/repository/post_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,11 @@ func (r *PostRepository) QueryPostsPage(ctx context.Context, postsQueryCondition
findOptions := options.Find()
findOptions.SetSkip(postsQueryCondition.Skip).SetLimit(postsQueryCondition.Size)
if postsQueryCondition.Sorting.Field != nil && postsQueryCondition.Sorting.Order != nil {
findOptions.SetSort(bsonx.M(*postsQueryCondition.Sorting.Field, orderConvertToInt(*postsQueryCondition.Sorting.Order)))
sortBuilder := bsonx.NewD().Add(*postsQueryCondition.Sorting.Field, orderConvertToInt(*postsQueryCondition.Sorting.Order))
if *postsQueryCondition.Sorting.Field != "create_time" {
sortBuilder.Add("create_time", -1)
}
findOptions.SetSort(sortBuilder.Build())
} else {
findOptions.SetSort(bsonx.M("create_time", -1))
}
Expand Down
10 changes: 8 additions & 2 deletions server/internal/tag/service/tags_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ package service

import (
"context"
"fmt"
"log/slog"
"net/http"

"github.com/gin-gonic/gin"

"github.com/chenmingyong0423/fnote/server/internal/count_stats/service"
"github.com/chenmingyong0423/fnote/server/internal/pkg/api"
"github.com/chenmingyong0423/fnote/server/internal/pkg/domain"
Expand Down Expand Up @@ -81,7 +85,8 @@ func (s *TagService) DeleteTag(ctx context.Context, id string) error {
// 分类数量 -1
gErr := s.countStatsService.DecreaseByReferenceIdAndType(ctx, domain.CountStatsTypeTagCount.ToString(), domain.CountStatsTypeTagCount)
if gErr != nil {

l := slog.Default().With("X-Request-ID", ctx.(*gin.Context).GetString("X-Request-ID"))
l.WarnContext(ctx, fmt.Sprintf("%+v", gErr))
}
}()
return nil
Expand Down Expand Up @@ -112,7 +117,8 @@ func (s *TagService) AdminCreateTag(ctx context.Context, tag domain.Tag) error {
// 分类数量 +1
gErr := s.countStatsService.IncreaseByReferenceIdAndType(ctx, domain.CountStatsTypeTagCount.ToString(), domain.CountStatsTypeTagCount)
if gErr != nil {

l := slog.Default().With("X-Request-ID", ctx.(*gin.Context).GetString("X-Request-ID"))
l.WarnContext(ctx, fmt.Sprintf("%+v", gErr))
}
}()
return nil
Expand Down
3 changes: 2 additions & 1 deletion server/internal/visit_log/repository/dao/vl_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ package dao

import (
"context"
"github.com/chenmingyong0423/go-mongox/builder/query"
"time"

"github.com/chenmingyong0423/go-mongox/builder/query"

"github.com/chenmingyong0423/go-mongox"

"github.com/pkg/errors"
Expand Down
1 change: 1 addition & 0 deletions server/internal/visit_log/service/vl_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package service

import (
"context"

"github.com/chenmingyong0423/fnote/server/internal/pkg/domain"
"github.com/chenmingyong0423/fnote/server/internal/visit_log/repository"
"github.com/pkg/errors"
Expand Down

0 comments on commit 87ddd15

Please sign in to comment.