From 4982d7ffeca49ff60afa826678374145730d5b07 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Thu, 25 Mar 2021 23:33:37 +0100 Subject: [PATCH] add some more shortcuts --- internal/db/pg.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/internal/db/pg.go b/internal/db/pg.go index e73ef210b..ab8af86ca 100644 --- a/internal/db/pg.go +++ b/internal/db/pg.go @@ -371,6 +371,30 @@ func (ps *postgresService) GetLastStatusForAccountID(accountID string, status *m } +func (ps *postgresService) IsUsernameAvailable(username string) error { + // if no error we fail because it means we found something + // if error but it's not pg.ErrNoRows then we fail + // if err is pg.ErrNoRows we're good, we found nothing so continue + if err := ps.conn.Model(&model.Account{}).Where("username = ?").Where("domain = ?", nil).Select(); err == nil { + return fmt.Errorf("username %s already in use", username) + } else if err != pg.ErrNoRows { + return err + } + return nil +} + +func (ps *postgresService) IsEmailAvailable(email string) error { + // if no error we fail because it means we found something + // if error but it's not db.ErrorNoEntries then we fail + // if err is ErrNoEntries we're good, we found nothing so continue + if err := ps.conn.Model(&model.Account{}).Where("email = ?", email).WhereOr("unconfirmed_email = ?", email).Select(); err == nil { + return fmt.Errorf("email %s already in use", email) + } else if err != pg.ErrNoRows { + return err + } + return nil +} + /* CONVERSION FUNCTIONS */