From eb0ed09c0ad97d1463007a3737518bab0004504a Mon Sep 17 00:00:00 2001 From: chmod100 Date: Wed, 15 Jun 2022 13:26:21 +0800 Subject: [PATCH] fix: count error & totalPage error (#1373) (#1376) Signed-off-by: LetFu --- manager/handlers/handlers.go | 5 +---- manager/service/application.go | 2 +- manager/service/config.go | 2 +- manager/service/job.go | 2 +- manager/service/oauth.go | 2 +- manager/service/scheduler.go | 2 +- manager/service/scheduler_cluster.go | 2 +- manager/service/security_group.go | 2 +- manager/service/security_rule.go | 2 +- manager/service/seed_peer.go | 2 +- manager/service/seed_peer_cluster.go | 2 +- manager/service/user.go | 2 +- 12 files changed, 12 insertions(+), 15 deletions(-) diff --git a/manager/handlers/handlers.go b/manager/handlers/handlers.go index 11620de203b..20b91b2174b 100644 --- a/manager/handlers/handlers.go +++ b/manager/handlers/handlers.go @@ -47,10 +47,7 @@ func (h *Handlers) setPaginationDefault(page, perPage *int) { } func (h *Handlers) setPaginationLinkHeader(ctx *gin.Context, page, perPage, totalCount int) { - totalPage := totalCount / perPage - if totalPage == 0 { - totalPage = 1 - } + totalPage := totalCount/perPage + 1 var prevPage int if page == 1 { diff --git a/manager/service/application.go b/manager/service/application.go index 4e659d34f02..d80bc471a10 100644 --- a/manager/service/application.go +++ b/manager/service/application.go @@ -81,7 +81,7 @@ func (s *service) GetApplication(ctx context.Context, id uint) (*model.Applicati func (s *service) GetApplications(ctx context.Context, q types.GetApplicationsQuery) ([]model.Application, int64, error) { var count int64 applications := []model.Application{} - if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Preload("SeedPeerClusters").Preload("SchedulerClusters").Preload("User").Find(&applications).Count(&count).Error; err != nil { + if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Preload("SeedPeerClusters").Preload("SchedulerClusters").Preload("User").Find(&applications).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/config.go b/manager/service/config.go index 457a804863f..ac6a05d478f 100644 --- a/manager/service/config.go +++ b/manager/service/config.go @@ -81,7 +81,7 @@ func (s *service) GetConfigs(ctx context.Context, q types.GetConfigsQuery) ([]mo Name: q.Name, Value: q.Value, UserID: q.UserID, - }).Find(&configs).Count(&count).Error; err != nil { + }).Find(&configs).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/job.go b/manager/service/job.go index 9647bb3a7c2..0a570a88ea8 100644 --- a/manager/service/job.go +++ b/manager/service/job.go @@ -181,7 +181,7 @@ func (s *service) GetJobs(ctx context.Context, q types.GetJobsQuery) ([]model.Jo Type: q.Type, State: q.State, UserID: q.UserID, - }).Find(&jobs).Count(&count).Error; err != nil { + }).Find(&jobs).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/oauth.go b/manager/service/oauth.go index 597f98d3fb7..95ad82a610f 100644 --- a/manager/service/oauth.go +++ b/manager/service/oauth.go @@ -82,7 +82,7 @@ func (s *service) GetOauths(ctx context.Context, q types.GetOauthsQuery) ([]mode if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.Oauth{ Name: q.Name, ClientID: q.ClientID, - }).Find(&oauths).Count(&count).Error; err != nil { + }).Find(&oauths).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/scheduler.go b/manager/service/scheduler.go index 355bec6f46c..463a9a9d107 100644 --- a/manager/service/scheduler.go +++ b/manager/service/scheduler.go @@ -89,7 +89,7 @@ func (s *service) GetSchedulers(ctx context.Context, q types.GetSchedulersQuery) IP: q.IP, State: q.State, SchedulerClusterID: q.SchedulerClusterID, - }).Find(&schedulers).Count(&count).Error; err != nil { + }).Find(&schedulers).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/scheduler_cluster.go b/manager/service/scheduler_cluster.go index c7384b7b202..3b6066d691f 100644 --- a/manager/service/scheduler_cluster.go +++ b/manager/service/scheduler_cluster.go @@ -143,7 +143,7 @@ func (s *service) GetSchedulerClusters(ctx context.Context, q types.GetScheduler var schedulerClusters []model.SchedulerCluster if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.SchedulerCluster{ Name: q.Name, - }).Preload("SeedPeerClusters").Preload("SecurityGroup").Find(&schedulerClusters).Count(&count).Error; err != nil { + }).Preload("SeedPeerClusters").Preload("SecurityGroup").Find(&schedulerClusters).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/security_group.go b/manager/service/security_group.go index c38a5215fb8..7e297da58a3 100644 --- a/manager/service/security_group.go +++ b/manager/service/security_group.go @@ -75,7 +75,7 @@ func (s *service) GetSecurityGroups(ctx context.Context, q types.GetSecurityGrou var securityGroups []model.SecurityGroup if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.SecurityGroup{ Name: q.Name, - }).Preload("SecurityRules").Find(&securityGroups).Count(&count).Error; err != nil { + }).Preload("SecurityRules").Find(&securityGroups).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/security_rule.go b/manager/service/security_rule.go index fc4f487a624..f7d21736027 100644 --- a/manager/service/security_rule.go +++ b/manager/service/security_rule.go @@ -79,7 +79,7 @@ func (s *service) GetSecurityRules(ctx context.Context, q types.GetSecurityRules var securityRules []model.SecurityRule if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.SecurityRule{ Name: q.Name, - }).Find(&securityRules).Count(&count).Error; err != nil { + }).Find(&securityRules).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/seed_peer.go b/manager/service/seed_peer.go index 390161f683d..d6ec206fab6 100644 --- a/manager/service/seed_peer.go +++ b/manager/service/seed_peer.go @@ -95,7 +95,7 @@ func (s *service) GetSeedPeers(ctx context.Context, q types.GetSeedPeersQuery) ( Port: q.Port, DownloadPort: q.DownloadPort, SeedPeerClusterID: q.SeedPeerClusterID, - }).Find(&seedPeers).Count(&count).Error; err != nil { + }).Find(&seedPeers).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/seed_peer_cluster.go b/manager/service/seed_peer_cluster.go index d56e9ae504b..5d1399e8a1a 100644 --- a/manager/service/seed_peer_cluster.go +++ b/manager/service/seed_peer_cluster.go @@ -107,7 +107,7 @@ func (s *service) GetSeedPeerClusters(ctx context.Context, q types.GetSeedPeerCl var seedPeerClusters []model.SeedPeerCluster if err := s.db.WithContext(ctx).Scopes(model.Paginate(q.Page, q.PerPage)).Where(&model.SeedPeerCluster{ Name: q.Name, - }).Find(&seedPeerClusters).Count(&count).Error; err != nil { + }).Find(&seedPeerClusters).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err } diff --git a/manager/service/user.go b/manager/service/user.go index ffdc7cc14ec..7e1f94cdfe0 100644 --- a/manager/service/user.go +++ b/manager/service/user.go @@ -63,7 +63,7 @@ func (s *service) GetUsers(ctx context.Context, q types.GetUsersQuery) ([]model. Email: q.Email, Location: q.Location, State: q.State, - }).Find(&users).Count(&count).Error; err != nil { + }).Find(&users).Limit(-1).Offset(-1).Count(&count).Error; err != nil { return nil, 0, err }