feat: move user dto outside
This commit is contained in:
@ -4,27 +4,14 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"gitea.local/admin/hspguard/internal/repository"
|
"gitea.local/admin/hspguard/internal/repository"
|
||||||
|
"gitea.local/admin/hspguard/internal/types"
|
||||||
"gitea.local/admin/hspguard/internal/web"
|
"gitea.local/admin/hspguard/internal/web"
|
||||||
"github.com/google/uuid"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type UserDTO struct {
|
func NewUserDTO(row *repository.User) types.UserDTO {
|
||||||
ID uuid.UUID `json:"id"`
|
return types.UserDTO{
|
||||||
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,
|
ID: row.ID,
|
||||||
Email: row.Email,
|
Email: row.Email,
|
||||||
FullName: row.FullName,
|
FullName: row.FullName,
|
||||||
@ -45,15 +32,23 @@ func (h *AdminHandler) GetUsers(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var response []UserDTO
|
type Response struct {
|
||||||
|
Items []types.UserDTO `json:"items"`
|
||||||
|
Count int `json:"count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
var items []types.UserDTO
|
||||||
|
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
response = append(response, NewUserDTO(&user))
|
items = append(items, NewUserDTO(&user))
|
||||||
}
|
}
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
encoder := json.NewEncoder(w)
|
||||||
|
|
||||||
if err := encoder.Encode(&response); err != nil {
|
if err := encoder.Encode(&Response{
|
||||||
|
Items: items,
|
||||||
|
Count: len(items),
|
||||||
|
}); err != nil {
|
||||||
web.Error(w, "failed to send response", http.StatusInternalServerError)
|
web.Error(w, "failed to send response", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -160,16 +160,16 @@ func (h *AuthHandler) getProfile(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
if err := json.NewEncoder(w).Encode(map[string]any{
|
if err := json.NewEncoder(w).Encode(types.UserDTO{
|
||||||
"id": user.ID.String(),
|
ID: user.ID,
|
||||||
"full_name": user.FullName,
|
FullName: user.FullName,
|
||||||
"email": user.Email,
|
Email: user.Email,
|
||||||
"phone_number": user.PhoneNumber,
|
PhoneNumber: user.PhoneNumber,
|
||||||
"isAdmin": user.IsAdmin,
|
IsAdmin: user.IsAdmin,
|
||||||
"last_login": user.LastLogin,
|
LastLogin: user.LastLogin,
|
||||||
"profile_picture": user.ProfilePicture,
|
ProfilePicture: user.ProfilePicture,
|
||||||
"updated_at": user.UpdatedAt,
|
UpdatedAt: user.UpdatedAt,
|
||||||
"created_at": user.CreatedAt,
|
CreatedAt: user.CreatedAt,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
web.Error(w, "failed to encode user profile", http.StatusInternalServerError)
|
web.Error(w, "failed to encode user profile", http.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
19
internal/types/user.go
Normal file
19
internal/types/user.go
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"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"`
|
||||||
|
}
|
Reference in New Issue
Block a user