2022-08-14 05:44:57 +00:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2022-08-14 21:40:57 +00:00
|
|
|
"strconv"
|
2022-08-14 05:44:57 +00:00
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/templates"
|
2022-08-14 05:44:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Search controller.
|
|
|
|
func Search() http.HandlerFunc {
|
|
|
|
tmpl := templates.Must("account/search.html")
|
2022-08-14 21:40:57 +00:00
|
|
|
|
|
|
|
// Whitelist for ordering options.
|
|
|
|
var sortWhitelist = []string{
|
|
|
|
"last_login_at desc",
|
|
|
|
"created_at desc",
|
|
|
|
"username",
|
|
|
|
"lower(name)",
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Search filters.
|
|
|
|
var (
|
|
|
|
isCertified = r.FormValue("certified")
|
|
|
|
username = r.FormValue("username") // email or username
|
|
|
|
gender = r.FormValue("gender")
|
|
|
|
orientation = r.FormValue("orientation")
|
|
|
|
maritalStatus = r.FormValue("marital_status")
|
2022-08-14 21:40:57 +00:00
|
|
|
sort = r.FormValue("sort")
|
|
|
|
sortOK bool
|
|
|
|
ageMin int
|
|
|
|
ageMax int
|
2022-08-14 05:44:57 +00:00
|
|
|
)
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
ageMin, _ = strconv.Atoi(r.FormValue("age_min"))
|
|
|
|
ageMax, _ = strconv.Atoi(r.FormValue("age_max"))
|
|
|
|
if ageMin > ageMax {
|
|
|
|
ageMin, ageMax = ageMax, ageMin
|
|
|
|
}
|
|
|
|
|
2022-08-15 00:45:55 +00:00
|
|
|
// Get current user.
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't get current user!")
|
|
|
|
templates.Redirect(w, "/")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// Sort options.
|
|
|
|
for _, v := range sortWhitelist {
|
|
|
|
if sort == v {
|
|
|
|
sortOK = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !sortOK {
|
|
|
|
sort = "last_login_at desc"
|
|
|
|
}
|
|
|
|
|
2022-08-14 05:44:57 +00:00
|
|
|
// Default
|
|
|
|
if isCertified == "" {
|
|
|
|
isCertified = "true"
|
|
|
|
}
|
|
|
|
|
|
|
|
pager := &models.Pagination{
|
|
|
|
PerPage: config.PageSizeMemberSearch,
|
2022-08-14 21:40:57 +00:00
|
|
|
Sort: sort,
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
pager.ParsePage(r)
|
|
|
|
|
2022-09-09 04:42:20 +00:00
|
|
|
users, err := models.SearchUsers(currentUser, &models.UserSearch{
|
2022-08-14 05:44:57 +00:00
|
|
|
EmailOrUsername: username,
|
|
|
|
Gender: gender,
|
|
|
|
Orientation: orientation,
|
|
|
|
MaritalStatus: maritalStatus,
|
|
|
|
Certified: isCertified == "true",
|
2022-08-14 21:40:57 +00:00
|
|
|
AgeMin: ageMin,
|
|
|
|
AgeMax: ageMax,
|
2022-08-14 05:44:57 +00:00
|
|
|
}, pager)
|
|
|
|
if err != nil {
|
|
|
|
session.FlashError(w, r, "Couldn't search users: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var vars = map[string]interface{}{
|
|
|
|
"Users": users,
|
|
|
|
"Pager": pager,
|
|
|
|
"Enum": config.ProfileEnums,
|
|
|
|
|
|
|
|
// Search filter values.
|
|
|
|
"Certified": isCertified,
|
|
|
|
"Gender": gender,
|
|
|
|
"Orientation": orientation,
|
|
|
|
"MaritalStatus": maritalStatus,
|
|
|
|
"EmailOrUsername": username,
|
2022-08-14 21:40:57 +00:00
|
|
|
"AgeMin": ageMin,
|
|
|
|
"AgeMax": ageMax,
|
|
|
|
"Sort": sort,
|
2022-08-14 05:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := tmpl.Execute(w, r, vars); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|