feat: user_rework (todo -> GetAll)
This commit is contained in:
+39
-3
@@ -3,7 +3,6 @@ package main
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
@@ -12,8 +11,39 @@ import (
|
||||
)
|
||||
|
||||
type userKey string
|
||||
type roleKey string
|
||||
|
||||
const userContextKey userKey = "user"
|
||||
const roleContextKey roleKey = "role"
|
||||
|
||||
// GetUser godoc
|
||||
//
|
||||
// @Summary Fetches all users
|
||||
// @Description Fetches all users
|
||||
// @Tags admin
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} store.User
|
||||
// @Failure 400 {object} error
|
||||
// @Failure 404 {object} error
|
||||
// @Failure 500 {object} error
|
||||
// @Security ApiKeyAuth
|
||||
// @Router /admin/users [get]
|
||||
func (api *api) getUsersHandler(w http.ResponseWriter, r *http.Request) {
|
||||
role := getRoleFromContext(r)
|
||||
isAdmin := false
|
||||
if role.Level == 3 {
|
||||
isAdmin = true
|
||||
}
|
||||
|
||||
users, err := api.store.Users.GetAll(r.Context(), isAdmin)
|
||||
if err != nil {
|
||||
api.internalServerError(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
api.jsonResponse(w, http.StatusOK, users)
|
||||
}
|
||||
|
||||
// GetUser godoc
|
||||
//
|
||||
@@ -78,7 +108,6 @@ type updateUserPayload struct {
|
||||
// @Router /users/{id} [patch]
|
||||
func (api *api) updateUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
user := getUserFromContext(r)
|
||||
log.Println(user)
|
||||
|
||||
var payload updateUserPayload
|
||||
if err := readJSON(w, r, &payload); err != nil {
|
||||
@@ -103,7 +132,9 @@ func (api *api) updateUser(ctx context.Context, user *store.User) error {
|
||||
return err
|
||||
}
|
||||
|
||||
api.cacheStore.Users.Delete(ctx, user.ID)
|
||||
if api.config.redisCfg.enabled {
|
||||
api.cacheStore.Users.Delete(ctx, user.ID)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -300,3 +331,8 @@ func getUserFromContext(r *http.Request) *store.User {
|
||||
user, _ := r.Context().Value(userContextKey).(*store.User)
|
||||
return user
|
||||
}
|
||||
|
||||
func getRoleFromContext(r *http.Request) *store.Role {
|
||||
role, _ := r.Context().Value(roleContextKey).(*store.Role)
|
||||
return role
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user