Files
sm-bm-plugin/models/material.go
Jakub Zych 1046237771 feat: add the study data models and schema migrations
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.
2026-10-02 14:26:08 +02:00

76 lines
2.3 KiB
Go

// Code generated by summer make. DO NOT EDIT.
package models
import (
"time"
"gorm.io/gorm"
)
// MaterialTypes lists the allowed Material.Type values.
var MaterialTypes = []string{"pdf", "notatka", "link", "inne"}
// MaterialFilesField is the system_files field of a material's attachments
// (attachMany "files").
const MaterialFilesField = "files"
// Material is a study material: a note, a link or uploaded files, optionally
// tied to a course. A material without published_at is a draft and is not
// served by the API.
type Material struct {
ID uint `gorm:"column:id;primaryKey"`
CourseID *uint `gorm:"column:course_id"`
Course *Course `gorm:"foreignKey:CourseID"`
Title string `gorm:"column:title"`
Type string `gorm:"column:type"`
Body *string `gorm:"column:body"`
Source *string `gorm:"column:source"`
URL *string `gorm:"column:url"`
PublishedAt *DateTime `gorm:"column:published_at"`
CreatedAt time.Time `gorm:"column:created_at"`
UpdatedAt time.Time `gorm:"column:updated_at"`
Tags []Tag `gorm:"many2many:jz_bm_material_tag;joinForeignKey:material_id;joinReferences:tag_id"`
}
// TableName is the plugin table.
func (Material) TableName() string { return "jz_bm_materials" }
// MorphName is the attachment_type of the material's system_files rows.
func (Material) MorphName() string { return `Jz\Bm\Models\Material` }
// Fillable lists the columns mass assignment may set.
func (Material) Fillable() []string {
return []string{"course_id", "title", "type", "body", "source", "url", "published_at"}
}
// Rules are checked by the admin API on every save.
func (Material) Rules() map[string]string {
return map[string]string{
"title": "required|max:255",
"type": "required|in:pdf,notatka,link,inne",
"source": "nullable|max:500",
"url": "nullable|max:2048",
}
}
// TemporalErrors checks published_at, which lagoon.Validate cannot.
func (m *Material) TemporalErrors() map[string][]string {
e := temporalErrors{}
if m.PublishedAt != nil {
e.dateTime("published_at", *m.PublishedAt, false)
}
return e.result()
}
// BeforeSave stores NULL for empty optional text and an empty publication
// time.
func (m *Material) BeforeSave(*gorm.DB) error {
blank(&m.Body, &m.Source, &m.URL)
if m.PublishedAt != nil && *m.PublishedAt == "" {
m.PublishedAt = nil
}
return nil
}