From 624a95eaea3a248df4820077062ccca2076b8474 Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Mon, 25 Dec 2023 12:05:26 -0800 Subject: [PATCH 1/4] feat: implemented Update() --- store/user.go | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/store/user.go b/store/user.go index a278c6b..4623a4c 100644 --- a/store/user.go +++ b/store/user.go @@ -1,7 +1,9 @@ package store + import ( "context" "fmt" + "log" "strconv" "time" @@ -85,24 +87,46 @@ func (s *pgUserStore) GetOneByEmail(ctx context.Context, email string) (*User, e } func (s *pgUserStore) Create(ctx context.Context, user User) (*User, error) { - // String StudentID conversion to ingetger + // String StudentID conversion to ingetger value, err := strconv.Atoi(user.StudentID) if err != nil { return nil, fmt.Errorf("studentID must be a valid integer") } - + query := "INSERT INTO users (email, name, student_id) VALUES ($1, $2, $3) RETURNING id, created_at" - // reassign the error value if needed + // reassign the error value if needed err = s.db.QueryRowContext(ctx, query, user.Email, user.Name, value).Scan(&user.ID, &user.CreatedAt) if err != nil { return nil, fmt.Errorf("Unable to create and store a user, error %w", err) } - return &user, nil + return &user, nil } func (s *pgUserStore) Update(ctx context.Context, user User) (*User, error) { - return nil, nil + value, err := strconv.Atoi(user.StudentID) + if err != nil { + return nil, fmt.Errorf("studentID must be a valid integer") + } + + query := "UPDATE users SET email = $1, name = $2, student_id = $3 WHERE id = $4" + + result, err = s.db.ExecContext(ctx, query, user.Email, user.Name, value, user.ID) + if err != nil { + return nil, fmt.Errorf("Unable to update user, error %w", err) + + } + + rows, err := result.RowsAffected() + if err != nil { + return nil, fmt.Errorf("Unable to update user, error %w", err) + } + + if rows != 1 { + log.Fatalf("Expected to update 1 row, affected %d", rows) + } + + return &user, nil } func (s *pgUserStore) Delete(ctx context.Context, id int) error { From dd2c50c9a8ba33a39b7b6c41501879dcdd5b701e Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Mon, 25 Dec 2023 12:13:17 -0800 Subject: [PATCH 2/4] feat: implemented Delete() --- store/user.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/store/user.go b/store/user.go index 4623a4c..43dc144 100644 --- a/store/user.go +++ b/store/user.go @@ -3,7 +3,6 @@ package store import ( "context" "fmt" - "log" "strconv" "time" @@ -111,24 +110,25 @@ func (s *pgUserStore) Update(ctx context.Context, user User) (*User, error) { query := "UPDATE users SET email = $1, name = $2, student_id = $3 WHERE id = $4" - result, err = s.db.ExecContext(ctx, query, user.Email, user.Name, value, user.ID) - if err != nil { - return nil, fmt.Errorf("Unable to update user, error %w", err) - - } + _, err := s.db.ExecContext(ctx, query, user.Email, user.Name, value, user.ID) - rows, err := result.RowsAffected() if err != nil { return nil, fmt.Errorf("Unable to update user, error %w", err) - } - if rows != 1 { - log.Fatalf("Expected to update 1 row, affected %d", rows) } return &user, nil } func (s *pgUserStore) Delete(ctx context.Context, id int) error { + query := "DELETE FROM users WHERE id = $1" + + _, err := s.db.ExecContext(ctx, query, id) + + if err != nil { + return fmt.Errorf("Unable to delete user, error %w", err) + + } + return nil } From 4668d08a8e886f7e1869568116186c1a67132ffd Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Mon, 25 Dec 2023 14:44:08 -0800 Subject: [PATCH 3/4] fix: spelling in comment --- store/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/user.go b/store/user.go index 43dc144..17b4458 100644 --- a/store/user.go +++ b/store/user.go @@ -86,7 +86,7 @@ func (s *pgUserStore) GetOneByEmail(ctx context.Context, email string) (*User, e } func (s *pgUserStore) Create(ctx context.Context, user User) (*User, error) { - // String StudentID conversion to ingetger + // String StudentID conversion to integer value, err := strconv.Atoi(user.StudentID) if err != nil { return nil, fmt.Errorf("studentID must be a valid integer") From eb6a48b3962a63b1797066f066bde06cc9389b1b Mon Sep 17 00:00:00 2001 From: Stephanie Feng Date: Fri, 29 Dec 2023 17:33:54 -0800 Subject: [PATCH 4/4] fix: syntax error in Update() --- store/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/store/user.go b/store/user.go index 17b4458..5d2d92a 100644 --- a/store/user.go +++ b/store/user.go @@ -110,7 +110,7 @@ func (s *pgUserStore) Update(ctx context.Context, user User) (*User, error) { query := "UPDATE users SET email = $1, name = $2, student_id = $3 WHERE id = $4" - _, err := s.db.ExecContext(ctx, query, user.Email, user.Name, value, user.ID) + _, err = s.db.ExecContext(ctx, query, user.Email, user.Name, value, user.ID) if err != nil { return nil, fmt.Errorf("Unable to update user, error %w", err)