Skip to content

Commit

Permalink
fix: count error & totalPage error (dragonflyoss#1373) (dragonflyoss#…
Browse files Browse the repository at this point in the history
…1376)

Signed-off-by: LetFu <[email protected]>
  • Loading branch information
chmod100 authored Jun 15, 2022
1 parent 95c92c2 commit eb0ed09
Show file tree
Hide file tree
Showing 12 changed files with 12 additions and 15 deletions.
5 changes: 1 addition & 4 deletions manager/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion manager/service/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/scheduler_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/security_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/seed_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/seed_peer_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion manager/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit eb0ed09

Please sign in to comment.