2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import ()
|
|
|
|
|
2019-12-28 09:58:17 +08:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
func ownerGetByEmail(email string) (owner, error) {
|
|
|
|
if email == "" {
|
|
|
|
return owner{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 09:58:17 +08:00
|
|
|
SELECT ` + ownersRowColumns + `
|
2019-12-28 09:31:27 +08:00
|
|
|
FROM owners
|
|
|
|
WHERE email=$1;
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
row := db.QueryRow(statement, email)
|
|
|
|
|
|
|
|
var o owner
|
2019-12-28 09:58:17 +08:00
|
|
|
if err := ownersRowScan(row, &o); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
// TODO: Make sure this is actually no such email.
|
|
|
|
return owner{}, errorNoSuchEmail
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, nil
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
func ownerGetByOwnerToken(ownerToken string) (owner, error) {
|
|
|
|
if ownerToken == "" {
|
2018-05-27 22:40:42 +08:00
|
|
|
return owner{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 09:58:17 +08:00
|
|
|
SELECT ` + ownersRowColumns + `
|
2018-05-27 22:40:42 +08:00
|
|
|
FROM owners
|
2019-12-28 09:58:17 +08:00
|
|
|
WHERE owners.ownerHex IN (
|
|
|
|
SELECT ownerSessions.ownerHex FROM ownerSessions
|
|
|
|
WHERE ownerSessions.ownerToken = $1
|
2018-05-27 22:40:42 +08:00
|
|
|
);
|
|
|
|
`
|
2018-06-20 11:29:55 +08:00
|
|
|
row := db.QueryRow(statement, ownerToken)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
var o owner
|
2019-12-28 09:58:17 +08:00
|
|
|
if err := ownersRowScan(row, &o); err != nil {
|
2018-05-27 22:40:42 +08:00
|
|
|
logger.Errorf("cannot scan owner: %v\n", err)
|
|
|
|
return owner{}, errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, nil
|
|
|
|
}
|
2019-02-19 00:23:44 +08:00
|
|
|
|
|
|
|
func ownerGetByOwnerHex(ownerHex string) (owner, error) {
|
|
|
|
if ownerHex == "" {
|
|
|
|
return owner{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 09:58:17 +08:00
|
|
|
SELECT ` + ownersRowColumns + `
|
2019-02-19 00:23:44 +08:00
|
|
|
FROM owners
|
|
|
|
WHERE ownerHex = $1;
|
|
|
|
`
|
|
|
|
row := db.QueryRow(statement, ownerHex)
|
|
|
|
|
|
|
|
var o owner
|
2019-12-28 09:58:17 +08:00
|
|
|
if err := ownersRowScan(row, &o); err != nil {
|
2019-02-19 00:23:44 +08:00
|
|
|
logger.Errorf("cannot scan owner: %v\n", err)
|
|
|
|
return owner{}, errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return o, nil
|
|
|
|
}
|