fog/internal/store/postgres/users.go

45 lines
656 B
Go
Raw Permalink Normal View History

2024-12-30 10:02:01 +00:00
package postgres
import (
"context"
"database/sql"
"time"
"github.com/lib/pq"
"midnadimple.com/fog/internal/store"
)
type UsersStore struct {
DB *sql.DB
}
func (s *UsersStore) Create(ctx context.Context, user *store.User) error {
query := `
INSERT INTO users (username, password, email) VALUES($1, $2, $3) RETURNING id, created_at
`
var createdAtStr string
err := s.DB.QueryRowContext(
ctx,
query,
user.Username,
user.Password,
user.Email,
).Scan(
&user.ID,
&createdAtStr,
)
if err != nil {
return err
}
user.CreatedAt, err = pq.ParseTimestamp(time.UTC, createdAtStr)
if err != nil {
return err
}
return nil
}