feat: add data models and API structures
- Add User model with authentication fields and validation - Add Session model for managing user sessions - Add API response structures for consistent API responses - Include GORM tags for database mapping and validation
This commit is contained in:
31
models/api.go
Normal file
31
models/api.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ApiKey struct {
|
||||||
|
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
|
||||||
|
Key string `json:"key" gorm:"not null;unique"`
|
||||||
|
UserID *uuid.UUID `json:"user_id"`
|
||||||
|
Revoked bool `json:"revoked" gorm:"not null;default:false"`
|
||||||
|
ExpiresAt *time.Time `json:"expires_at"`
|
||||||
|
CreatedAt time.Time `json:"created_at" gorm:"not null;default:now()"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" gorm:"not null;default:now()"`
|
||||||
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index;default:null"`
|
||||||
|
|
||||||
|
// Relationships
|
||||||
|
User *User `json:"user" gorm:"foreignKey:user_id;references:id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ApiKey) TableName() string {
|
||||||
|
return "api_keys"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *ApiKey) BeforeCreate(tx *gorm.DB) (err error) {
|
||||||
|
a.ID = uuid.New()
|
||||||
|
return
|
||||||
|
}
|
31
models/session.go
Normal file
31
models/session.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Session struct {
|
||||||
|
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
|
||||||
|
UserID uuid.UUID `json:"user_id" gorm:"not null"`
|
||||||
|
Revoked bool `json:"revoked" gorm:"not null;default:false"`
|
||||||
|
LoggedOut bool `json:"logged_out" gorm:"not null;default:false"`
|
||||||
|
ExpiresAt time.Time `json:"expires_at" gorm:"not null"`
|
||||||
|
CreatedAt time.Time `json:"created_at" gorm:"not null;default:now()"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" gorm:"not null;default:now()"`
|
||||||
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index;default:null"`
|
||||||
|
|
||||||
|
// Relationships
|
||||||
|
User User `json:"user" gorm:"foreignKey:user_id;references:id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) TableName() string {
|
||||||
|
return "sessions"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Session) BeforeCreate(tx *gorm.DB) (err error) {
|
||||||
|
s.ID = uuid.New()
|
||||||
|
return
|
||||||
|
}
|
36
models/user.go
Normal file
36
models/user.go
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package models
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type User struct {
|
||||||
|
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid;default:gen_random_uuid()"`
|
||||||
|
FirstName string `json:"first_name" gorm:"not null"`
|
||||||
|
LastName string `json:"last_name" gorm:"not null"`
|
||||||
|
Username string `json:"username" gorm:"not null;unique"`
|
||||||
|
Password string `json:"password" gorm:"not null"`
|
||||||
|
Email string `json:"email" gorm:"not null;unique"`
|
||||||
|
Enabled bool `json:"enabled" gorm:"not null;default:true"`
|
||||||
|
Verified bool `json:"verified" gorm:"not null;default:false"` // TODO: remove this field
|
||||||
|
SuperAdmin bool `json:"super_admin" gorm:"not null;default:false"`
|
||||||
|
CreatedAt time.Time `json:"created_at" gorm:"not null;default:now()"`
|
||||||
|
UpdatedAt time.Time `json:"updated_at" gorm:"not null;default:now()"`
|
||||||
|
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index;default:null"`
|
||||||
|
|
||||||
|
// Relationships
|
||||||
|
ApiKeys []ApiKey `json:"api_keys" gorm:"foreignKey:user_id;references:id"`
|
||||||
|
Sessions []Session `json:"sessions" gorm:"foreignKey:user_id;references:id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) TableName() string {
|
||||||
|
return "users"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (u *User) BeforeCreate(tx *gorm.DB) (err error) {
|
||||||
|
u.ID = uuid.New()
|
||||||
|
return
|
||||||
|
}
|
Reference in New Issue
Block a user