From 82394f1b9f042b2941aa1a2362e5c88f2df05770 Mon Sep 17 00:00:00 2001 From: ignacior Date: Wed, 14 Feb 2024 10:04:51 -0500 Subject: [PATCH 1/2] fix: group and select distinct items --- internal/app/infrastructure/repositories/gorm/item.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/internal/app/infrastructure/repositories/gorm/item.go b/internal/app/infrastructure/repositories/gorm/item.go index a868424..5adef00 100644 --- a/internal/app/infrastructure/repositories/gorm/item.go +++ b/internal/app/infrastructure/repositories/gorm/item.go @@ -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 @@ -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 From 906047fc4ed5606a60bba1b030517dae8caf1608 Mon Sep 17 00:00:00 2001 From: ignacior Date: Wed, 14 Feb 2024 10:05:07 -0500 Subject: [PATCH 2/2] feat: update gorm repository test --- .../app/infrastructure/repositories/gorm/item_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/app/infrastructure/repositories/gorm/item_test.go b/internal/app/infrastructure/repositories/gorm/item_test.go index 9b6ddd7..6b5dd7d 100644 --- a/internal/app/infrastructure/repositories/gorm/item_test.go +++ b/internal/app/infrastructure/repositories/gorm/item_test.go @@ -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` = ?")). @@ -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")) @@ -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) @@ -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"))