From 8f3b0e5489fae71d8a1ee691c08b97f3e3025d8e Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 12 Apr 2024 19:39:51 +0300 Subject: [PATCH] add pagination methods --- internal/controllers/fields.go | 2 +- internal/controllers/pipelines.go | 3 +-- internal/controllers/steps.go | 2 +- internal/controllers/tags.go | 3 +-- internal/controllers/user.go | 3 +-- internal/models/resp.go | 10 ++++----- internal/repository/fields.go | 36 ++++++++++++++++++++++++++++--- internal/repository/pipelines.go | 36 ++++++++++++++++++++++++++++--- internal/repository/steps.go | 36 ++++++++++++++++++++++++++++--- internal/repository/tags.go | 36 ++++++++++++++++++++++++++++--- internal/repository/user.go | 36 ++++++++++++++++++++++++++++--- internal/service/fields.go | 7 +++--- internal/service/pipelines.go | 7 +++--- internal/service/steps.go | 7 +++--- internal/service/tags.go | 7 +++--- internal/service/user.go | 6 +++--- 16 files changed, 194 insertions(+), 43 deletions(-) diff --git a/internal/controllers/fields.go b/internal/controllers/fields.go index 4a0567d..05336da 100644 --- a/internal/controllers/fields.go +++ b/internal/controllers/fields.go @@ -8,7 +8,7 @@ func (c *Controller) GettingFieldsFromCash(ctx *fiber.Ctx) error { return err } - response, err := c.service.GettingFieldsFromCash(ctx.Context()) + response, err := c.service.GettingFieldsFromCash(ctx.Context(), req) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") } diff --git a/internal/controllers/pipelines.go b/internal/controllers/pipelines.go index 14b2c89..414f577 100644 --- a/internal/controllers/pipelines.go +++ b/internal/controllers/pipelines.go @@ -17,8 +17,7 @@ func (c *Controller) GettingPipelinesFromCash(ctx *fiber.Ctx) error { return err } - response, err := c.service.GettingPipelinesFromCash(ctx.Context()) - + response, err := c.service.GettingPipelinesFromCash(ctx.Context(), req) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") } diff --git a/internal/controllers/steps.go b/internal/controllers/steps.go index e093fe2..7d7b208 100644 --- a/internal/controllers/steps.go +++ b/internal/controllers/steps.go @@ -8,7 +8,7 @@ func (c *Controller) GettingStepsFromCash(ctx *fiber.Ctx) error { return err } - response, err := c.service.GettingStepsFromCash(ctx.Context()) + response, err := c.service.GettingStepsFromCash(ctx.Context(), req) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") diff --git a/internal/controllers/tags.go b/internal/controllers/tags.go index cbc7804..dada7e6 100644 --- a/internal/controllers/tags.go +++ b/internal/controllers/tags.go @@ -8,8 +8,7 @@ func (c *Controller) GettingTagsFromCash(ctx *fiber.Ctx) error { return err } - response, err := c.service.GettingTagsFromCash(ctx.Context()) - + response, err := c.service.GettingTagsFromCash(ctx.Context(), req) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") } diff --git a/internal/controllers/user.go b/internal/controllers/user.go index 2c649be..2abab48 100644 --- a/internal/controllers/user.go +++ b/internal/controllers/user.go @@ -23,8 +23,7 @@ func (c *Controller) GettingUserFromCash(ctx *fiber.Ctx) error { return err } - response, err := c.service.GettingUserFromCash(ctx.Context()) - + response, err := c.service.GettingUserFromCash(ctx.Context(), req) if err != nil { return ctx.Status(fiber.StatusInternalServerError).SendString("Internal Server Error") } diff --git a/internal/models/resp.go b/internal/models/resp.go index db92920..b8895a2 100644 --- a/internal/models/resp.go +++ b/internal/models/resp.go @@ -38,21 +38,21 @@ type ListSavedIDUTMResp struct { type UserListFieldsResp struct { /* - общее количество кастомных полей, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список кастомных полей, которые были закешированы нашим сервисом*/ Items []Field `json:"items"` } type UserListPipelinesResp struct { /* - общее количество воронок, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список воронок, которые были закешированы нашим сервисом*/ Items []Pipeline `json:"items"` } type UserListResp struct { /* - общее количество юзеров, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список юзеров, которые были закешированы нашим сервисом*/ Items []User `json:"items"` } @@ -61,12 +61,12 @@ type UserListStepsResp struct { /* - список шагов воронок, которые были закешированы нашим сервисом*/ Items []Step `json:"items"` /* - общее количество шагов воронок, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` } type UserListTagsResp struct { /* - общее количество тегов, которые у нас закешированы для этого пользователя*/ - Count int `json:"count"` + Count int64 `json:"count"` /* - список тегов, которые были закешированы нашим сервисом*/ Items []Tag `json:"items"` } diff --git a/internal/repository/fields.go b/internal/repository/fields.go index 4df2559..cd4daf3 100644 --- a/internal/repository/fields.go +++ b/internal/repository/fields.go @@ -6,13 +6,43 @@ import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "time" ) -func (r *Repository) GettingFieldsFromCash(ctx context.Context) (*models.UserListFieldsResp, error) { - //TODO:IMPLEMENT ME +func (r *Repository) GettingFieldsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListFieldsResp, error) { + offset := (req.Page - 1) * req.Size - return &models.UserListFieldsResp{}, nil + totalFields, err := r.fields.CountDocuments(ctx, bson.M{"deleted": false}) + if err != nil { + return nil, err + } + + var fields []models.Field + + cursor, err := r.fields.Find(ctx, bson.M{"deleted": false}, options.Find().SetLimit(int64(req.Size)).SetSkip(int64(offset))) + if err != nil { + return nil, err + } + defer cursor.Close(ctx) + + for cursor.Next(ctx) { + var field models.Field + if err := cursor.Decode(&field); err != nil { + return nil, err + } + fields = append(fields, field) + } + if err := cursor.Err(); err != nil { + return nil, err + } + + fieldListResp := &models.UserListFieldsResp{ + Count: totalFields, + Items: fields, + } + + return fieldListResp, nil } func (r *Repository) UpdateListCustom(ctx context.Context) error { diff --git a/internal/repository/pipelines.go b/internal/repository/pipelines.go index 349bb39..61f6fbd 100644 --- a/internal/repository/pipelines.go +++ b/internal/repository/pipelines.go @@ -6,6 +6,7 @@ import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "time" ) @@ -16,10 +17,39 @@ func (r *Repository) UpdateListPipelines(ctx context.Context) error { } -func (r *Repository) GettingPipelinesFromCash(ctx context.Context) (*models.UserListPipelinesResp, error) { - //TODO:IMPLEMENT ME +func (r *Repository) GettingPipelinesFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListPipelinesResp, error) { + offset := (req.Page - 1) * req.Size - return &models.UserListPipelinesResp{}, nil + totalPipelines, err := r.pipelines.CountDocuments(ctx, bson.M{"deleted": false}) + if err != nil { + return nil, err + } + + var pipelines []models.Pipeline + + cursor, err := r.pipelines.Find(ctx, bson.M{"deleted": false}, options.Find().SetLimit(int64(req.Size)).SetSkip(int64(offset))) + if err != nil { + return nil, err + } + defer cursor.Close(ctx) + + for cursor.Next(ctx) { + var pipeline models.Pipeline + if err := cursor.Decode(&pipeline); err != nil { + return nil, err + } + pipelines = append(pipelines, pipeline) + } + if err := cursor.Err(); err != nil { + return nil, err + } + + pipelineListResp := &models.UserListPipelinesResp{ + Count: totalPipelines, + Items: pipelines, + } + + return pipelineListResp, nil } func (r *Repository) CheckPipelines(ctx context.Context, accountID string, pipelines []amo.Pipeline) error { diff --git a/internal/repository/steps.go b/internal/repository/steps.go index e74d242..251acd2 100644 --- a/internal/repository/steps.go +++ b/internal/repository/steps.go @@ -6,13 +6,43 @@ import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "time" ) -func (r *Repository) GettingStepsFromCash(ctx context.Context) (*models.UserListStepsResp, error) { - //TODO:IMPLEMENT ME +func (r *Repository) GettingStepsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListStepsResp, error) { + offset := (req.Page - 1) * req.Size - return &models.UserListStepsResp{}, nil + totalSteps, err := r.steps.CountDocuments(ctx, bson.M{"deleted": false}) + if err != nil { + return nil, err + } + + var steps []models.Step + + cursor, err := r.steps.Find(ctx, bson.M{"deleted": false}, options.Find().SetLimit(int64(req.Size)).SetSkip(int64(offset))) + if err != nil { + return nil, err + } + defer cursor.Close(ctx) + + for cursor.Next(ctx) { + var step models.Step + if err := cursor.Decode(&step); err != nil { + return nil, err + } + steps = append(steps, step) + } + if err := cursor.Err(); err != nil { + return nil, err + } + + stepListResp := &models.UserListStepsResp{ + Count: totalSteps, + Items: steps, + } + + return stepListResp, nil } func (r *Repository) UpdateListSteps(ctx context.Context) error { diff --git a/internal/repository/tags.go b/internal/repository/tags.go index 424cf15..cb64779 100644 --- a/internal/repository/tags.go +++ b/internal/repository/tags.go @@ -6,13 +6,43 @@ import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "time" ) -func (r *Repository) GettingTagsFromCash(ctx context.Context) (*models.UserListTagsResp, error) { - //TODO:IMPLEMENT ME +func (r *Repository) GettingTagsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListTagsResp, error) { + offset := (req.Page - 1) * req.Size - return &models.UserListTagsResp{}, nil + totalTags, err := r.tags.CountDocuments(ctx, bson.M{"deleted": false}) + if err != nil { + return nil, err + } + + var tags []models.Tag + + cursor, err := r.tags.Find(ctx, bson.M{"deleted": false}, options.Find().SetLimit(int64(req.Size)).SetSkip(int64(offset))) + if err != nil { + return nil, err + } + defer cursor.Close(ctx) + + for cursor.Next(ctx) { + var tag models.Tag + if err := cursor.Decode(&tag); err != nil { + return nil, err + } + tags = append(tags, tag) + } + if err := cursor.Err(); err != nil { + return nil, err + } + + tagListResp := &models.UserListTagsResp{ + Count: totalTags, + Items: tags, + } + + return tagListResp, nil } func (r *Repository) UpdateListTags(ctx context.Context) error { diff --git a/internal/repository/user.go b/internal/repository/user.go index 9ddf551..bf94ad6 100644 --- a/internal/repository/user.go +++ b/internal/repository/user.go @@ -5,6 +5,7 @@ import ( "context" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "time" ) @@ -15,10 +16,39 @@ func (r *Repository) UpdateListUsers(ctx context.Context) error { } -func (r *Repository) GettingUserFromCash(ctx context.Context) (*models.UserListResp, error) { - //TODO:IMPLEMENT ME +func (r *Repository) GettingUserFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListResp, error) { + offset := (req.Page - 1) * req.Size - return &models.UserListResp{}, nil + totalUsers, err := r.mdbUser.CountDocuments(ctx, bson.M{"Deleted": false}) + if err != nil { + return nil, err + } + + var users []models.User + + cursor, err := r.mdbUser.Find(ctx, bson.M{"Deleted": false}, options.Find().SetLimit(int64(req.Size)).SetSkip(int64(offset))) + if err != nil { + return nil, err + } + defer cursor.Close(ctx) + + for cursor.Next(ctx) { + var user models.User + if err := cursor.Decode(&user); err != nil { + return nil, err + } + users = append(users, user) + } + if err := cursor.Err(); err != nil { + return nil, err + } + + userListResp := &models.UserListResp{ + Count: totalUsers, + Items: users, + } + + return userListResp, nil } func (r *Repository) SoftDeleteAccount(ctx context.Context, accountID string) error { diff --git a/internal/service/fields.go b/internal/service/fields.go index 858d30c..3cca471 100644 --- a/internal/service/fields.go +++ b/internal/service/fields.go @@ -3,12 +3,13 @@ package service import ( "amocrm/internal/models" "context" + "go.uber.org/zap" ) -func (s *Service) GettingFieldsFromCash(ctx context.Context) (*models.UserListFieldsResp, error) { - - response, err := s.repository.GettingFieldsFromCash(ctx) +func (s *Service) GettingFieldsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListFieldsResp, error) { + response, err := s.repository.GettingFieldsFromCash(ctx, req) if err != nil { + s.logger.Error("error getting fields with pagination", zap.Error(err)) return nil, err } return response, nil diff --git a/internal/service/pipelines.go b/internal/service/pipelines.go index 2992314..9915657 100644 --- a/internal/service/pipelines.go +++ b/internal/service/pipelines.go @@ -3,6 +3,7 @@ package service import ( "amocrm/internal/models" "context" + "go.uber.org/zap" ) func (s *Service) UpdateListPipelines(ctx context.Context) error { @@ -14,10 +15,10 @@ func (s *Service) UpdateListPipelines(ctx context.Context) error { return nil } -func (s *Service) GettingPipelinesFromCash(ctx context.Context) (*models.UserListPipelinesResp, error) { - - response, err := s.repository.GettingPipelinesFromCash(ctx) +func (s *Service) GettingPipelinesFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListPipelinesResp, error) { + response, err := s.repository.GettingPipelinesFromCash(ctx, req) if err != nil { + s.logger.Error("error getting pipelines with pagination", zap.Error(err)) return nil, err } return response, nil diff --git a/internal/service/steps.go b/internal/service/steps.go index c451dca..d02cbed 100644 --- a/internal/service/steps.go +++ b/internal/service/steps.go @@ -3,12 +3,13 @@ package service import ( "amocrm/internal/models" "context" + "go.uber.org/zap" ) -func (s *Service) GettingStepsFromCash(ctx context.Context) (*models.UserListStepsResp, error) { - - response, err := s.repository.GettingStepsFromCash(ctx) +func (s *Service) GettingStepsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListStepsResp, error) { + response, err := s.repository.GettingStepsFromCash(ctx, req) if err != nil { + s.logger.Error("error getting steps with pagination", zap.Error(err)) return nil, err } return response, nil diff --git a/internal/service/tags.go b/internal/service/tags.go index efb73f8..729b2b5 100644 --- a/internal/service/tags.go +++ b/internal/service/tags.go @@ -3,12 +3,13 @@ package service import ( "amocrm/internal/models" "context" + "go.uber.org/zap" ) -func (s *Service) GettingTagsFromCash(ctx context.Context) (*models.UserListTagsResp, error) { - - response, err := s.repository.GettingTagsFromCash(ctx) +func (s *Service) GettingTagsFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListTagsResp, error) { + response, err := s.repository.GettingTagsFromCash(ctx, req) if err != nil { + s.logger.Error("error getting tags with pagination", zap.Error(err)) return nil, err } return response, nil diff --git a/internal/service/user.go b/internal/service/user.go index da2922a..12dd514 100644 --- a/internal/service/user.go +++ b/internal/service/user.go @@ -15,10 +15,10 @@ func (s *Service) UpdateListUsers(ctx context.Context) error { return nil } -func (s *Service) GettingUserFromCash(ctx context.Context) (*models.UserListResp, error) { - - response, err := s.repository.GettingUserFromCash(ctx) +func (s *Service) GettingUserFromCash(ctx context.Context, req *models.PaginationReq) (*models.UserListResp, error) { + response, err := s.repository.GettingUserFromCash(ctx, req) if err != nil { + s.logger.Error("error getting users with pagination", zap.Error(err)) return nil, err } return response, nil