2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import ()
|
|
|
|
|
2019-12-28 08:20:05 +08:00
|
|
|
var commentersRowColumns string = `
|
|
|
|
commenters.commenterHex,
|
|
|
|
commenters.email,
|
|
|
|
commenters.name,
|
|
|
|
commenters.link,
|
|
|
|
commenters.photo,
|
|
|
|
commenters.provider,
|
|
|
|
commenters.joinDate
|
|
|
|
`
|
|
|
|
|
|
|
|
func commentersRowScan(s sqlScanner, c *commenter) error {
|
|
|
|
return s.Scan(
|
|
|
|
&c.CommenterHex,
|
|
|
|
&c.Email,
|
|
|
|
&c.Name,
|
|
|
|
&c.Link,
|
|
|
|
&c.Photo,
|
|
|
|
&c.Provider,
|
|
|
|
&c.JoinDate,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2018-05-27 22:40:42 +08:00
|
|
|
func commenterGetByHex(commenterHex string) (commenter, error) {
|
|
|
|
if commenterHex == "" {
|
|
|
|
return commenter{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 08:20:05 +08:00
|
|
|
SELECT ` + commentersRowColumns + `
|
|
|
|
FROM commenters
|
|
|
|
WHERE commenterHex = $1;
|
|
|
|
`
|
2018-05-27 22:40:42 +08:00
|
|
|
row := db.QueryRow(statement, commenterHex)
|
|
|
|
|
2019-12-28 08:20:05 +08:00
|
|
|
var c commenter
|
|
|
|
if err := commentersRowScan(row, &c); err != nil {
|
2018-06-07 00:51:31 +08:00
|
|
|
// TODO: is this the only error?
|
|
|
|
return commenter{}, errorNoSuchCommenter
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-06-07 15:41:02 +08:00
|
|
|
func commenterGetByEmail(provider string, email string) (commenter, error) {
|
|
|
|
if provider == "" || email == "" {
|
|
|
|
return commenter{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 08:20:05 +08:00
|
|
|
SELECT ` + commentersRowColumns + `
|
|
|
|
FROM commenters
|
|
|
|
WHERE email = $1 AND provider = $2;
|
|
|
|
`
|
2018-06-07 15:41:02 +08:00
|
|
|
row := db.QueryRow(statement, email, provider)
|
|
|
|
|
2019-12-28 08:20:05 +08:00
|
|
|
var c commenter
|
|
|
|
if err := commentersRowScan(row, &c); err != nil {
|
2018-06-07 15:41:02 +08:00
|
|
|
// TODO: is this the only error?
|
|
|
|
return commenter{}, errorNoSuchCommenter
|
|
|
|
}
|
|
|
|
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
func commenterGetByCommenterToken(commenterToken string) (commenter, error) {
|
|
|
|
if commenterToken == "" {
|
2018-05-27 22:40:42 +08:00
|
|
|
return commenter{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-28 08:20:05 +08:00
|
|
|
SELECT ` + commentersRowColumns + `
|
|
|
|
FROM commenterSessions
|
|
|
|
JOIN commenters ON commenterSessions.commenterHex = commenters.commenterHex
|
|
|
|
WHERE commenterToken = $1;
|
2018-05-27 22:40:42 +08:00
|
|
|
`
|
2018-06-20 11:29:55 +08:00
|
|
|
row := db.QueryRow(statement, commenterToken)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
2019-12-28 08:20:05 +08:00
|
|
|
var c commenter
|
|
|
|
if err := commentersRowScan(row, &c); err != nil {
|
2019-11-24 17:53:54 +08:00
|
|
|
// TODO: is this the only error?
|
2018-06-20 11:29:55 +08:00
|
|
|
return commenter{}, errorNoSuchToken
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:53:54 +08:00
|
|
|
if c.CommenterHex == "none" {
|
2018-06-20 11:29:55 +08:00
|
|
|
return commenter{}, errorNoSuchToken
|
2018-06-07 00:51:31 +08:00
|
|
|
}
|
|
|
|
|
2019-11-24 17:53:54 +08:00
|
|
|
return c, nil
|
2018-05-27 22:40:42 +08:00
|
|
|
}
|