refactor: unify account exists methods
This commit is contained in:
parent
5598660176
commit
99c440ab88
|
@ -33,37 +33,45 @@ func NewAccountService(params AccountServiceParams) *AccountServiceDefault {
|
||||||
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity}
|
return &AccountServiceDefault{db: params.Db, config: params.Config, identity: params.Identity}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceDefault) EmailExists(email string) (bool, models.User) {
|
func (s *AccountServiceDefault) exists(model interface{}, conditions map[string]interface{}) (bool, interface{}, error) {
|
||||||
var user models.User
|
// Conduct a query with the provided model and conditions
|
||||||
|
result := s.db.Model(model).Where(conditions).First(model)
|
||||||
|
|
||||||
result := s.db.Model(&models.User{}).Where(&models.User{Email: email}).First(&user)
|
// Check if any rows were found
|
||||||
|
exists := result.RowsAffected > 0
|
||||||
|
|
||||||
return result.RowsAffected > 0, user
|
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
|
||||||
}
|
return false, nil, nil
|
||||||
func (s AccountServiceDefault) PubkeyExists(pubkey string) (bool, models.PublicKey) {
|
}
|
||||||
var model models.PublicKey
|
|
||||||
|
|
||||||
result := s.db.Model(&models.PublicKey{}).Where(&models.PublicKey{Key: pubkey}).First(&model)
|
return exists, model, result.Error
|
||||||
|
|
||||||
return result.RowsAffected > 0, model
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceDefault) AccountExists(id uint64) (bool, models.User) {
|
func (s *AccountServiceDefault) EmailExists(email string) (bool, *models.User, error) {
|
||||||
var model models.User
|
user := &models.User{}
|
||||||
|
exists, model, err := s.exists(user, map[string]interface{}{"email": email})
|
||||||
result := s.db.Model(&models.User{}).First(&model, id)
|
if !exists || err != nil {
|
||||||
|
return false, nil, err
|
||||||
return result.RowsAffected > 0, model
|
}
|
||||||
|
return true, model.(*models.User), nil // Type assertion since `exists` returns interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceDefault) AccountExistsByEmail(email string) (bool, models.User) {
|
func (s *AccountServiceDefault) PubkeyExists(pubkey string) (bool, *models.PublicKey, error) {
|
||||||
var model models.User
|
publicKey := &models.PublicKey{}
|
||||||
|
exists, model, err := s.exists(publicKey, map[string]interface{}{"key": pubkey})
|
||||||
|
if !exists || err != nil {
|
||||||
|
return false, nil, err
|
||||||
|
}
|
||||||
|
return true, model.(*models.PublicKey), nil // Type assertion is necessary
|
||||||
|
}
|
||||||
|
|
||||||
model.Email = email
|
func (s *AccountServiceDefault) AccountExists(id uint) (bool, *models.User, error) {
|
||||||
|
user := &models.User{}
|
||||||
result := s.db.Model(&models.User{}).Where(&model).First(&model)
|
exists, model, err := s.exists(user, map[string]interface{}{"id": id})
|
||||||
|
if !exists || err != nil {
|
||||||
return result.RowsAffected > 0, model
|
return false, nil, err
|
||||||
|
}
|
||||||
|
return true, model.(*models.User), nil // Ensure to assert the type correctly
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s AccountServiceDefault) CreateAccount(email string, password string) (*models.User, error) {
|
func (s AccountServiceDefault) CreateAccount(email string, password string) (*models.User, error) {
|
||||||
|
|
Loading…
Reference in New Issue