Enhance company search functionality with additional filters
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
- Updated the `SearchForm` to include optional filters for `name`, `email`, `phone`, and `employee_count`, allowing for more granular search capabilities. - Modified the `Search` method in the `companyRepository` to handle the new filters, improving the search logic with regex support for `name`, `email`, and `phone`. - Updated API documentation in the `company` handler to reflect the new query parameters for enhanced clarity. This update improves the search functionality within the company domain, providing users with more flexible and precise search options.
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"tm/pkg/logger"
|
||||
@@ -213,18 +212,36 @@ func (r *companyRepository) Search(ctx context.Context, form *SearchForm, pagina
|
||||
filter := bson.M{}
|
||||
|
||||
if form.Search != nil {
|
||||
search := strings.TrimSpace(*form.Search)
|
||||
if search != "" {
|
||||
pattern := regexp.QuoteMeta(search)
|
||||
orConditions := []bson.M{
|
||||
{"name": bson.M{"$regex": pattern, "$options": "i"}},
|
||||
{"email": bson.M{"$regex": pattern, "$options": "i"}},
|
||||
{"phone": bson.M{"$regex": pattern, "$options": "i"}},
|
||||
filter["$text"] = bson.M{"$search": *form.Search}
|
||||
}
|
||||
|
||||
if form.Name != nil {
|
||||
name := strings.TrimSpace(*form.Name)
|
||||
if name != "" {
|
||||
filter["name"] = bson.M{
|
||||
"$regex": regexp.QuoteMeta(name),
|
||||
"$options": "i",
|
||||
}
|
||||
if employeeCount, err := strconv.Atoi(search); err == nil {
|
||||
orConditions = append(orConditions, bson.M{"employee_count": employeeCount})
|
||||
}
|
||||
}
|
||||
|
||||
if form.Email != nil {
|
||||
email := strings.TrimSpace(*form.Email)
|
||||
if email != "" {
|
||||
filter["email"] = bson.M{
|
||||
"$regex": regexp.QuoteMeta(email),
|
||||
"$options": "i",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if form.Phone != nil {
|
||||
phone := strings.TrimSpace(*form.Phone)
|
||||
if phone != "" {
|
||||
filter["phone"] = bson.M{
|
||||
"$regex": regexp.QuoteMeta(phone),
|
||||
"$options": "i",
|
||||
}
|
||||
filter["$or"] = orConditions
|
||||
}
|
||||
}
|
||||
|
||||
@@ -274,7 +291,9 @@ func (r *companyRepository) Search(ctx context.Context, form *SearchForm, pagina
|
||||
}
|
||||
|
||||
// Range filters
|
||||
if form.EmployeeCountMin != nil || form.EmployeeCountMax != nil {
|
||||
if form.EmployeeCount != nil {
|
||||
filter["employee_count"] = *form.EmployeeCount
|
||||
} else if form.EmployeeCountMin != nil || form.EmployeeCountMax != nil {
|
||||
rangeFilter := bson.M{}
|
||||
if form.EmployeeCountMin != nil {
|
||||
rangeFilter["$gte"] = *form.EmployeeCountMin
|
||||
|
||||
Reference in New Issue
Block a user