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.

63 lines
871 B

  1. package Database
  2. import (
  3. "errors"
  4. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  5. "gorm.io/gorm"
  6. )
  7. func GetUsers(page, pageSize int) ([]Models.User, error) {
  8. var (
  9. users []Models.User
  10. err error
  11. )
  12. if page == 0 {
  13. page = 1
  14. }
  15. switch {
  16. case pageSize > 100:
  17. pageSize = 100
  18. case pageSize <= 0:
  19. pageSize = 10
  20. }
  21. err = DB.Offset(page).
  22. Limit(pageSize).
  23. Find(&users).
  24. Error
  25. return users, err
  26. }
  27. func CheckUniqueEmail(email string) error {
  28. var (
  29. exists bool
  30. err error
  31. )
  32. err = DB.Model(Models.User{}).
  33. Select("count(*) > 0").
  34. Where("email = ?", email).
  35. Find(&exists).
  36. Error
  37. if err != nil {
  38. return err
  39. }
  40. if exists {
  41. return errors.New("Invalid email")
  42. }
  43. return nil
  44. }
  45. func CreateUser(userData *Models.User) error {
  46. return DB.Session(&gorm.Session{FullSaveAssociations: true}).
  47. Create(userData).
  48. Error
  49. }