diff --git a/backend/internal/domain/user.go b/backend/internal/domain/user.go index 9a2d58b..16fc939 100644 --- a/backend/internal/domain/user.go +++ b/backend/internal/domain/user.go @@ -27,7 +27,7 @@ func (u *User) NormalizedUsername() string { func (u *User) Validate() error { return validation.ValidateStruct(u, validation.Field(&u.UUID, validation.Required), - validation.Field(&u.Username, validation.Required, validation.Length(3, 20)), + validation.Field(&u.Username, validation.Length(3, 20)), validation.Field(&u.Password, validation.Required, validation.Length(6, 100)), validation.Field(&u.Email, validation.Required, is.Email), ) @@ -36,6 +36,7 @@ func (u *User) Validate() error { type UserRepository interface { GetByID(context.Context, int64) (User, error) GetByUsername(context.Context, string) (User, error) + GetByEmail(context.Context, string) (User, error) CreateOrUpdate(context.Context, *User) error Delete(context.Context, int64) error } diff --git a/backend/internal/repository/postgres_user.go b/backend/internal/repository/postgres_user.go index 6619e96..67a00f5 100644 --- a/backend/internal/repository/postgres_user.go +++ b/backend/internal/repository/postgres_user.go @@ -78,15 +78,31 @@ func (p *postgresUserRepository) GetByUsername(ctx context.Context, username str return users[0], nil } -func (p *postgresUserRepository) CreateOrUpdate(ctx context.Context, u *domain.User) error { - if err := u.Validate(); err != nil { - return err - } +func (p *postgresUserRepository) GetByEmail(ctx context.Context, email string) (domain.User, error) { + query := ` + SELECT id, uuid, username, password, email, created_at, updated_at, is_active + FROM users + WHERE email = $1` + users, err := p.fetch(ctx, query, email) + if err != nil { + return domain.User{}, err + } + if len(users) == 0 { + return domain.User{}, domain.ErrNotFound + } + return users[0], nil +} + +func (p *postgresUserRepository) CreateOrUpdate(ctx context.Context, u *domain.User) error { if strings.TrimSpace(u.UUID) == "" { u.UUID = uuid.New().String() } + if err := u.Validate(); err != nil { + return err + } + u.NormalizedUsername() query := ` diff --git a/backend/migrations/00001_create_user_table.sql b/backend/migrations/00001_create_user_table.sql index 8b0b6d7..afa1499 100644 --- a/backend/migrations/00001_create_user_table.sql +++ b/backend/migrations/00001_create_user_table.sql @@ -2,7 +2,7 @@ CREATE TABLE users ( id SERIAL PRIMARY KEY, uuid UUID NOT NULL, - username TEXT NOT NULL, + username TEXT NULL, password TEXT NOT NULL, email TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),