75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
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
|
|
}
|