Make description an optional field

This commit is contained in:
Brian Picciano 2022-10-12 23:43:31 +02:00
parent d959985ed0
commit d957041113
7 changed files with 31 additions and 13 deletions

View File

@ -293,10 +293,9 @@ func postFromPostReq(r *http.Request) (post.Post, error) {
if p.ID == "" ||
p.Title == "" ||
p.Description == "" ||
p.Body == "" ||
len(p.Tags) == 0 {
return post.Post{}, errors.New("ID, Title, Description, Tags, and Body are all required")
return post.Post{}, errors.New("ID, Title, Tags, and Body are all required")
}
return p, nil

View File

@ -4,9 +4,11 @@
{{ .Payload.Title }}
</h1>
{{ if ne .Payload.Description "" }}
<p>
<em>- {{ .Payload.Description }}</em>
</p>
{{ end }}
<hr/>

View File

@ -61,7 +61,7 @@ func (s *draftStore) Set(post Post) error {
body=excluded.body`,
post.ID,
post.Title,
post.Description,
&sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
&sql.NullString{String: string(tagsJSON), Valid: len(post.Tags) > 0},
&sql.NullString{String: post.Series, Valid: post.Series != ""},
post.Body,
@ -113,11 +113,11 @@ func (s *draftStore) get(
var (
post Post
tags, series sql.NullString
description, tags, series sql.NullString
)
err := rows.Scan(
&post.ID, &post.Title, &post.Description, &tags, &series,
&post.ID, &post.Title, &description, &tags, &series,
&post.Body,
)
@ -125,6 +125,7 @@ func (s *draftStore) get(
return nil, fmt.Errorf("scanning row: %w", err)
}
post.Description = description.String
post.Series = series.String
if tags.String != "" {

View File

@ -68,6 +68,7 @@ func TestDraftStore(t *testing.T) {
// we will now try updating the post, and ensure it updates properly
post.Title = "something else"
post.Description = "some description"
post.Series = "whatever"
post.Body = "anything"
post.Tags = []string{"bar", "baz"}

View File

@ -115,7 +115,7 @@ func (s *store) Set(post Post, now time.Time) (bool, error) {
body=excluded.body`,
post.ID,
post.Title,
post.Description,
&sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
&sql.NullString{String: post.Series, Valid: post.Series != ""},
nowSQL,
post.Body,
@ -202,12 +202,12 @@ func (s *store) get(
var (
post StoredPost
series, tag sql.NullString
description, tag, series sql.NullString
publishedAt, lastUpdatedAt sql.NullInt64
)
err := rows.Scan(
&post.ID, &post.Title, &post.Description, &series, &tag,
&post.ID, &post.Title, &description, &series, &tag,
&publishedAt, &lastUpdatedAt,
&post.Body,
)
@ -216,6 +216,7 @@ func (s *store) get(
return nil, fmt.Errorf("scanning row: %w", err)
}
post.Description = description.String
post.Series = series.String
if tag.String != "" {

View File

@ -39,7 +39,6 @@ func testPost(i int) Post {
return Post{
ID: istr,
Title: istr,
Description: istr,
Body: istr,
}
}
@ -127,6 +126,7 @@ func TestStore(t *testing.T) {
newNow := h.clock.Now().UTC()
post.Title = "something else"
post.Description = "some description"
post.Series = "whatever"
post.Body = "anything"
post.Tags = []string{"bar", "baz"}

View File

@ -52,6 +52,20 @@ var migrations = &migrate.MemoryMigrationSource{Migrations: []*migrate.Migration
)`,
},
},
{
Id: "3",
Up: []string{
`ALTER TABLE post_drafts RENAME description TO description_old`,
`ALTER TABLE post_drafts ADD COLUMN description TEXT`,
`UPDATE post_drafts AS pd SET description=pd.description_old`,
`ALTER TABLE post_drafts DROP COLUMN description_old`,
`ALTER TABLE posts RENAME description TO description_old`,
`ALTER TABLE posts ADD COLUMN description TEXT`,
`UPDATE posts AS p SET description=p.description_old`,
`ALTER TABLE posts DROP COLUMN description_old`,
},
},
}}
// SQLDB is a sqlite3 database which can be used by storage interfaces within