mirror of
https://github.com/ForFarmTeam/ForFarm.git
synced 2025-12-19 14:04:08 +01:00
feat: add GetByEmail, make username nullable
This commit is contained in:
parent
d2bace4825
commit
0acf3ea83d
@ -27,7 +27,7 @@ func (u *User) NormalizedUsername() string {
|
|||||||
func (u *User) Validate() error {
|
func (u *User) Validate() error {
|
||||||
return validation.ValidateStruct(u,
|
return validation.ValidateStruct(u,
|
||||||
validation.Field(&u.UUID, validation.Required),
|
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.Password, validation.Required, validation.Length(6, 100)),
|
||||||
validation.Field(&u.Email, validation.Required, is.Email),
|
validation.Field(&u.Email, validation.Required, is.Email),
|
||||||
)
|
)
|
||||||
@ -36,6 +36,7 @@ func (u *User) Validate() error {
|
|||||||
type UserRepository interface {
|
type UserRepository interface {
|
||||||
GetByID(context.Context, int64) (User, error)
|
GetByID(context.Context, int64) (User, error)
|
||||||
GetByUsername(context.Context, string) (User, error)
|
GetByUsername(context.Context, string) (User, error)
|
||||||
|
GetByEmail(context.Context, string) (User, error)
|
||||||
CreateOrUpdate(context.Context, *User) error
|
CreateOrUpdate(context.Context, *User) error
|
||||||
Delete(context.Context, int64) error
|
Delete(context.Context, int64) error
|
||||||
}
|
}
|
||||||
|
|||||||
@ -78,15 +78,31 @@ func (p *postgresUserRepository) GetByUsername(ctx context.Context, username str
|
|||||||
return users[0], nil
|
return users[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *postgresUserRepository) CreateOrUpdate(ctx context.Context, u *domain.User) error {
|
func (p *postgresUserRepository) GetByEmail(ctx context.Context, email string) (domain.User, error) {
|
||||||
if err := u.Validate(); err != nil {
|
query := `
|
||||||
return err
|
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) == "" {
|
if strings.TrimSpace(u.UUID) == "" {
|
||||||
u.UUID = uuid.New().String()
|
u.UUID = uuid.New().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := u.Validate(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
u.NormalizedUsername()
|
u.NormalizedUsername()
|
||||||
|
|
||||||
query := `
|
query := `
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
uuid UUID NOT NULL,
|
uuid UUID NOT NULL,
|
||||||
username TEXT NOT NULL,
|
username TEXT NULL,
|
||||||
password TEXT NOT NULL,
|
password TEXT NOT NULL,
|
||||||
email TEXT NOT NULL,
|
email TEXT NOT NULL,
|
||||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user