package Database
|
|
|
|
import (
|
|
"git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
|
|
|
|
"gorm.io/gorm"
|
|
"gorm.io/gorm/clause"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func GetMessagesByThreadKey(associationKey string) ([]Models.Message, error) {
|
|
var (
|
|
messages []Models.Message
|
|
err error
|
|
)
|
|
|
|
err = DB.Preload("MessageData").
|
|
Find(&messages, "association_key = ?", associationKey).
|
|
Error
|
|
|
|
return messages, err
|
|
}
|
|
|
|
func CreateMessage(message *Models.Message) error {
|
|
var (
|
|
err error
|
|
)
|
|
|
|
err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Create(message).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func CreateMessages(messages *[]Models.Message) error {
|
|
var (
|
|
err error
|
|
)
|
|
|
|
err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Create(messages).
|
|
Error
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteMessage(message *Models.Message) error {
|
|
return DB.Session(&gorm.Session{FullSaveAssociations: true}).
|
|
Delete(message).
|
|
Error
|
|
}
|