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.

57 lines
974 B

  1. package Database
  2. import (
  3. "git.tovijaeschke.xyz/tovi/Envelope/Backend/Models"
  4. "github.com/gofrs/uuid"
  5. "gorm.io/gorm"
  6. "gorm.io/gorm/clause"
  7. )
  8. func GetFriendById(id string) (Models.Friend, error) {
  9. var (
  10. friend Models.Friend
  11. err error
  12. )
  13. err = DB.Preload(clause.Associations).
  14. First(&friend, "id = ?", id).
  15. Error
  16. return friend, err
  17. }
  18. func GetFriendsByUserId(userID string) ([]Models.Friend, error) {
  19. var (
  20. friends []Models.Friend
  21. err error
  22. )
  23. err = DB.Model(Models.Friend{}).
  24. Where("user_id = ?", userID).
  25. Find(&friends).
  26. Error
  27. return friends, err
  28. }
  29. func CreateFriendRequest(friend *Models.Friend) error {
  30. var (
  31. err error
  32. )
  33. friend.ThreadID, err = uuid.NewV1()
  34. if err != nil {
  35. return err
  36. }
  37. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  38. Create(friend).
  39. Error
  40. }
  41. func DeleteFriend(friend *Models.Friend) error {
  42. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  43. Delete(friend).
  44. Error
  45. }