Skip to content

Commit

Permalink
website/admin: add users handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Wessie committed Apr 4, 2024
1 parent 4ed43ff commit fd00b2c
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 8 deletions.
3 changes: 2 additions & 1 deletion website/admin/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (s *State) GetHome(w http.ResponseWriter, r *http.Request) {

err := s.TemplateExecutor.Execute(w, r, input)
if err != nil {
s.errorHandler(w, r, err)
s.errorHandler(w, r, err, "input creation failure")
return
}
}
2 changes: 0 additions & 2 deletions website/admin/profiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (
"github.com/gorilla/schema"
)

const bcryptCost = 14

var profileDecoder = schema.NewDecoder()

// ProfileForm defines the form we use for the profile page
Expand Down
6 changes: 5 additions & 1 deletion website/admin/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/R-a-dio/valkyrie/util/secret"
vmiddleware "github.com/R-a-dio/valkyrie/website/middleware"
"github.com/R-a-dio/valkyrie/website/shared"
"github.com/rs/zerolog/hlog"
"github.com/spf13/afero"

"github.com/alexedwards/scs/v2"
Expand Down Expand Up @@ -85,6 +86,8 @@ func Route(ctx context.Context, s State) func(chi.Router) {
vmiddleware.RequirePermission(radio.PermDatabaseView, s.GetSongs))
r.Post("/songs",
vmiddleware.RequirePermission(radio.PermDatabaseEdit, s.PostSongs))
r.Get("/users",
vmiddleware.RequirePermission(radio.PermAdmin, s.GetUsersList))

// proxy to the grafana host
grafana, _ := url.Parse("http://localhost:3000")
Expand All @@ -100,7 +103,8 @@ func (s *State) PostStreamerStop(w http.ResponseWriter, r *http.Request) {
s.Conf().Streamer.Client().Stop(r.Context(), false)
}

func (s *State) errorHandler(w http.ResponseWriter, r *http.Request, err error) {
func (s *State) errorHandler(w http.ResponseWriter, r *http.Request, err error, msg string) {
// TODO: implement this better
hlog.FromRequest(r).Error().Err(err).Msg(msg)
http.Error(w, err.Error(), http.StatusInternalServerError)
}
2 changes: 1 addition & 1 deletion website/admin/songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (s *State) GetSongs(w http.ResponseWriter, r *http.Request) {
func (s *State) PostSongs(w http.ResponseWriter, r *http.Request) {
form, err := s.postSongs(w, r)
if err != nil {
s.errorHandler(w, r, err)
s.errorHandler(w, r, err, "")
return
}

Expand Down
60 changes: 60 additions & 0 deletions website/admin/users.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package admin

import (
"cmp"
"net/http"
"slices"

radio "github.com/R-a-dio/valkyrie"
"github.com/R-a-dio/valkyrie/errors"
"github.com/R-a-dio/valkyrie/website/middleware"
)

type UsersInput struct {
middleware.Input

Users []radio.User
}

func (UsersInput) TemplateBundle() string {
return "users"
}

type UsersForm struct {
radio.User
}

func NewUsersInput(us radio.UserStorage, r *http.Request) (*UsersInput, error) {
const op errors.Op = "website/admin.NewUsersInput"

// get all the users
users, err := us.All()
if err != nil {
return nil, errors.E(op, err)
}
// sort users by their id
slices.SortFunc(users, func(a, b radio.User) int {
return cmp.Compare(a.ID, b.ID)
})
// construct the input
input := &UsersInput{
Input: middleware.InputFromRequest(r),
Users: users,
}

return input, nil
}

func (s State) GetUsersList(w http.ResponseWriter, r *http.Request) {
input, err := NewUsersInput(s.Storage.User(r.Context()), r)
if err != nil {
s.errorHandler(w, r, err, "input creation failure")
return
}

err = s.TemplateExecutor.Execute(w, r, input)
if err != nil {
s.errorHandler(w, r, err, "template failure")
return
}
}
6 changes: 3 additions & 3 deletions website/api/v1/song.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (a *API) GetSong(w http.ResponseWriter, r *http.Request) {

tid, err := radio.ParseTrackID(query.Get("id"))
if err != nil {
hlog.FromRequest(r).Error().Err(err)
hlog.FromRequest(r).Error().Err(err).Msg("")
http.Error(w, "invalid or missing id", http.StatusBadRequest)
return
}
Expand All @@ -25,7 +25,7 @@ func (a *API) GetSong(w http.ResponseWriter, r *http.Request) {

song, err := a.storage.Track(r.Context()).Get(tid)
if err != nil {
hlog.FromRequest(r).Error().Err(err)
hlog.FromRequest(r).Error().Err(err).Msg("")
http.Error(w, "unknown id", http.StatusNotFound)
return
}
Expand All @@ -44,7 +44,7 @@ func (a *API) GetSong(w http.ResponseWriter, r *http.Request) {
if errors.IsE(err, os.ErrNotExist) {
status = http.StatusNotFound
}
hlog.FromRequest(r).Error().Err(err)
hlog.FromRequest(r).Error().Err(err).Msg("")
http.Error(w, http.StatusText(status), status)
return
}
Expand Down

0 comments on commit fd00b2c

Please sign in to comment.