chandafa's picture
deployment v0.1
1b18b9e
package models
import (
"time"
"github.com/google/uuid"
"gorm.io/gorm"
)
type ChatRoom struct {
ID uuid.UUID `json:"id" gorm:"type:char(36);primaryKey"`
UserA uuid.UUID `json:"user_a" gorm:"type:char(36);index"`
UserB uuid.UUID `json:"user_b" gorm:"type:char(36);index"`
IsActive bool `json:"is_active" gorm:"default:true"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
func (r *ChatRoom) BeforeCreate(tx *gorm.DB) error {
if r.ID == uuid.Nil {
r.ID = uuid.New()
}
return nil
}
type ChatMessage struct {
ID uuid.UUID `json:"id" gorm:"type:char(36);primaryKey"`
RoomID uuid.UUID `json:"room_id" gorm:"type:char(36);index"`
SenderID uuid.UUID `json:"sender_id" gorm:"type:char(36);index"`
Message string `json:"message" gorm:"type:text"`
CreatedAt time.Time `json:"created_at"`
}
func (m *ChatMessage) BeforeCreate(tx *gorm.DB) error {
if m.ID == uuid.Nil {
m.ID = uuid.New()
}
return nil
}
type ChatStartResponse struct {
RoomID uuid.UUID `json:"room_id"`
Message string `json:"message"`
CreatedAt time.Time `json:"created_at"`
}
type SendMessageRequest struct {
Message string `json:"message" validate:"required,min=1,max=1000"`
}
type MessageResponse struct {
ID uuid.UUID `json:"id"`
RoomID uuid.UUID `json:"room_id"`
SenderID uuid.UUID `json:"sender_id"`
Message string `json:"message"`
CreatedAt time.Time `json:"created_at"`
IsOwn bool `json:"is_own"`
}