Posts can have parameters

This commit is contained in:
Jan-Lukas Else 2020-07-29 18:28:51 +02:00
parent 00add1c312
commit 6c4a9476b4
2 changed files with 31 additions and 4 deletions

View File

@ -24,6 +24,13 @@ func migrateDb() error {
return err
},
},
&migrator.Migration{
Name: "00003",
Func: func(tx *sql.Tx) error {
_, err := tx.Exec("create table post_parameters (path text not null, parameter text not null, value text, primary key (path, parameter));")
return err
},
},
),
)
if err != nil {

View File

@ -10,10 +10,11 @@ import (
var postNotFound = errors.New("post not found")
type post struct {
path string
content string
published string
updated string
path string
content string
published string
updated string
parameters map[string]string
}
func servePost(w http.ResponseWriter, r *http.Request) {
@ -43,9 +44,28 @@ func getPost(path string, context context.Context) (*post, error) {
} else if err != nil {
return nil, err
}
err = queriedPost.fetchParameters(context)
if err != nil {
return nil, err
}
return queriedPost, nil
}
func (p *post) fetchParameters(context context.Context) error {
rows, err := appDb.QueryContext(context, "select parameter, COALESCE(value, '') from post_parameters where path=?", p.path)
if err != nil {
return err
}
p.parameters = make(map[string]string)
for rows.Next() {
var parameter string
var value string
_ = rows.Scan(&parameter, &value)
p.parameters[parameter] = value
}
return nil
}
func allPostPaths() ([]string, error) {
var postPaths []string
rows, err := appDb.Query("select path from posts")