42 lines
1.5 KiB
Go
42 lines
1.5 KiB
Go
package models
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Message représente un message dans une room de chat
|
|
type Message 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:"column:sender_id;type:uuid;not null" json:"user_id"`
|
|
Content string `gorm:"not null;type:text" json:"content"`
|
|
Type string `gorm:"column:message_type;not null;default:'text'" json:"type"`
|
|
ParentID *uuid.UUID `gorm:"column:reply_to_id;type:uuid" json:"parent_id,omitempty"`
|
|
IsEdited bool `gorm:"default:false" json:"is_edited"`
|
|
IsDeleted bool `gorm:"default:false" json:"is_deleted"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
|
|
|
|
// Relations
|
|
Room Room `gorm:"foreignKey:RoomID;constraint:OnDelete:CASCADE" json:"-"`
|
|
User User `gorm:"foreignKey:UserID;constraint:OnDelete:CASCADE" json:"-"`
|
|
Parent *Message `gorm:"foreignKey:ParentID;constraint:OnDelete:SET NULL" json:"-"`
|
|
}
|
|
|
|
// BeforeCreate hook GORM pour générer UUID si non défini
|
|
func (m *Message) BeforeCreate(tx *gorm.DB) error {
|
|
if m.ID == uuid.Nil {
|
|
m.ID = uuid.New()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// TableName définit le nom de la table pour GORM
|
|
func (Message) TableName() string {
|
|
return "messages"
|
|
}
|