lens @ 2a986064a2657c9091a31a941ce0d004191b8453

  1package view
  2
  3import (
  4	"strconv"
  5
  6	"github.com/valyala/fasthttp"
  7
  8	"git.sr.ht/~gabrielgio/img/pkg/database/repository"
  9	"git.sr.ht/~gabrielgio/img/pkg/ext"
 10	"git.sr.ht/~gabrielgio/img/pkg/service"
 11	"git.sr.ht/~gabrielgio/img/templates"
 12)
 13
 14type (
 15	SettingsView struct {
 16		// there is not need to create a controller for this
 17		settingsRepository repository.SettingsRepository
 18		userController     *service.AuthController
 19	}
 20)
 21
 22func NewSettingsView(
 23	settingsRespository repository.SettingsRepository,
 24	userController *service.AuthController,
 25) *SettingsView {
 26	return &SettingsView{
 27		settingsRepository: settingsRespository,
 28		userController:     userController,
 29	}
 30}
 31
 32func (self *SettingsView) Index(ctx *fasthttp.RequestCtx) error {
 33	s, err := self.settingsRepository.Load(ctx)
 34	if err != nil {
 35		return err
 36	}
 37
 38	users, err := self.userController.List(ctx)
 39	if err != nil {
 40		return err
 41	}
 42
 43	templates.WritePageTemplate(ctx, &templates.SettingsPage{
 44		Settings: s,
 45		Users:    users,
 46	})
 47
 48	return nil
 49}
 50
 51func (self *SettingsView) User(ctx *fasthttp.RequestCtx) error {
 52	id := string(ctx.FormValue("userId"))
 53	idValue, err := ParseUint(id)
 54	if err != nil {
 55		return err
 56	}
 57
 58	if idValue == nil {
 59		templates.WritePageTemplate(ctx, &templates.UserPage{})
 60	} else {
 61		user, err := self.userController.Get(ctx, *idValue)
 62		if err != nil {
 63			return err
 64		}
 65
 66		templates.WritePageTemplate(ctx, &templates.UserPage{
 67			ID:       idValue,
 68			Username: user.Username,
 69			Path:     user.Path,
 70			IsAdmin:  user.IsAdmin,
 71		})
 72	}
 73
 74	return nil
 75}
 76
 77func (self *SettingsView) UpsertUser(ctx *fasthttp.RequestCtx) error {
 78	var (
 79		username = string(ctx.FormValue("username"))
 80		password = ctx.FormValue("password")
 81		path     = string(ctx.FormValue("path"))
 82		isAdmin  = string(ctx.FormValue("isAdmin")) == "on"
 83		id       = string(ctx.FormValue("userId"))
 84	)
 85
 86	idValue, err := ParseUint(id)
 87	if err != nil {
 88		return err
 89	}
 90
 91	err = self.userController.Upsert(ctx, idValue, username, "", password, isAdmin, path)
 92	if err != nil {
 93		return err
 94	}
 95
 96	ctx.Redirect("/settings", 307)
 97	return nil
 98}
 99
100func (self *SettingsView) Delete(ctx *fasthttp.RequestCtx) error {
101	var (
102		id = string(ctx.FormValue("userId"))
103	)
104
105	idValue, err := ParseUint(id)
106	if err != nil {
107		return err
108	}
109
110	if idValue != nil {
111		err = self.userController.Delete(ctx, *idValue)
112		if err != nil {
113			return err
114		}
115	}
116
117	ctx.Redirect("/settings", 307)
118	return nil
119}
120
121func (self *SettingsView) Save(ctx *fasthttp.RequestCtx) error {
122	var (
123		showMode             = string(ctx.FormValue("showMode")) == "on"
124		showOwner            = string(ctx.FormValue("showOwner")) == "on"
125		preloadVideoMetadata = string(ctx.FormValue("preloadVideoMetadata")) == "on"
126	)
127
128	err := self.settingsRepository.Save(ctx, &repository.Settings{
129		ShowMode:             showMode,
130		ShowOwner:            showOwner,
131		PreloadVideoMetadata: preloadVideoMetadata,
132	})
133	if err != nil {
134		return err
135	}
136
137	return self.Index(ctx)
138}
139
140func (self *SettingsView) SetMyselfIn(r *ext.Router) {
141	r.GET("/settings/", self.Index)
142	r.POST("/settings/", self.Save)
143
144	r.GET("/users/", self.User)
145	r.GET("/users/delete", self.Delete)
146	r.POST("/users/", self.UpsertUser)
147}
148
149func ParseUint(id string) (*uint, error) {
150	var idValue *uint
151	if id != "" {
152		v, err := strconv.Atoi(id)
153		if err != nil {
154			return nil, err
155		}
156
157		u := uint(v)
158		idValue = &u
159	}
160	return idValue, nil
161}