feat: private checklists for zjazd preparation

Per-user checklists with items that may link a course, reading, deadline,
material or own presentation deck. An item with a reading shares the
reading's read mark. Student API routes with token twins, MCP tools,
migrations, tests and README.
This commit is contained in:
Jakub Zych
2026-10-30 12:41:04 +01:00
parent 643f8cbf7c
commit d7cd5baedf
15 changed files with 1262 additions and 21 deletions

View File

@@ -175,18 +175,31 @@ func (rec Record[T, PT]) find(w http.ResponseWriter, r *http.Request, db *gorm.D
// save decodes the body onto row, validates the result and writes it.
func (rec Record[T, PT]) save(w http.ResponseWriter, r *http.Request, h *Handlers, db *gorm.DB, row *T, created bool) {
if _, ok := h.fillRecord(w, r, db, row, PT(row), rec.References, nil); !ok {
return
}
writeSaved(w, r, db, row, created)
}
// fillRecord decodes the JSON body onto row (only model.Fillable() keys),
// then checks model.Rules() and TemporalErrors() against the row as it
// would be saved, that every foreign key in references names an existing
// row, and extra (when given) for checks of its own. It answers 422 or 500
// itself and returns false when the row must not be saved; else it returns
// the row as a column map.
func (h *Handlers) fillRecord(w http.ResponseWriter, r *http.Request, db *gorm.DB, row any, model recordModel,
references map[string]string, extra func(check map[string]any, errs map[string][]string) error) (map[string]any, bool) {
raw, ok := body(w, r)
if !ok {
return
return nil, false
}
var in map[string]any
dec := json.NewDecoder(bytes.NewReader(raw))
dec.UseNumber()
if err := dec.Decode(&in); err != nil || in == nil {
writeInvalid(w, "The body must be a JSON object of columns.")
return
return nil, false
}
model := PT(row)
fillable := model.Fillable()
errs := map[string][]string{}
for key := range in {
@@ -196,7 +209,7 @@ func (rec Record[T, PT]) save(w http.ResponseWriter, r *http.Request, h *Handler
}
if len(errs) > 0 {
writeValidation(w, errs)
return
return nil, false
}
values, fieldErrs := normalizeValues(row, in)
for k, v := range fieldErrs {
@@ -208,12 +221,12 @@ func (rec Record[T, PT]) save(w http.ResponseWriter, r *http.Request, h *Handler
errs[typeErr.Key] = []string{"The " + typeErr.Key + " has the wrong type."}
} else if err != nil {
writeOpaque500(w)
return
return nil, false
}
}
if len(errs) > 0 {
writeValidation(w, errs)
return
return nil, false
}
// The rules run on the row as it would be saved, so a PATCH of one
// column still meets the required ones.
@@ -221,17 +234,17 @@ func (rec Record[T, PT]) save(w http.ResponseWriter, r *http.Request, h *Handler
verrs, err := lagoon.Validate(r.Context(), db, row, model.Rules(), check, h.translator())
if err != nil {
writeOpaque500(w)
return
return nil, false
}
for k, v := range verrs {
errs[k] = v
}
if tc, ok := any(row).(temporalChecker); ok {
if tc, ok := row.(temporalChecker); ok {
for k, v := range tc.TemporalErrors() {
errs[k] = append(errs[k], v...)
}
}
for col, table := range rec.References {
for col, table := range references {
id, ok := check[col].(uint)
if !ok || id == 0 {
continue
@@ -239,17 +252,29 @@ func (rec Record[T, PT]) save(w http.ResponseWriter, r *http.Request, h *Handler
var n int64
if err := db.Table(table).Where("id = ?", id).Count(&n).Error; err != nil {
writeOpaque500(w)
return
return nil, false
}
if n == 0 {
errs[col] = append(errs[col], "The selected "+col+" is invalid.")
}
}
if extra != nil {
if err := extra(check, errs); err != nil {
writeOpaque500(w)
return nil, false
}
}
if len(errs) > 0 {
writeValidation(w, errs)
return
return nil, false
}
err = lagoon.Transaction(r.Context(), db, func(_ context.Context, tx *gorm.DB) error {
return check, true
}
// writeSaved saves row in a transaction and answers it as a column map, 201
// when created.
func writeSaved(w http.ResponseWriter, r *http.Request, db *gorm.DB, row any, created bool) {
err := lagoon.Transaction(r.Context(), db, func(_ context.Context, tx *gorm.DB) error {
return tx.Save(row).Error
})
if err != nil {