users.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. package store
  2. import "github.com/google/uuid"
  3. type UserHost struct {
  4. ID uuid.UUID `gorm:"type:uuid;primary_key" json:"id"`
  5. Email string `gorm:"unique;size:255" json:"email"`
  6. Host string `gorm:"size:255" json:"host"`
  7. }
  8. func (UserHost) TableName() string {
  9. return "user_hosts"
  10. }
  11. // GetUserHost returns the host associated with the given email
  12. func (s *DataStore) GetUserHost(email string) (string, error) {
  13. var user UserHost
  14. if err := s.db.Where("email = ?", email).First(&user).Error; err != nil {
  15. return "", err
  16. }
  17. return user.Host, nil
  18. }
  19. // AddUserHost adds a new user with the given email and host
  20. func (s *DataStore) AddUserHost(email, host string) error {
  21. user := UserHost{
  22. Email: email,
  23. Host: host,
  24. }
  25. return s.db.Create(&user).Error
  26. }
  27. // DeleteUserHost deletes the user with the given email
  28. func (s *DataStore) DeleteUserHost(email string) error {
  29. return s.db.Where("email = ?", email).Delete(UserHost{}).Error
  30. }
  31. // UpdateUserHost updates the host associated with the given email
  32. func (s *DataStore) UpdateUserHost(email, host string) error {
  33. return s.db.Model(UserHost{}).Where("email = ?", email).Update("host", host).Error
  34. }