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.

50 lines
1.1 KiB

  1. package Database
  2. import (
  3. "gorm.io/gorm"
  4. "gorm.io/gorm/clause"
  5. )
  6. // Attachment holds the attachment data
  7. type Attachment struct {
  8. Base
  9. FilePath string `gorm:"not null" json:"-"`
  10. Mimetype string `gorm:"not null" json:"mimetype"`
  11. Extension string `gorm:"not null" json:"extension"`
  12. Data string `gorm:"-" json:"data"`
  13. ImageLink string `gorm:"-" json:"image_link"`
  14. }
  15. // GetAttachmentByID gets the attachment record by the id
  16. func GetAttachmentByID(id string) (MessageData, error) {
  17. var (
  18. messageData MessageData
  19. err error
  20. )
  21. err = DB.Preload(clause.Associations).
  22. First(&messageData, "id = ?", id).
  23. Error
  24. return messageData, err
  25. }
  26. // CreateAttachment creates the attachment record
  27. func (attachment *Attachment) CreateAttachment() error {
  28. var (
  29. err error
  30. )
  31. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  32. Create(attachment).
  33. Error
  34. return err
  35. }
  36. // DeleteAttachment deletes the attachment record
  37. func (attachment *Attachment) DeleteAttachment() error {
  38. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  39. Delete(attachment).
  40. Error
  41. }