BrainMinder/models/widget.go
2024-08-22 10:13:16 +02:00

53 lines
1.2 KiB
Go

package models
import (
"database/sql"
"errors"
"strconv"
"brainminder.speedtech.it/internal/database"
"brainminder.speedtech.it/internal/funcs"
)
type WidgetModel struct {
DB *database.DB
}
type Widget struct {
Id int64 `db:"id"`
Name string `db:"name"`
Widget string `db:"widget"`
}
func (model *WidgetModel) All() ([]Widget, bool, error) {
ctx, cancel := database.GetContext()
defer cancel()
query := `SELECT * FROM bm_widgets ORDER BY name`
var rows []Widget
err := model.DB.SelectContext(ctx, &rows, query)
if errors.Is(err, sql.ErrNoRows) {
return nil, false, nil
}
return rows, true, err
}
func (model *WidgetModel) AllAsOptions() []funcs.WidgetOption {
Widgets, _, _ := model.All()
var selectOptions []funcs.WidgetOption
for _, Widget := range Widgets {
selectOptions = append(selectOptions, funcs.WidgetOption{Key: strconv.FormatInt(Widget.Id, 10), Value: Widget.Name})
}
return selectOptions
}
func (model *WidgetModel) UISections() []funcs.WidgetOption {
var selectOptions []funcs.WidgetOption
selectOptions = append(selectOptions, funcs.WidgetOption{Key: "general", Value: "General"})
selectOptions = append(selectOptions, funcs.WidgetOption{Key: "fields", Value: "Fields"})
return selectOptions
}