Skip to content

Commit

Permalink
Merge pull request #4 from ubccsss/steph-feng/user-store-continued
Browse files Browse the repository at this point in the history
feat: user store implementation, continued
  • Loading branch information
karenzhao35 authored Dec 30, 2023
2 parents a894ab1 + eb6a48b commit b695c66
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions store/user.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package store

import (
"context"
"fmt"
Expand Down Expand Up @@ -85,26 +86,49 @@ 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")
}

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"

_, 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)

}

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
}

0 comments on commit b695c66

Please sign in to comment.