first commit

This commit is contained in:
Beyhan Oğur
2026-04-26 21:37:58 +03:00
commit 8b1fbdee99
104 changed files with 23398 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// CorsWhitelist - CORS için izin verilen origin'ler
type CorsWhitelist struct {
ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id"`
Origin string `gorm:"type:varchar(255);uniqueIndex;not null" json:"origin"`
Description string `gorm:"type:text" json:"description"`
IsActive bool `gorm:"default:true" json:"is_active"`
CreatedBy string `gorm:"type:varchar(255)" json:"created_by,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// BeforeCreate - UUID otomatik oluşturma
func (c *CorsWhitelist) BeforeCreate(tx *gorm.DB) error {
if c.ID == uuid.Nil {
c.ID = uuid.New()
}
return nil
}
// CorsBlacklist - CORS için yasaklanan origin'ler
type CorsBlacklist struct {
ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id"`
Origin string `gorm:"type:varchar(255);uniqueIndex;not null" json:"origin"`
Reason string `gorm:"type:text" json:"reason"`
IsActive bool `gorm:"default:true" json:"is_active"`
CreatedBy string `gorm:"type:varchar(255)" json:"created_by,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// BeforeCreate - UUID otomatik oluşturma
func (c *CorsBlacklist) BeforeCreate(tx *gorm.DB) error {
if c.ID == uuid.Nil {
c.ID = uuid.New()
}
return nil
}
// RateLimitSetting - Rate limit ayarları
type RateLimitSetting struct {
ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id"`
Name string `gorm:"type:varchar(100);uniqueIndex;not null" json:"name"` // e.g., "login", "register", "api"
Description string `gorm:"type:text" json:"description"`
MaxRequests int64 `gorm:"not null" json:"max_requests"` // Max istek sayısı
WindowSeconds int `gorm:"not null" json:"window_seconds"` // Zaman penceresi (saniye)
IsActive bool `gorm:"default:true" json:"is_active"`
UpdatedBy string `gorm:"type:varchar(255)" json:"updated_by,omitempty"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// BeforeCreate - UUID otomatik oluşturma
func (r *RateLimitSetting) BeforeCreate(tx *gorm.DB) error {
if r.ID == uuid.Nil {
r.ID = uuid.New()
}
return nil
}

14
internal/models/role.go Normal file
View File

@@ -0,0 +1,14 @@
package models
type Role struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"uniqueIndex;not null" json:"name"` // admin, user
Description string `json:"description"`
Permissions []Permission `gorm:"many2many:role_permissions;" json:"permissions"`
}
type Permission struct {
ID uint `gorm:"primaryKey" json:"id"`
Name string `gorm:"uniqueIndex;not null" json:"name"` // user:read, user:write
Description string `json:"description"`
}

52
internal/models/user.go Normal file
View File

@@ -0,0 +1,52 @@
package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
// User model structure
type User struct {
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
UserName string `json:"username"`
Email string `gorm:"uniqueIndex;not null" json:"email"`
Password string `json:"-"` // Password shouldn't be returned in JSON
Avatar string `gorm:"type:varchar(500)" json:"avatar,omitempty"` // Avatar URL from OAuth or uploaded
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
// Email verification: only required for email/password registration; OAuth users are treated as verified
// Changed to *bool to handle false values correctly with GORM defaults
EmailVerified *bool `gorm:"default:false" json:"email_verified"` // default false for email/password registration
EmailVerifyToken string `gorm:"index" json:"-"`
EmailVerifiedAt *time.Time `json:"email_verified_at,omitempty"`
SocialAccounts []SocialAccount `gorm:"foreignKey:UserID" json:"social_accounts,omitempty"`
Roles []Role `gorm:"many2many:user_roles;" json:"roles,omitempty"`
}
// Helper to safely get EmailVerified status
func (u *User) IsEmailVerified() bool {
if u.EmailVerified == nil {
return false
}
return *u.EmailVerified
}
// SocialAccount model structure
type SocialAccount struct {
ID uuid.UUID `gorm:"type:uuid;default:gen_random_uuid();primaryKey" json:"id"`
UserID uuid.UUID `gorm:"not null" json:"user_id"`
Provider string `gorm:"not null" json:"provider"` // google, github
ProviderID string `gorm:"not null" json:"provider_id"`
Email string `json:"email"`
Name string `json:"name,omitempty"` // Full name from provider
AvatarURL string `json:"avatar_url,omitempty"` // Avatar URL from provider
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// Hooks can be added here if needed