mirror of https://github.com/jlelse/GoBlog
Posts can have parameters
This commit is contained in:
parent
00add1c312
commit
6c4a9476b4
|
@ -24,6 +24,13 @@ func migrateDb() error {
|
||||||
return err
|
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 {
|
if err != nil {
|
||||||
|
|
28
posts.go
28
posts.go
|
@ -10,10 +10,11 @@ import (
|
||||||
var postNotFound = errors.New("post not found")
|
var postNotFound = errors.New("post not found")
|
||||||
|
|
||||||
type post struct {
|
type post struct {
|
||||||
path string
|
path string
|
||||||
content string
|
content string
|
||||||
published string
|
published string
|
||||||
updated string
|
updated string
|
||||||
|
parameters map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
func servePost(w http.ResponseWriter, r *http.Request) {
|
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 {
|
} else if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
err = queriedPost.fetchParameters(context)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return queriedPost, nil
|
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(¶meter, &value)
|
||||||
|
p.parameters[parameter] = value
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func allPostPaths() ([]string, error) {
|
func allPostPaths() ([]string, error) {
|
||||||
var postPaths []string
|
var postPaths []string
|
||||||
rows, err := appDb.Query("select path from posts")
|
rows, err := appDb.Query("select path from posts")
|
||||||
|
|
Loading…
Reference in New Issue