66 lines
3.0 KiB
Go
66 lines
3.0 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// ModelUser represents a user in the system
|
|
type ModelUser struct {
|
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement;type:bigint"`
|
|
Username string `gorm:"column:username;type:varchar(100);not null;uniqueIndex:idx_username"`
|
|
Email string `gorm:"column:email;type:varchar(255);not null;uniqueIndex"`
|
|
Password string `gorm:"column:password;type:varchar(255);not null"`
|
|
FirstName *string `gorm:"column:first_name;type:varchar(100)"`
|
|
LastName *string `gorm:"column:last_name;type:varchar(100)"`
|
|
Bio *string `gorm:"column:bio;type:text"`
|
|
IsActive bool `gorm:"column:is_active;type:boolean;default:true"`
|
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;default:now()"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;default:now()"`
|
|
|
|
Posts []*ModelPost `gorm:"foreignKey:UserID;association_foreignkey:ID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE"`
|
|
Comments []*ModelComment `gorm:"foreignKey:UserID;association_foreignkey:ID;constraint:OnDelete:SET NULL"`
|
|
}
|
|
|
|
func (ModelUser) TableName() string {
|
|
return "users"
|
|
}
|
|
|
|
// ModelPost represents a blog post
|
|
type ModelPost struct {
|
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement;type:bigint"`
|
|
UserID int64 `gorm:"column:user_id;type:bigint;not null;index:idx_user_id"`
|
|
Title string `gorm:"column:title;type:varchar(255);not null"`
|
|
Slug string `gorm:"column:slug;type:varchar(255);not null;uniqueIndex:idx_slug"`
|
|
Content string `gorm:"column:content;type:text;not null"`
|
|
Excerpt *string `gorm:"column:excerpt;type:text"`
|
|
Published bool `gorm:"column:published;type:boolean;default:false"`
|
|
ViewCount int64 `gorm:"column:view_count;type:bigint;default:0"`
|
|
PublishedAt *time.Time `gorm:"column:published_at;type:timestamp"`
|
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;default:now()"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;default:now()"`
|
|
|
|
User *ModelUser `gorm:"foreignKey:UserID;references:ID;constraint:OnDelete:CASCADE,OnUpdate:CASCADE"`
|
|
Comments []*ModelComment `gorm:"foreignKey:PostID;association_foreignkey:ID;constraint:OnDelete:CASCADE"`
|
|
}
|
|
|
|
func (ModelPost) TableName() string {
|
|
return "posts"
|
|
}
|
|
|
|
// ModelComment represents a comment on a post
|
|
type ModelComment struct {
|
|
ID int64 `gorm:"column:id;primaryKey;autoIncrement;type:bigint"`
|
|
PostID int64 `gorm:"column:post_id;type:bigint;not null;index:idx_post_id"`
|
|
UserID *int64 `gorm:"column:user_id;type:bigint;index:idx_user_id"`
|
|
Content string `gorm:"column:content;type:text;not null"`
|
|
CreatedAt time.Time `gorm:"column:created_at;type:timestamp;default:now()"`
|
|
UpdatedAt time.Time `gorm:"column:updated_at;type:timestamp;default:now()"`
|
|
|
|
Post *ModelPost `gorm:"foreignKey:PostID;references:ID;constraint:OnDelete:CASCADE"`
|
|
User *ModelUser `gorm:"foreignKey:UserID;references:ID;constraint:OnDelete:SET NULL"`
|
|
}
|
|
|
|
func (ModelComment) TableName() string {
|
|
return "comments"
|
|
}
|