package view import ( "github.com/valyala/fasthttp" "git.sr.ht/~gabrielgio/img" "git.sr.ht/~gabrielgio/img/pkg/components/settings" "git.sr.ht/~gabrielgio/img/pkg/ext" ) type SettingsView struct { // there is not need to create a controller for this repository settings.Repository } func NewSettingsView(respository settings.Repository) *SettingsView { return &SettingsView{ repository: respository, } } func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error { s, err := self.repository.Load(ctx) if err != nil { return err } return img.Render(ctx, "settings.html", &img.HTMLView[*settings.Settings]{ Title: "Settings", Data: s, }) } func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error { var ( showMode = string(ctx.FormValue("showMode")) == "on" showOwner = string(ctx.FormValue("showOwner")) == "on" ) err := self.repository.Save(ctx, &settings.Settings{ ShowMode: showMode, ShowOwner: showOwner, }) if err != nil { return err } return self.Index(ctx) } func (self *SettingsView) SetMyselfIn(r *ext.Router) { r.GET("/settings/", self.Index) r.POST("/settings/", self.Save) }