feat: get users endpoint

This commit is contained in:
2025-06-04 12:33:06 +02:00
parent 92e9b87227
commit c27d837ab0

59
internal/admin/users.go Normal file
View File

@ -0,0 +1,59 @@
package admin
import (
"encoding/json"
"log"
"net/http"
"time"
"gitea.local/admin/hspguard/internal/repository"
"gitea.local/admin/hspguard/internal/web"
"github.com/google/uuid"
)
type UserDTO struct {
ID uuid.UUID `json:"id"`
Email string `json:"email"`
FullName string `json:"full_name"`
IsAdmin bool `json:"is_admin"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
LastLogin *time.Time `json:"last_login"`
PhoneNumber *string `json:"phone_number"`
ProfilePicture *string `json:"profile_picture"`
}
func NewUserDTO(row *repository.User) UserDTO {
return UserDTO{
ID: row.ID,
Email: row.Email,
FullName: row.FullName,
IsAdmin: row.IsAdmin,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
LastLogin: row.LastLogin,
PhoneNumber: row.PhoneNumber,
ProfilePicture: row.ProfilePicture,
}
}
func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
users, err := h.repo.FindAllUsers(r.Context())
if err != nil {
log.Println("ERR: Failed to query users from db:", err)
web.Error(w, "failed to get all users", http.StatusInternalServerError)
return
}
var response []UserDTO
for _, user := range users {
response = append(response, NewUserDTO(&user))
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(&response); err != nil {
web.Error(w, "failed to send response", http.StatusInternalServerError)
}
}