Skip to content

Commit

Permalink
Merge pull request #63 from moevm/back-fix-7
Browse files Browse the repository at this point in the history
added count of all documents matches filter
  • Loading branch information
AnjeZenda authored Dec 6, 2024
2 parents 210c614 + 373f610 commit 03131fb
Show file tree
Hide file tree
Showing 7 changed files with 340 additions and 319 deletions.
2 changes: 2 additions & 0 deletions server/api/plants/v1/plants.proto
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ message GetPlantsV1Request {

message GetPlantsV1Response {
repeated Plant plants = 1;
int64 count = 2;
}

message GetCareRuleV1Request {
Expand Down Expand Up @@ -182,6 +183,7 @@ message GetPlantsWithCareRulesV1Response {
string type = 4;
}
repeated PlantInfo plants = 1;
int64 count = 2;
}


Expand Down
8 changes: 8 additions & 0 deletions server/gen/docs/plants/v1/plants.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,10 @@
"type": "object",
"$ref": "#/definitions/v1Plant"
}
},
"count": {
"type": "string",
"format": "int64"
}
}
},
Expand Down Expand Up @@ -873,6 +877,10 @@
"type": "object",
"$ref": "#/definitions/v1GetPlantsWithCareRulesV1ResponsePlantInfo"
}
},
"count": {
"type": "string",
"format": "int64"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion server/internal/handlers/plants/get_plants_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (h *Handler) GetPlantsV1(
filter.Page = req.Page
filter.Size = req.Size
filter.SortBy = req.Sort
plants, err := h.storage.GetPlants(ctx, filter)
plants, count, err := h.storage.GetPlants(ctx, filter)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not get plants. Error %v", err)
}
Expand All @@ -35,5 +35,6 @@ func (h *Handler) GetPlantsV1(
}
return &api.GetPlantsV1Response{
Plants: result,
Count: count,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (h *Handler) GetPlantsWithCareRulesV1(
if len(req.Filter.TemperatureRegime) != 0 {
filter.Labels["temperature_regime"] = req.Filter.TemperatureRegime
}
rules, err := h.storage.GetPlantsWithCareRules(ctx, filter)
rules, count, err := h.storage.GetPlantsWithCareRules(ctx, filter)
if err != nil {
return nil, status.Error(codes.Internal, "internal error occured")
}
Expand All @@ -47,5 +47,6 @@ func (h *Handler) GetPlantsWithCareRulesV1(
}
return &api.GetPlantsWithCareRulesV1Response{
Plants: result,
Count: count,
}, nil
}
4 changes: 2 additions & 2 deletions server/internal/handlers/plants/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type Storage interface {
GetPlantsWithCareRules(ctx context.Context, fltr *models.Filter) ([]*models.CareRules, error)
GetPlantsWithCareRules(ctx context.Context, fltr *models.Filter) ([]*models.CareRules, int64, error)
CreateNewCareRule(ctx context.Context, rule *models.CareRules) error
GetCareRulesForPlant(ctx context.Context, id string) (*models.CareRules, error)
GetPlants(ctx context.Context, fltr *models.Filter) ([]*models.Plant, error)
GetPlants(ctx context.Context, fltr *models.Filter) ([]*models.Plant, int64, error)
AddPlant(ctx context.Context, plant *models.Plant) error
GetPlantsForTrade(ctx context.Context, id string) ([]*models.Plant, error)
GetPlant(ctx context.Context, id string) (*models.Plant, error)
Expand Down
597 changes: 308 additions & 289 deletions server/internal/pkg/pb/plants/v1/plants.pb.go

Large diffs are not rendered by default.

42 changes: 16 additions & 26 deletions server/internal/storage/plants.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,30 @@ type valsType interface {
string | primitive.ObjectID
}

// {
// _id: ObjectId('5f3e8c1d1a9e3f1b1b2c3d19'),
// species: 'Плющ',
// description: [
// {
// user_id: ObjectId('5f2d8c1d1d8e2f1a1a2b3c7e'),
// description_addition: 'Поливайте мягкой водой, избегая застоя.',
// created_at: ISODate('2024-12-01T10:30:00.000Z')
// }
// ],
// created_at: ISODate('2024-12-01T10:30:00.000Z'),
// updated_at: ISODate('2024-12-01T10:30:00.000Z'),
// image: 'https://avatars.mds.yandex.net/i?id=8c2f8a972981d9594dbcbee96c16cace_l-6489726-images-thumbs&n=13',
// light_condition: 'Полутеневые',
// temperature_regime: 'Средний режим (15-22°C)',
// type: 'Комнатное растение'
// }

func (s *Storage) GetPlantsWithCareRules(ctx context.Context, fltr *models.Filter) ([]*models.CareRules, error) {
func (s *Storage) GetPlantsWithCareRules(ctx context.Context, fltr *models.Filter) ([]*models.CareRules, int64, error) {
collection := s.DataBase.Collection("care_rules")
filter := parseLabelsToBSON(fltr.Labels)
opts := options.Find()
opts.SetLimit(fltr.Size)
opts.SetSkip((fltr.Page - 1) * fltr.Size)
count, err := collection.CountDocuments(ctx, filter)
if err != nil {
return nil, 0, err
}
cursor, err := collection.Find(ctx, filter, opts)
if err != nil {
return nil, err
return nil, 0, err
}
defer cursor.Close(ctx)
rules := make([]*models.CareRules, 0)
for cursor.Next(ctx) {
var rule models.CareRules
if err = cursor.Decode(&rule); err != nil {
return nil, err
return nil, 0, err
}
rules = append(rules, &rule)
}
return rules, nil
return rules, count, nil
}

func (s *Storage) CreateNewCareRule(ctx context.Context, rule *models.CareRules) error {
Expand Down Expand Up @@ -113,7 +99,7 @@ func (s *Storage) GetCareRulesForPlant(ctx context.Context, id string) (*models.
return &result, nil
}

func (s *Storage) GetPlants(ctx context.Context, fltr *models.Filter) ([]*models.Plant, error) {
func (s *Storage) GetPlants(ctx context.Context, fltr *models.Filter) ([]*models.Plant, int64, error) {
filter := bson.D{{"sold_at", time.Time{}}}
opts := options.Find()
if fltr.SortBy != "" {
Expand All @@ -125,19 +111,23 @@ func (s *Storage) GetPlants(ctx context.Context, fltr *models.Filter) ([]*models
filter = append(filter, parseLabelsToBSON(fltr.Labels)...)
}
collection := s.DataBase.Collection("plants")
count, err := collection.CountDocuments(ctx, filter)
if err != nil {
return nil, 0, err
}
cursor, err := collection.Find(ctx, filter, opts)
if err != nil {
return nil, err
return nil, 0, err
}
plants := make([]*models.Plant, 0)
for cursor.Next(ctx) {
var plant models.Plant
if err := cursor.Decode(&plant); err != nil {
return nil, err
return nil, 0, err
}
plants = append(plants, &plant)
}
return plants, nil
return plants, count, nil
}

func (s *Storage) AddPlant(ctx context.Context, plant *models.Plant) error {
Expand Down

0 comments on commit 03131fb

Please sign in to comment.