From 96de5f8679e75bdd5a7016525db0077061afd0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20devkcud=20Albanese?= Date: Sat, 2 Nov 2024 19:14:35 -0300 Subject: [PATCH 1/2] fix: Set as else if instead of lone if --- internal/service/repository/component.go | 6 ++---- internal/service/repository/project.go | 12 ++++-------- internal/service/repository/user.go | 8 +++----- 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/internal/service/repository/component.go b/internal/service/repository/component.go index a945a89..3c02265 100644 --- a/internal/service/repository/component.go +++ b/internal/service/repository/component.go @@ -504,14 +504,12 @@ func (cr *componentRepository) Search(issuerID uint, search *dto.SearchComponent query = query.Order("c.created_at " + orderDirection) } else if search.OrderModifiedDate { query = query.Order("c.updated_at " + orderDirection) + } else if search.MostHolders { + query = query.Order("(SELECT COUNT(*) FROM component_holders ch WHERE ch.component_id = c.id) " + orderDirection) } else { query = query.Order("c.created_at " + orderDirection) } - if search.MostHolders { - query = query.Order("(SELECT COUNT(*) FROM component_holders ch WHERE ch.component_id = c.id) " + orderDirection) - } - return cr.paginateComponents(query, page, perPage) } diff --git a/internal/service/repository/project.go b/internal/service/repository/project.go index 46f0859..b068832 100644 --- a/internal/service/repository/project.go +++ b/internal/service/repository/project.go @@ -749,16 +749,12 @@ func (pr *projectRepository) Search(issuerID uint, search *dto.SearchProject, pa query = query.Order("p.created_at " + orderDirection) } else if search.OrderModifiedDate { query = query.Order("p.updated_at " + orderDirection) - } else { - query = query.Order("p.created_at " + orderDirection) - } - - if search.MostFavorites { + } else if search.MostFavorites { query = query.Order("total_favorites " + orderDirection) - } - - if search.MostClones { + } else if search.MostClones { query = query.Order("total_clones " + orderDirection) + } else { + query = query.Order("p.created_at " + orderDirection) } return pr.paginateProjects(query, page, perPage) diff --git a/internal/service/repository/user.go b/internal/service/repository/user.go index 4539b9a..584f7c9 100644 --- a/internal/service/repository/user.go +++ b/internal/service/repository/user.go @@ -113,11 +113,7 @@ func (u userRepository) Search(issuerID uint, search *dto.SearchUser, page, perp query = query.Order("created_at " + orderDirection) } else if search.OrderModifiedDate { query = query.Order("updated_at " + orderDirection) - } else { - query = query.Order("created_at DESC") - } - - if search.MostFollowers { + } else if search.MostFollowers { query = query.Joins(` LEFT JOIN ( SELECT following_id, COUNT(*) AS follower_count @@ -125,6 +121,8 @@ func (u userRepository) Search(issuerID uint, search *dto.SearchUser, page, perp GROUP BY following_id ) follower_counts ON follower_counts.following_id = users.id`). Order("follower_count" + orderDirection + "NULLS LAST") + } else { + query = query.Order("created_at " + orderDirection) } return pagination.Generate[dto.UserProfile](query, page, perpage) From d536036772bd59688108c4096662cc372c59b9e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20devkcud=20Albanese?= Date: Sat, 2 Nov 2024 19:16:22 -0300 Subject: [PATCH 2/2] fix: Add regex prepare name for search in user and component --- internal/service/repository/component.go | 8 +++++++- internal/service/repository/user.go | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/internal/service/repository/component.go b/internal/service/repository/component.go index 3c02265..5716675 100644 --- a/internal/service/repository/component.go +++ b/internal/service/repository/component.go @@ -488,7 +488,13 @@ func (cr *componentRepository) Search(issuerID uint, search *dto.SearchComponent regexp_like(u.first_name, ?, 'i') OR regexp_like(u.last_name, ?, 'i') OR regexp_like(u.username, ?, 'i') - )`, *search.Name, *search.Name, *search.Name, *search.Name, *search.Name) + )`, + utils.RegexPrepareName(*search.Name), + utils.RegexPrepareName(*search.Name), + utils.RegexPrepareName(*search.Name), + utils.RegexPrepareName(*search.Name), + utils.RegexPrepareName(*search.Name), + ) // TODO: Create ranking system } diff --git a/internal/service/repository/user.go b/internal/service/repository/user.go index 584f7c9..8716e6d 100644 --- a/internal/service/repository/user.go +++ b/internal/service/repository/user.go @@ -6,6 +6,7 @@ import ( "github.com/swibly/swibly-api/pkg/aws" "github.com/swibly/swibly-api/pkg/db" "github.com/swibly/swibly-api/pkg/pagination" + "github.com/swibly/swibly-api/pkg/utils" "gorm.io/gorm" ) @@ -88,7 +89,12 @@ func (u userRepository) Search(issuerID uint, search *dto.SearchUser, page, perp Where("show_profile = TRUE") if search.Name != nil { - query = query.Where("regexp_like(first_name, ?, 'i') OR regexp_like(last_name, ?, 'i') OR regexp_like(username, ?, 'i')", *search.Name, *search.Name, *search.Name) + query = query.Where( + "regexp_like(first_name, ?, 'i') OR regexp_like(last_name, ?, 'i') OR regexp_like(username, ?, 'i')", + utils.RegexPrepareName(*search.Name), + utils.RegexPrepareName(*search.Name), + utils.RegexPrepareName(*search.Name), + ) // TODO: Create ranking system }