package Database
|
|
|
|
import (
|
|
"git.tovijaeschke.xyz/tovi/Capsule/Backend/Models"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
// GetMessageByID gets a message
|
|
func GetMessageByID(id string) (Models.Message, error) {
|
|
var (
|
|
message Models.Message
|
|
err error
|
|
)
|
|
|
|
err = DB.Preload(clause.Associations).
|
|
First(&message, "id = ?", id).
|
|
Error
|
|
|
|
return message, err
|
|
}
|
|
|
|
// GetMessagesByAssociationKey for getting whole thread
|
|
func GetMessagesByAssociationKey(associationKey string, page int) ([]Models.Message, error) {
|
|
var (
|
|
messages []Models.Message
|
|
offset int
|
|
err error
|
|
)
|
|
|
|
offset = page * PageSize
|
|
|
|
err = DB.Preload("MessageData").
|
|
Preload("MessageData.Attachment").
|
|
Offset(offset).
|
|
Limit(PageSize).
|
|
Order("created_at DESC").
|
|
Find(&messages, "association_key = ?", associationKey).
|
|
Error
|
|
|
|
return messages, err
|
|
}
|
|
|
|
// CreateMessage creates a message record
|
|
func CreateMessage(message *Models.Message) error {
|
|
var err error
|
|
|
|
err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Create(message).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
// CreateMessages creates multiple records
|
|
func CreateMessages(messages *[]Models.Message) error {
|
|
var err error
|
|
|
|
err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Create(messages).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
// DeleteMessage deletes a message
|
|
func DeleteMessage(message *Models.Message) error {
|
|
return DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Delete(message).
|
|
Error
|
|
}
|