package Database import ( "time" "github.com/gofrs/uuid" "gorm.io/gorm" "gorm.io/gorm/clause" ) // UserConversation Used to link the current user to their conversations type UserConversation struct { Base UserID uuid.UUID `gorm:"type:uuid;column:user_id;not null;" json:"user_id"` User User ` json:"user"` ConversationDetailID string `gorm:"not null" json:"conversation_detail_id"` // Stored encrypted Admin string `gorm:"not null" json:"admin"` // Bool if user is admin of thread, stored encrypted SymmetricKey string `gorm:"not null" json:"symmetric_key"` // Stored encrypted CreatedAt time.Time `gorm:"not null" json:"created_at"` } type UserConversationList []UserConversation func GetUserConversationById(id string) (UserConversation, error) { var ( message UserConversation err error ) err = DB.First(&message, "id = ?", id). Error return message, err } func GetUserConversationsByUserId(id string, page int) ([]UserConversation, error) { var ( conversations []UserConversation offset int err error ) offset = page * PageSize err = DB.Offset(offset). Limit(PageSize). Find(&conversations, "user_id = ?", id). Order("created_at DESC"). Error return conversations, err } func (userConversation *UserConversation) CreateUserConversation() error { var err error err = DB.Session(&gorm.Session{FullSaveAssociations: true}). Create(userConversation). Error return err } func (userConversations *UserConversationList) CreateUserConversations() error { var err error err = DB.Create(userConversations). Error return err } func (userConversation *UserConversation) UpdateUserConversation() error { var err error err = DB.Model(UserConversation{}). Updates(userConversation). Error return err } func (userConversations *UserConversationList) UpdateUserConversations() error { var err error err = DB.Model(UserConversation{}). Updates(userConversations). Error return err } func (userConversations *UserConversationList) UpdateOrCreateUserConversations() error { var err error err = DB.Model(UserConversation{}). Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, DoUpdates: clause.AssignmentColumns([]string{"admin"}), }). Create(userConversations). Error return err } func (userConversation *UserConversation) DeleteUserConversation() error { return DB.Session(&gorm.Session{FullSaveAssociations: true}). Delete(userConversation). Error }