2018-07-05 13:06:52 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
)
|
|
|
|
|
|
|
|
func pageGet(domain string, path string) (page, error) {
|
|
|
|
// path can be empty
|
|
|
|
if domain == "" {
|
|
|
|
return page{}, errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-02-19 00:23:44 +08:00
|
|
|
SELECT isLocked, commentCount, stickyCommentHex, title
|
2018-07-05 13:06:52 +08:00
|
|
|
FROM pages
|
|
|
|
WHERE domain=$1 AND path=$2;
|
|
|
|
`
|
|
|
|
row := db.QueryRow(statement, domain, path)
|
|
|
|
|
|
|
|
p := page{Domain: domain, Path: path}
|
2019-02-19 00:23:44 +08:00
|
|
|
if err := row.Scan(&p.IsLocked, &p.CommentCount, &p.StickyCommentHex, &p.Title); err != nil {
|
2018-07-05 13:06:52 +08:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
// If there haven't been any comments, there won't be a record for this
|
|
|
|
// page. The sane thing to do is return defaults.
|
|
|
|
// TODO: the defaults are hard-coded in two places: here and the schema
|
|
|
|
p.IsLocked = false
|
2018-09-23 12:40:06 +08:00
|
|
|
p.CommentCount = 0
|
2018-12-19 07:57:32 +08:00
|
|
|
p.StickyCommentHex = "none"
|
2019-02-19 00:23:44 +08:00
|
|
|
p.Title = ""
|
2018-07-05 13:06:52 +08:00
|
|
|
} else {
|
|
|
|
logger.Errorf("error scanning page: %v", err)
|
|
|
|
return page{}, errorInternal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|