Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retrieve items and their count right #29

Merged
merged 2 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/app/infrastructure/repositories/gorm/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ func (r *ItemRepository) GetByID(id string) (*entities.Item, error) {
func (r *ItemRepository) GetByQueryFilters(queryFilter repositories.QueryFilter, pageFilter *repositories.PageFilter) ([]*entities.Item, error) {
var items []*entities.Item
err := applyFilters(r.db, queryFilter).
Joins("inner join item_keywords on item_keywords.item_id = items.id").
Joins("left join item_keywords on item_keywords.item_id = items.id").
Offset(pageFilter.Offset).
Limit(pageFilter.Limit).
Preload("Keywords").
Group("items.id").
Find(&items).
Error

Expand All @@ -60,8 +61,9 @@ func (r *ItemRepository) GetByQueryFilters(queryFilter repositories.QueryFilter,
func (r *ItemRepository) CountByQueryFilters(queryFilter repositories.QueryFilter) (int64, error) {
var count int64
err := applyFilters(r.db, queryFilter).
Joins("inner join item_keywords on item_keywords.item_id = items.id").
Joins("left join item_keywords on item_keywords.item_id = items.id").
Model(&entities.Item{}).
Select("count(distinct items.id)").
Count(&count).
Error

Expand Down
10 changes: 5 additions & 5 deletions internal/app/infrastructure/repositories/gorm/item_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func TestItemRepositoryGetByQueryFilters(t *testing.T) {
}
rows := sqlmock.NewRows([]string{"id", "sku", "name", "description", "unit", "user_id", "created_at", "updated_at"}).
AddRow(item.ID, item.Sku, item.Name, item.Description, item.Unit, item.UserID, item.CreatedAt, item.UpdatedAt)
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT `items`.`id`,`items`.`sku`,`items`.`name`,`items`.`description`,`items`.`unit`,`items`.`user_id`,`items`.`created_at`,`items`.`updated_at` FROM `items` inner join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ? LIMIT 10 OFFSET 1")).
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT `items`.`id`,`items`.`sku`,`items`.`name`,`items`.`description`,`items`.`unit`,`items`.`user_id`,`items`.`created_at`,`items`.`updated_at` FROM `items` left join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ? GROUP BY `items`.`id` LIMIT 10 OFFSET 1")).
WithArgs("%search%", "%search%", "%search%", item.UserID).
WillReturnRows(rows)
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `item_keywords` WHERE `item_keywords`.`item_id` = ?")).
Expand Down Expand Up @@ -258,7 +258,7 @@ func TestItemRepositoryGetByQueryFiltersErrorCanNotGetByQueryFilters(t *testing.
Limit: 10,
}

dbMock.ExpectQuery(regexp.QuoteMeta("SELECT `items`.`id`,`items`.`sku`,`items`.`name`,`items`.`description`,`items`.`unit`,`items`.`user_id`,`items`.`created_at`,`items`.`updated_at` FROM `items` inner join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ? LIMIT 10 OFFSET 1")).
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT `items`.`id`,`items`.`sku`,`items`.`name`,`items`.`description`,`items`.`unit`,`items`.`user_id`,`items`.`created_at`,`items`.`updated_at` FROM `items` left join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ? GROUP BY `items`.`id` LIMIT 10 OFFSET 1")).
WithArgs("%search%", "%search%", "%search%", userID).
WillReturnError(errors.New("database error"))

Expand Down Expand Up @@ -313,9 +313,9 @@ func TestItemRepositoryCountByQueryFilters(t *testing.T) {
}

count := int64(10)
rows := sqlmock.NewRows([]string{"count"}).
rows := sqlmock.NewRows([]string{"count(distinct items.id)"}).
AddRow(count)
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM `items` inner join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ?")).
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT count(distinct items.id) FROM `items` left join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ?")).
WithArgs("%search%", "%search%", "%search%", userID).
WillReturnRows(rows)

Expand Down Expand Up @@ -368,7 +368,7 @@ func TestItemRepositoryCountByQueryFiltersErrorCanNotCountByQueryFilters(t *test
},
}

dbMock.ExpectQuery(regexp.QuoteMeta("SELECT count(*) FROM `items` inner join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ?")).
dbMock.ExpectQuery(regexp.QuoteMeta("SELECT count(distinct items.id) FROM `items` left join item_keywords on item_keywords.item_id = items.id WHERE (sku LIKE ? OR name LIKE ? OR description LIKE ?) AND user_id = ?")).
WithArgs("%search%", "%search%", "%search%", userID).
WillReturnError(errors.New("database error"))

Expand Down
Loading