tsk-30: Update password (#33)
Closes #30 Reviewed-on: phoenix/textsender-auth#33 Co-authored-by: phoenix <kundeng00@pm.me> Co-committed-by: phoenix <kundeng00@pm.me>
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
@@ -37,9 +38,11 @@ func (m *MockUserStore) CreateUser(ctx context.Context, user *user.User) error {
|
||||
}
|
||||
|
||||
if _, exists := m.UsersByUsername[user.Username]; exists {
|
||||
return errors.New("User with email already exists")
|
||||
return errors.New("User with username already exists")
|
||||
}
|
||||
|
||||
user.Created = time.Now()
|
||||
|
||||
m.Users[user.Id] = user
|
||||
m.UsersByUsername[user.Username] = user
|
||||
return nil
|
||||
@@ -112,3 +115,24 @@ func (m *MockUserStore) UserExists(ctx context.Context, username string) (bool,
|
||||
return exists, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockUserStore) UpdatePassword(ctx context.Context, id uuid.UUID, password string) (int64, error) {
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
if m.Error != nil {
|
||||
return 0, m.Error
|
||||
}
|
||||
|
||||
user, exists := m.Users[id]
|
||||
if !exists {
|
||||
return 0, errors.New("User not found")
|
||||
}
|
||||
|
||||
user.Password = password
|
||||
|
||||
m.Users[id] = user
|
||||
m.UsersByUsername[user.Username] = user
|
||||
|
||||
return 1, nil
|
||||
}
|
||||
+18
-8
@@ -17,6 +17,7 @@ type UserStore interface {
|
||||
GetUserByUsername(ctx context.Context, username string) (*user.User, error)
|
||||
GetAllUsers(ctx context.Context) ([]*user.User, error)
|
||||
UserExists(ctx context.Context, username string) (bool, error)
|
||||
UpdatePassword(ctx context.Context, id uuid.UUID, password string) (int64, error)
|
||||
}
|
||||
|
||||
type PGUserStore struct {
|
||||
@@ -31,20 +32,20 @@ func (s *PGUserStore) CreateUser(ctx context.Context, user *user.User) error {
|
||||
query := `
|
||||
INSERT INTO users (phone_number, username, password)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, phone_number, username
|
||||
RETURNING id, phone_number, username, created
|
||||
`
|
||||
|
||||
return s.db.QueryRow(ctx, query, user.PhoneNumber, user.Username, user.Password).Scan(
|
||||
&user.Id, &user.PhoneNumber, &user.Username,
|
||||
&user.Id, &user.PhoneNumber, &user.Username, &user.Created,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *PGUserStore) GetUserByID(ctx context.Context, id uuid.UUID) (*user.User, error) {
|
||||
query := `SELECT id, username, password, phone_number FROM users WHERE id = $1`
|
||||
query := `SELECT id, username, password, phone_number, created FROM users WHERE id = $1`
|
||||
|
||||
var user user.User
|
||||
err := s.db.QueryRow(ctx, query, id).Scan(
|
||||
&user.Id, &user.Username, &user.Password, &user.PhoneNumber,
|
||||
&user.Id, &user.Username, &user.Password, &user.PhoneNumber, &user.Created,
|
||||
)
|
||||
|
||||
if err == pgx.ErrNoRows {
|
||||
@@ -58,11 +59,11 @@ func (s *PGUserStore) GetUserByID(ctx context.Context, id uuid.UUID) (*user.User
|
||||
}
|
||||
|
||||
func (s *PGUserStore) GetUserByUsername(ctx context.Context, username string) (*user.User, error) {
|
||||
query := `SELECT id, username, password, phone_number FROM users WHERE username = $1`
|
||||
query := `SELECT id, username, password, phone_number, created FROM users WHERE username = $1`
|
||||
|
||||
var user user.User
|
||||
err := s.db.QueryRow(ctx, query, username).Scan(
|
||||
&user.Id, &user.Username, &user.Password, &user.PhoneNumber,
|
||||
&user.Id, &user.Username, &user.Password, &user.PhoneNumber, &user.Created,
|
||||
)
|
||||
|
||||
if err == pgx.ErrNoRows {
|
||||
@@ -76,7 +77,7 @@ func (s *PGUserStore) GetUserByUsername(ctx context.Context, username string) (*
|
||||
}
|
||||
|
||||
func (s *PGUserStore) GetAllUsers(ctx context.Context) ([]*user.User, error) {
|
||||
query := `SELECT id, username, password, phone_number FROM users`
|
||||
query := `SELECT id, username, password, phone_number, created FROM users`
|
||||
|
||||
rows, err := s.db.Query(ctx, query)
|
||||
if err != nil {
|
||||
@@ -88,7 +89,7 @@ func (s *PGUserStore) GetAllUsers(ctx context.Context) ([]*user.User, error) {
|
||||
for rows.Next() {
|
||||
var user user.User
|
||||
if err := rows.Scan(
|
||||
&user.Id, &user.Username, &user.Password, &user.PhoneNumber,
|
||||
&user.Id, &user.Username, &user.Password, &user.PhoneNumber, &user.Created,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scanning user row: %w", err)
|
||||
}
|
||||
@@ -113,3 +114,12 @@ func (s *PGUserStore) UserExists(ctx context.Context, username string) (bool, er
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (s *PGUserStore) UpdatePassword(ctx context.Context, id uuid.UUID, password string) (int64, error) {
|
||||
query := `UPDATE users SET password = $1 WHERE id = $2`
|
||||
if affected, err := s.db.Exec(ctx, query, password, id); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return affected.RowsAffected(), nil
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user