105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
// internal/models/user.go
|
|
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// User represents a user in the system
|
|
type User struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
Name string `gorm:"type:varchar(100);not null" json:"name"`
|
|
Email string `gorm:"type:varchar(100);uniqueIndex;not null" json:"email"`
|
|
Password string `gorm:"type:varchar(255);not null" json:"-"` // Hide password in JSON
|
|
NRP string `gorm:"type:varchar(20);uniqueIndex" json:"nrp"`
|
|
Phone string `gorm:"type:varchar(20)" json:"phone"`
|
|
RoleID uint `gorm:"not null;default:3" json:"role_id"` // Default to user role
|
|
Role Role `gorm:"foreignKey:RoleID" json:"role,omitempty"`
|
|
Status string `gorm:"type:varchar(20);default:'active'" json:"status"` // active, blocked
|
|
CreatedAt time.Time `json:"created_at"`
|
|
UpdatedAt time.Time `json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// Relationships
|
|
Items []Item `gorm:"foreignKey:ReporterID" json:"items,omitempty"`
|
|
LostItems []LostItem `gorm:"foreignKey:UserID" json:"lost_items,omitempty"`
|
|
Claims []Claim `gorm:"foreignKey:UserID" json:"claims,omitempty"`
|
|
}
|
|
|
|
// TableName specifies the table name for User model
|
|
func (User) TableName() string {
|
|
return "users"
|
|
}
|
|
|
|
// BeforeCreate hook to validate user data
|
|
func (u *User) BeforeCreate(tx *gorm.DB) error {
|
|
// Set default role if not specified
|
|
if u.RoleID == 0 {
|
|
u.RoleID = 3 // Default to user role
|
|
}
|
|
|
|
// Set default status
|
|
if u.Status == "" {
|
|
u.Status = "active"
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// IsAdmin checks if user is admin
|
|
func (u *User) IsAdmin() bool {
|
|
return u.Role.Name == "admin"
|
|
}
|
|
|
|
// IsManager checks if user is manager
|
|
func (u *User) IsManager() bool {
|
|
return u.Role.Name == "manager"
|
|
}
|
|
|
|
// IsUser checks if user is regular user
|
|
func (u *User) IsUser() bool {
|
|
return u.Role.Name == "user"
|
|
}
|
|
|
|
// IsActive checks if user is active
|
|
func (u *User) IsActive() bool {
|
|
return u.Status == "active"
|
|
}
|
|
|
|
// IsBlocked checks if user is blocked
|
|
func (u *User) IsBlocked() bool {
|
|
return u.Status == "blocked"
|
|
}
|
|
|
|
// UserResponse represents user data for API responses (without sensitive info)
|
|
type UserResponse struct {
|
|
ID uint `json:"id"`
|
|
Name string `json:"name"`
|
|
Email string `json:"email"`
|
|
NRP string `json:"nrp"`
|
|
Phone string `json:"phone"`
|
|
Role string `json:"role"`
|
|
Status string `json:"status"`
|
|
CreatedAt time.Time `json:"created_at"`
|
|
}
|
|
|
|
// ToResponse converts User to UserResponse
|
|
func (u *User) ToResponse() UserResponse {
|
|
roleName := ""
|
|
if u.Role.ID != 0 {
|
|
roleName = u.Role.Name
|
|
}
|
|
|
|
return UserResponse{
|
|
ID: u.ID,
|
|
Name: u.Name,
|
|
Email: u.Email,
|
|
NRP: u.NRP,
|
|
Phone: u.Phone,
|
|
Role: roleName,
|
|
Status: u.Status,
|
|
CreatedAt: u.CreatedAt,
|
|
}
|
|
} |