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.

52 lines
898 B

  1. package Database
  2. import (
  3. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  4. "gorm.io/gorm"
  5. "gorm.io/gorm/clause"
  6. )
  7. func GetMessageById(id string) (Models.Message, error) {
  8. var (
  9. message Models.Message
  10. err error
  11. )
  12. err = DB.Preload(clause.Associations).
  13. First(&message, "id = ?", id).
  14. Error
  15. return message, err
  16. }
  17. func GetMessagesByThreadId(id string) ([]Models.Message, error) {
  18. var (
  19. messages []Models.Message
  20. err error
  21. )
  22. err = DB.Preload(clause.Associations).
  23. Find(&messages, "thread_id = ?", id).
  24. Error
  25. return messages, err
  26. }
  27. func CreateMessage(message *Models.Message) error {
  28. var (
  29. err error
  30. )
  31. err = DB.Session(&gorm.Session{FullSaveAssociations: true}).
  32. Create(message).
  33. Error
  34. return err
  35. }
  36. func DeleteMessage(message *Models.Message) error {
  37. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  38. Delete(message).
  39. Error
  40. }