Skip to content

Commit

Permalink
remove activated field from user
Browse files Browse the repository at this point in the history
  • Loading branch information
denpeshkov committed Feb 6, 2024
1 parent bd7dd38 commit e4f6289
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
11 changes: 5 additions & 6 deletions internal/greenlight/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import (

// User represents a user.
type User struct {
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Password Password `json:"-"`
Activated bool `json:"activated"`
Version int `json:"-"`
ID int64 `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Password Password `json:"-"`
Version int `json:"-"`
}

// Valid returns an error if the validation fails, otherwise nil.
Expand Down
5 changes: 2 additions & 3 deletions internal/http/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,8 @@ func (s *Server) handleUserCreate(w http.ResponseWriter, r *http.Request) {
}

u := &greenlight.User{
Name: req.Name,
Email: req.Email,
Activated: false,
Name: req.Name,
Email: req.Email,
}
pass, errPas := greenlight.NewPassword(req.Password)
if errPas != nil {
Expand Down
1 change: 0 additions & 1 deletion internal/postgres/migrations/005_create_users_table.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ CREATE TABLE IF NOT EXISTS users (
name text NOT NULL,
email citext UNIQUE NOT NULL,
password_hash bytea NOT NULL,
activated bool NOT NULL,
version integer NOT NULL DEFAULT 1
);
12 changes: 6 additions & 6 deletions internal/postgres/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func (s *UserService) Get(ctx context.Context, email string) (*greenlight.User,
}
defer tx.Rollback()

query := `SELECT id, name, email, password_hash, activated, version FROM users WHERE email = $1`
query := `SELECT id, name, email, password_hash, version FROM users WHERE email = $1`
args := []any{email}
var u greenlight.User
if err := tx.QueryRowContext(ctx, query, args...).Scan(&u.ID, &u.Name, &u.Password, &u.Activated, &u.Version); err != nil {
if err := tx.QueryRowContext(ctx, query, args...).Scan(&u.ID, &u.Name, &u.Password, &u.Version); err != nil {
switch {
case errors.Is(err, sql.ErrNoRows):
return nil, greenlight.NewNotFoundError("User not found.")
Expand All @@ -65,8 +65,8 @@ func (s *UserService) Create(ctx context.Context, u *greenlight.User) error {
}
defer tx.Rollback()

query := `INSERT INTO users (name, email, password_hash, activated) VALUES ($1, $2, $3, $4) RETURNING id, version`
args := []any{u.ID, u.Email, u.Password, u.Activated}
query := `INSERT INTO users (name, email, password_hash) VALUES ($1, $2, $3) RETURNING id, version`
args := []any{u.ID, u.Email, u.Password}
if err := tx.QueryRowContext(ctx, query, args...).Scan(&u.ID, &u.Version); err != nil {
switch {
case err.Error() == `pq: duplicate key value violates unique constraint "users_email_key"`:
Expand Down Expand Up @@ -94,8 +94,8 @@ func (s *UserService) Update(ctx context.Context, u *greenlight.User) error {
}
defer tx.Rollback()

query := `UPDATE users SET (name, email, password_hash, activated, version) = ($1, $2, $3, $4, version+1) WHERE id = $5 AND version = $6 RETURNING version`
args := []any{u.Name, u.Email, u.Password, u.Activated, u.ID, u.Version}
query := `UPDATE users SET (name, email, password_hash, version) = ($1, $2, $3, version+1) WHERE id = $5 AND version = $6 RETURNING version`
args := []any{u.Name, u.Email, u.Password, u.ID, u.Version}
if err := tx.QueryRowContext(ctx, query, args...).Scan(&u.ID, &u.Version); err != nil {
switch {
case err.Error() == `pq: duplicate key value violates unique constraint "users_email_key"`:
Expand Down

0 comments on commit e4f6289

Please sign in to comment.