Commit f6d6a1f7 authored by Adhityaa Chandrasekar's avatar Adhityaa Chandrasekar

owner_get.go: clean up SQL

parent d6e7507b
...@@ -2,20 +2,38 @@ package main ...@@ -2,20 +2,38 @@ package main
import () import ()
var ownersRowColumns string = `
owners.ownerHex,
owners.email,
owners.name,
owners.confirmedEmail,
owners.joinDate
`
func ownersRowScan(s sqlScanner, o *owner) error {
return s.Scan(
&o.OwnerHex,
&o.Email,
&o.Name,
&o.ConfirmedEmail,
&o.JoinDate,
)
}
func ownerGetByEmail(email string) (owner, error) { func ownerGetByEmail(email string) (owner, error) {
if email == "" { if email == "" {
return owner{}, errorMissingField return owner{}, errorMissingField
} }
statement := ` statement := `
SELECT ownerHex, email, name, confirmedEmail, joinDate SELECT ` + ownersRowColumns + `
FROM owners FROM owners
WHERE email=$1; WHERE email=$1;
` `
row := db.QueryRow(statement, email) row := db.QueryRow(statement, email)
var o owner var o owner
if err := row.Scan(&o.OwnerHex, &o.Email, &o.Name, &o.ConfirmedEmail, &o.JoinDate); err != nil { if err := ownersRowScan(row, &o); err != nil {
// TODO: Make sure this is actually no such email. // TODO: Make sure this is actually no such email.
return owner{}, errorNoSuchEmail return owner{}, errorNoSuchEmail
} }
...@@ -29,17 +47,17 @@ func ownerGetByOwnerToken(ownerToken string) (owner, error) { ...@@ -29,17 +47,17 @@ func ownerGetByOwnerToken(ownerToken string) (owner, error) {
} }
statement := ` statement := `
SELECT ownerHex, email, name, confirmedEmail, joinDate SELECT ` + ownersRowColumns + `
FROM owners FROM owners
WHERE ownerHex IN ( WHERE owners.ownerHex IN (
SELECT ownerHex FROM ownerSessions SELECT ownerSessions.ownerHex FROM ownerSessions
WHERE ownerToken = $1 WHERE ownerSessions.ownerToken = $1
); );
` `
row := db.QueryRow(statement, ownerToken) row := db.QueryRow(statement, ownerToken)
var o owner var o owner
if err := row.Scan(&o.OwnerHex, &o.Email, &o.Name, &o.ConfirmedEmail, &o.JoinDate); err != nil { if err := ownersRowScan(row, &o); err != nil {
logger.Errorf("cannot scan owner: %v\n", err) logger.Errorf("cannot scan owner: %v\n", err)
return owner{}, errorInternal return owner{}, errorInternal
} }
...@@ -53,14 +71,14 @@ func ownerGetByOwnerHex(ownerHex string) (owner, error) { ...@@ -53,14 +71,14 @@ func ownerGetByOwnerHex(ownerHex string) (owner, error) {
} }
statement := ` statement := `
SELECT ownerHex, email, name, confirmedEmail, joinDate SELECT ` + ownersRowColumns + `
FROM owners FROM owners
WHERE ownerHex = $1; WHERE ownerHex = $1;
` `
row := db.QueryRow(statement, ownerHex) row := db.QueryRow(statement, ownerHex)
var o owner var o owner
if err := row.Scan(&o.OwnerHex, &o.Email, &o.Name, &o.ConfirmedEmail, &o.JoinDate); err != nil { if err := ownersRowScan(row, &o); err != nil {
logger.Errorf("cannot scan owner: %v\n", err) logger.Errorf("cannot scan owner: %v\n", err)
return owner{}, errorInternal return owner{}, errorInternal
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment