116 lines
1.9 KiB
Go
116 lines
1.9 KiB
Go
/*
|
|
* SPDX-FileCopyrightText: 2023 jordi fita mas <jfita@peritasoft.com>
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
package form
|
|
|
|
import (
|
|
"context"
|
|
"database/sql/driver"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"dev.tandem.ws/tandem/camper/pkg/database"
|
|
)
|
|
|
|
type Select struct {
|
|
Name string
|
|
Selected []string
|
|
Options []*Option
|
|
Error error
|
|
}
|
|
|
|
func (s *Select) setError(err error) {
|
|
s.Error = err
|
|
}
|
|
|
|
func (s *Select) Value() (driver.Value, error) {
|
|
return s.String(), nil
|
|
}
|
|
|
|
func (s *Select) String() string {
|
|
if s.Selected == nil {
|
|
return ""
|
|
}
|
|
return s.Selected[0]
|
|
}
|
|
|
|
func (s *Select) Int() int {
|
|
i, err := strconv.Atoi(s.String())
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return i
|
|
}
|
|
|
|
func (s *Select) FillValue(r *http.Request) {
|
|
s.Selected = r.Form[s.Name]
|
|
}
|
|
|
|
func (s *Select) FillValueIndex(r *http.Request, idx int) {
|
|
if vs := r.Form[s.Name]; len(vs) > idx {
|
|
s.Selected = []string{vs[idx]}
|
|
} else {
|
|
s.Selected = nil
|
|
}
|
|
}
|
|
|
|
func (s *Select) ValidOptionsSelected() bool {
|
|
if len(s.Selected) == 0 {
|
|
return false
|
|
}
|
|
for _, selected := range s.Selected {
|
|
if !s.isValidOption(selected) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Select) isValidOption(selected string) bool {
|
|
for _, option := range s.Options {
|
|
if option.Value == selected {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func (s *Select) IsSelected(v string) bool {
|
|
for _, selected := range s.Selected {
|
|
if selected == v {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
type Option struct {
|
|
Value string
|
|
Label string
|
|
}
|
|
|
|
func MustGetOptions(ctx context.Context, conn *database.Conn, sql string, args ...interface{}) []*Option {
|
|
rows, err := conn.Query(ctx, sql, args...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var options []*Option
|
|
for rows.Next() {
|
|
option := &Option{}
|
|
err = rows.Scan(&option.Value, &option.Label)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
options = append(options, option)
|
|
}
|
|
if rows.Err() != nil {
|
|
panic(rows.Err())
|
|
}
|
|
|
|
return options
|
|
}
|