Files
sm-bm-plugin/controllers/hooks.go
Jakub Zych 3241cc9894 feat: zjazd (meeting) on readings and materials
The Pulpit hero lists what to read for the current or next zjazd, so
readings and materials get a nullable meeting_id (FK to jz_bm_meetings,
ON DELETE SET NULL, indexed). The reading admin form gets a "Na zjazd"
picker, the student reading JSON carries meeting_id, and the management
/readings records filter and validate it.
2026-10-27 01:41:48 +01:00

68 lines
1.8 KiB
Go

package controllers
import (
"context"
"git.golem15.com/golem15/summercms/modules/cabana"
"git.golem15.com/jakub/sm-bm-plugin/models"
)
// Permission codes of the plugin's admin screens.
const (
PermCourses = "jz.bm.access_courses"
PermSchedule = "jz.bm.access_schedule"
PermMaterials = "jz.bm.access_materials"
)
// temporalHooks checks the order of a model's dates before every admin save
// and answers a bad value as a 422 on its field. lagoon.Validate cannot
// compare two fields, so the model checks them itself
// (models.TemporalValidator).
type temporalHooks struct{}
func (temporalHooks) FormBeforeCreate(_ context.Context, model any) error {
return checkTemporal(model)
}
func (temporalHooks) FormBeforeUpdate(_ context.Context, model any) error {
return checkTemporal(model)
}
func checkTemporal(model any) error {
v, ok := model.(models.TemporalValidator)
if !ok {
return nil
}
msgs := v.TemporalErrors()
if len(msgs) == 0 {
return nil
}
details := make(map[string]any, len(msgs))
for field, m := range msgs {
details[field] = m
}
return &cabana.ValidationError{Details: details}
}
// courseRelation is the belongsTo picker of a course_id column.
func courseRelation(foreignKey string) cabana.FieldRelationContract {
return cabana.FieldRelationContract{
Field: "course",
Kind: "belongsTo",
NewRelated: func() any { return &models.Course{} },
ForeignKey: foreignKey,
LabelColumn: "name",
}
}
// meetingRelation is the belongsTo picker of a meeting_id column (zjazd).
func meetingRelation() cabana.FieldRelationContract {
return cabana.FieldRelationContract{
Field: "meeting",
Kind: "belongsTo",
NewRelated: func() any { return &models.Meeting{} },
ForeignKey: "meeting_id",
LabelColumn: "number",
}
}