lens @ 13fd911873a7df311544b1ec916c98ee05c91bbc

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