Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
1.3 KiB

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
// TODO: Add pagination
func GetMessagesByAssociationKey(associationKey string) ([]Models.Message, error) {
var (
messages []Models.Message
err error
)
err = DB.Preload("MessageData").
Preload("MessageData.Attachment").
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
}