65 lines
2.2 KiB
Go
65 lines
2.2 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Room représente une room de chat
|
|
type Room struct {
|
|
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
|
|
Name string `gorm:"size:255" json:"name"`
|
|
Description string `gorm:"type:text" json:"description"`
|
|
Type string `gorm:"column:room_type;not null;default:'public'" json:"type"`
|
|
IsPrivate bool `gorm:"default:false" json:"is_private"`
|
|
CreatedBy uuid.UUID `gorm:"column:creator_id;type:uuid;not null" json:"created_by"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `json:"-"`
|
|
|
|
// Relations
|
|
Creator User `gorm:"foreignKey:CreatedBy;constraint:OnDelete:CASCADE" json:"-"`
|
|
Members []RoomMember `gorm:"foreignKey:RoomID;constraint:OnDelete:CASCADE" json:"members,omitempty"`
|
|
Messages []Message `gorm:"foreignKey:RoomID;constraint:OnDelete:CASCADE" json:"messages,omitempty"`
|
|
}
|
|
|
|
// BeforeCreate hook GORM pour générer UUID si non défini
|
|
func (r *Room) BeforeCreate(tx *gorm.DB) error {
|
|
if r.ID == uuid.Nil {
|
|
r.ID = uuid.New()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TableName définit le nom de la table pour GORM
|
|
func (Room) TableName() string {
|
|
return "rooms"
|
|
}
|
|
|
|
// RoomMember représente l'appartenance d'un utilisateur à une room
|
|
type RoomMember struct {
|
|
ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
|
|
RoomID uuid.UUID `gorm:"type:uuid;not null" json:"room_id"`
|
|
UserID uuid.UUID `gorm:"type:uuid;not null" json:"user_id"`
|
|
Role string `gorm:"not null;default:'member'" json:"role"`
|
|
JoinedAt time.Time `gorm:"autoCreateTime" json:"joined_at"`
|
|
|
|
// Relations
|
|
Room Room `gorm:"foreignKey:RoomID;constraint:OnDelete:CASCADE" json:"-"`
|
|
User User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
|
|
}
|
|
|
|
// BeforeCreate hook GORM pour générer UUID si non défini
|
|
func (rm *RoomMember) BeforeCreate(tx *gorm.DB) error {
|
|
if rm.ID == uuid.Nil {
|
|
rm.ID = uuid.New()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TableName définit le nom de la table pour GORM
|
|
func (RoomMember) TableName() string {
|
|
return "room_members"
|
|
}
|