Refactor sql row can using collectable rows
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
@@ -16,6 +16,7 @@ package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
@@ -26,11 +27,11 @@ import (
|
||||
|
||||
type (
|
||||
Session struct {
|
||||
ID gid.GID
|
||||
UserID gid.GID
|
||||
ExpiredAt time.Time
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID gid.GID `db:"id"`
|
||||
UserID gid.GID `db:"user_id"`
|
||||
ExpiredAt time.Time `db:"expired_at"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -38,16 +39,6 @@ func (s Session) CursorKey() page.CursorKey {
|
||||
return page.NewCursorKey(s.ID, s.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *Session) scan(r pgx.Row) error {
|
||||
return r.Scan(
|
||||
&s.ID,
|
||||
&s.UserID,
|
||||
&s.ExpiredAt,
|
||||
&s.CreatedAt,
|
||||
&s.UpdatedAt,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *Session) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
@@ -69,14 +60,16 @@ LIMIT 1;
|
||||
|
||||
args := pgx.NamedArgs{"session_id": sessionID}
|
||||
|
||||
r := conn.QueryRow(ctx, q, args)
|
||||
|
||||
s2 := Session{}
|
||||
if err := s2.scan(r); err != nil {
|
||||
return err
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query session: %w", err)
|
||||
}
|
||||
|
||||
*s = s2
|
||||
session, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Session])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect session: %w", err)
|
||||
}
|
||||
*s = session
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ package coredata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
@@ -26,13 +27,13 @@ import (
|
||||
|
||||
type (
|
||||
User struct {
|
||||
ID gid.GID
|
||||
EmailAddress string
|
||||
HashedPassword []byte
|
||||
FullName string
|
||||
OrganizationID gid.GID
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
ID gid.GID `db:"id"`
|
||||
EmailAddress string `db:"email_address"`
|
||||
HashedPassword []byte `db:"hashed_password"`
|
||||
FullName string `db:"fullname"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
)
|
||||
|
||||
@@ -40,18 +41,6 @@ func (u User) CursorKey() page.CursorKey {
|
||||
return page.NewCursorKey(u.ID, u.CreatedAt)
|
||||
}
|
||||
|
||||
func (u *User) scan(r pgx.Row) error {
|
||||
return r.Scan(
|
||||
&u.ID,
|
||||
&u.EmailAddress,
|
||||
&u.HashedPassword,
|
||||
&u.FullName,
|
||||
&u.OrganizationID,
|
||||
&u.CreatedAt,
|
||||
&u.UpdatedAt,
|
||||
)
|
||||
}
|
||||
|
||||
func (u *User) LoadByEmail(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
@@ -75,14 +64,17 @@ LIMIT 1;
|
||||
|
||||
args := pgx.NamedArgs{"user_email": email}
|
||||
|
||||
r := conn.QueryRow(ctx, q, args)
|
||||
|
||||
u2 := User{}
|
||||
if err := u2.scan(r); err != nil {
|
||||
return err
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query user: %w", err)
|
||||
}
|
||||
|
||||
*u = u2
|
||||
user, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[User])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect user: %w", err)
|
||||
}
|
||||
|
||||
*u = user
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -110,14 +102,17 @@ LIMIT 1;
|
||||
|
||||
args := pgx.NamedArgs{"user_id": userID}
|
||||
|
||||
r := conn.QueryRow(ctx, q, args)
|
||||
|
||||
u2 := User{}
|
||||
if err := u2.scan(r); err != nil {
|
||||
return err
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query user: %w", err)
|
||||
}
|
||||
|
||||
*u = u2
|
||||
user, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[User])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect user: %w", err)
|
||||
}
|
||||
|
||||
*u = user
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user