feat: get single user endpoint
This commit is contained in:
@ -33,5 +33,6 @@ func (h *AdminHandler) RegisterRoutes(router chi.Router) {
|
||||
r.Patch("/api-services/toggle/{id}", h.ToggleApiService)
|
||||
|
||||
r.Get("/users", h.GetUsers)
|
||||
r.Get("/users/{id}", h.GetUser)
|
||||
})
|
||||
}
|
||||
|
@ -8,6 +8,8 @@ import (
|
||||
"gitea.local/admin/hspguard/internal/repository"
|
||||
"gitea.local/admin/hspguard/internal/types"
|
||||
"gitea.local/admin/hspguard/internal/web"
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func NewUserDTO(row *repository.User) types.UserDTO {
|
||||
@ -52,3 +54,24 @@ func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
||||
web.Error(w, "failed to send response", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AdminHandler) GetUser(w http.ResponseWriter, r *http.Request) {
|
||||
userId := chi.URLParam(r, "id")
|
||||
parsed, err := uuid.Parse(userId)
|
||||
if err != nil {
|
||||
web.Error(w, "user id provided is not a valid uuid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.repo.FindUserId(r.Context(), parsed)
|
||||
if err != nil {
|
||||
web.Error(w, "user with provided id not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
|
||||
if err := encoder.Encode(NewUserDTO(&user)); err != nil {
|
||||
web.Error(w, "failed to encode user dto", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user