diff --git a/radio.go b/radio.go index 7c3d7c57..944d5ac7 100644 --- a/radio.go +++ b/radio.go @@ -1076,15 +1076,16 @@ func (day ScheduleDay) String() string { type ScheduleEntry struct { ID ScheduleID // Weekday is the day this entry is for - Weekday ScheduleDay + //Weekday ScheduleDay + Weekday int // Text is the actual body of the entry Text string // Owner is who "owns" this day for streaming rights - Owner *User + User *User // UpdatedAt is when this was updated UpdatedAt time.Time // UpdatedBy is who updated this - UpdatedBy User + // UpdatedBy User // Notification indicates if we should notify users of this entry Notification bool } diff --git a/storage/mariadb/schedule.go b/storage/mariadb/schedule.go index dd1d4386..c9656332 100644 --- a/storage/mariadb/schedule.go +++ b/storage/mariadb/schedule.go @@ -1,13 +1,40 @@ package mariadb -import radio "github.com/R-a-dio/valkyrie" +import ( + radio "github.com/R-a-dio/valkyrie" + "github.com/R-a-dio/valkyrie/errors" + "github.com/jmoiron/sqlx" +) type ScheduleStorage struct { handle handle } func (ss ScheduleStorage) Latest() ([]radio.ScheduleEntry, error) { - return nil, nil + const op errors.Op = "mariadb/ScheduleStorage.Latest" + handle, deferFn := ss.handle.span(op) + defer deferFn() + + var query = ` + SELECT + schedule.id AS id, + schedule.weekday AS weekday, + schedule.text AS text, + schedule.owner AS 'user.id', + schedule.updated_at AS updated_at, + schedule.notification AS notification + FROM + schedule + ` + + var schedule []radio.ScheduleEntry + + err := sqlx.Select(handle, &schedule, query) + if err != nil { + return nil, errors.E(op, err) + } + + return schedule, nil } func (ss ScheduleStorage) Update(entry radio.ScheduleEntry) error { diff --git a/website/public/schedule.go b/website/public/schedule.go index bd73f4cd..b6624ecd 100644 --- a/website/public/schedule.go +++ b/website/public/schedule.go @@ -3,18 +3,26 @@ package public import ( "net/http" - "github.com/R-a-dio/valkyrie/errors" + radio "github.com/R-a-dio/valkyrie" "github.com/R-a-dio/valkyrie/website/middleware" ) type ScheduleInput struct { middleware.Input + + Schedule []radio.ScheduleEntry } -func NewScheduleInput(r *http.Request) ScheduleInput { - return ScheduleInput{ - Input: middleware.InputFromRequest(r), +func NewScheduleInput(ss radio.ScheduleStorageService, r *http.Request) (*ScheduleInput, error) { + schedule, err := ss.Schedule(r.Context()).Latest() + if err != nil { + return nil, err } + + return &ScheduleInput{ + Input: middleware.InputFromRequest(r), + Schedule: schedule, + }, nil } func (ScheduleInput) TemplateBundle() string { @@ -30,13 +38,11 @@ func (s State) GetSchedule(w http.ResponseWriter, r *http.Request) { } func (s State) getSchedule(w http.ResponseWriter, r *http.Request) error { - const op errors.Op = "website/public.getSchedule" - - input := NewScheduleInput(r) - - err := s.Templates.Execute(w, r, input) + input, err := NewScheduleInput(s.Storage, r) if err != nil { - return errors.E(op, err) + s.errorHandler(w, r, err) + return err } - return nil + + return s.Templates.Execute(w, r, input) }