More work on the new people search

This commit is contained in:
William Bouzourène 2025-03-25 21:55:54 +01:00
parent 9cd17bd8e6
commit 9a41a5f8aa
Signed by: bouzoure
SSH key fingerprint: SHA256:19MbXpLua4rUtk8tunMesD8KUKb91LXLHg8E/qTooww
6 changed files with 337 additions and 121 deletions

View file

@ -0,0 +1,75 @@
package database
import (
"database/sql"
"fmt"
"git.readonly.ch/bouzoure/pop-camarades/helpers"
"git.readonly.ch/bouzoure/pop-camarades/models"
)
type PeopleSearchParams struct {
Advanced bool `json:"advanced"`
Name string `json:"name"`
Section uint `json:"section"`
Active bool `json:"active"`
Archived bool `json:"archived"`
Email string `json:"email"`
Phone string `json:"phone"`
Address string `json:"address"`
PostalCode string `json:"postal_code"`
City string `json:"city"`
Fields map[uint]any `json:"fields"`
}
type PeopleSearchResults struct {
Results []models.Person
Count int64
Pagination helpers.Pagination
}
func PeopleSearch(params PeopleSearchParams, personType string, pageSize int, page int) (PeopleSearchResults, error) {
var results PeopleSearchResults
db, err := helpers.GetDatabase()
if err != nil {
return results, nil
}
var sqlQuery string
var sqlParams []any
sqlQuery = `--sql
SELECT people.ID,
people.is_member,
people.is_contact,
people.first_name,
people.last_name,
people.address1,
people.postal_code,
people.city,
people.section_id,
sections.name AS Section__name
FROM people
INNER JOIN sections
ON people.section_id = sections.id
WHERE is_member = @is_member
AND is_contact = @is_contact`
if personType == "members" {
sqlParams = append(sqlParams, sql.Named("is_member", true))
sqlParams = append(sqlParams, sql.Named("is_contact", false))
} else if personType == "contacts" {
sqlParams = append(sqlParams, sql.Named("is_member", false))
sqlParams = append(sqlParams, sql.Named("is_contact", true))
} else {
return results, fmt.Errorf("unkown person type")
}
sqlResult := db.Raw(sqlQuery, sqlParams...).Scan(&results.Results)
if sqlResult.Error != nil {
return results, sqlResult.Error
}
return results, nil
}