diff --git a/internal/service/repository/component.go b/internal/service/repository/component.go index a945a89..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 } @@ -504,14 +510,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..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 } @@ -113,11 +119,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 +127,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)