Courses, meetings, class sessions, deadlines, readings with per-user progress, materials with tags and file attachments. Timestamps are stored in UTC and held as Europe/Warsaw text so the admin text inputs can write them.
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
package models
|
|
|
|
import "strings"
|
|
|
|
// blank clears each optional text column whose value is empty or only
|
|
// whitespace, so an emptied admin text input stores NULL rather than "".
|
|
func blank(fields ...**string) {
|
|
for _, f := range fields {
|
|
if *f != nil && strings.TrimSpace(**f) == "" {
|
|
*f = nil
|
|
}
|
|
}
|
|
}
|
|
|
|
// temporalErrors is a field-to-messages map in the shape of lagoon.Validate.
|
|
type temporalErrors map[string][]string
|
|
|
|
func (e temporalErrors) add(field, msg string) { e[field] = append(e[field], msg) }
|
|
|
|
func (e temporalErrors) dateTime(field string, v DateTime, required bool) {
|
|
switch {
|
|
case v == "" && required:
|
|
e.add(field, "The "+strings.ReplaceAll(field, "_", " ")+" field is required.")
|
|
case v != "" && !v.Valid():
|
|
e.add(field, "The "+strings.ReplaceAll(field, "_", " ")+" must be a date and time such as 2026-10-03 08:00.")
|
|
}
|
|
}
|
|
|
|
func (e temporalErrors) date(field string, v Date, required bool) {
|
|
switch {
|
|
case v == "" && required:
|
|
e.add(field, "The "+strings.ReplaceAll(field, "_", " ")+" field is required.")
|
|
case v != "" && !v.Valid():
|
|
e.add(field, "The "+strings.ReplaceAll(field, "_", " ")+" must be a date in the YYYY-MM-DD form.")
|
|
}
|
|
}
|
|
|
|
func (e temporalErrors) result() map[string][]string {
|
|
if len(e) == 0 {
|
|
return nil
|
|
}
|
|
return e
|
|
}
|
|
|
|
// TemporalValidator is implemented by models with Date or DateTime columns.
|
|
// The admin controllers call it before a save, because lagoon.Validate has
|
|
// no date rules.
|
|
type TemporalValidator interface {
|
|
TemporalErrors() map[string][]string
|
|
}
|