2024-05-03 16:10:16 +00:00
|
|
|
package customer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2024-05-03 17:00:02 +00:00
|
|
|
"strconv"
|
2024-05-03 16:10:16 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/auth"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/form"
|
|
|
|
)
|
|
|
|
|
|
|
|
type filterForm struct {
|
2024-05-03 17:00:02 +00:00
|
|
|
company *auth.Company
|
|
|
|
Name *form.Input
|
|
|
|
Email *form.Input
|
|
|
|
Cursor *form.Cursor
|
2024-05-03 16:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFilterForm(company *auth.Company) *filterForm {
|
|
|
|
return &filterForm{
|
|
|
|
company: company,
|
|
|
|
Name: &form.Input{
|
|
|
|
Name: "name",
|
|
|
|
},
|
|
|
|
Email: &form.Input{
|
|
|
|
Name: "email",
|
|
|
|
},
|
2024-05-03 17:00:02 +00:00
|
|
|
Cursor: &form.Cursor{
|
|
|
|
Name: "cursor",
|
|
|
|
PerPage: 25,
|
2024-05-03 16:10:16 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterForm) Parse(r *http.Request) error {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
f.Name.FillValue(r)
|
|
|
|
f.Email.FillValue(r)
|
|
|
|
f.Cursor.FillValue(r)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterForm) BuildQuery(args []interface{}) (string, []interface{}) {
|
|
|
|
var where []string
|
|
|
|
appendWhere := func(expression string, value interface{}) {
|
|
|
|
args = append(args, value)
|
|
|
|
where = append(where, fmt.Sprintf(expression, len(args)))
|
|
|
|
}
|
|
|
|
maybeAppendWhere := func(expression string, value string, conv func(string) interface{}) {
|
|
|
|
if value != "" {
|
|
|
|
if conv == nil {
|
|
|
|
appendWhere(expression, value)
|
|
|
|
} else {
|
|
|
|
appendWhere(expression, conv(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
appendWhere("company_id = $%d", f.company.ID)
|
|
|
|
maybeAppendWhere("name ILIKE $%d", f.Name.Val, func(v string) interface{} {
|
|
|
|
return "%" + v + "%"
|
|
|
|
})
|
|
|
|
maybeAppendWhere("email ILIKE $%d", f.Email.Val, func(v string) interface{} {
|
|
|
|
return "%" + v + "%"
|
|
|
|
})
|
|
|
|
|
2024-05-03 17:00:02 +00:00
|
|
|
if f.Paginated() {
|
|
|
|
params := f.Cursor.Params()
|
2024-05-03 16:10:16 +00:00
|
|
|
if len(params) == 2 {
|
|
|
|
where = append(where, fmt.Sprintf("(name, contact_id) > ($%d, $%d)", len(args)+1, len(args)+2))
|
|
|
|
args = append(args, params[0])
|
|
|
|
args = append(args, params[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(where, ") AND ("), args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterForm) buildCursor(customers []*customerEntry) []*customerEntry {
|
2024-05-03 17:00:02 +00:00
|
|
|
return form.BuildCursor(f.Cursor, customers, func(entry *customerEntry) []string {
|
|
|
|
return []string{entry.Name, strconv.Itoa(entry.ID)}
|
|
|
|
})
|
2024-05-03 16:10:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterForm) HasValue() bool {
|
|
|
|
return f.Name.Val != "" ||
|
|
|
|
f.Email.Val != ""
|
|
|
|
}
|
2024-05-03 17:00:02 +00:00
|
|
|
|
|
|
|
func (f *filterForm) PerPage() int {
|
|
|
|
return f.Cursor.PerPage
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filterForm) Paginated() bool {
|
|
|
|
return f.Cursor.Pagination
|
|
|
|
}
|