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 == "" || if p.ID == "" ||
p.Title == "" || p.Title == "" ||
p.Description == "" ||
p.Body == "" || p.Body == "" ||
len(p.Tags) == 0 { 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 return p, nil

View File

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

View File

@ -61,7 +61,7 @@ func (s *draftStore) Set(post Post) error {
body=excluded.body`, body=excluded.body`,
post.ID, post.ID,
post.Title, 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: string(tagsJSON), Valid: len(post.Tags) > 0},
&sql.NullString{String: post.Series, Valid: post.Series != ""}, &sql.NullString{String: post.Series, Valid: post.Series != ""},
post.Body, post.Body,
@ -112,12 +112,12 @@ func (s *draftStore) get(
for rows.Next() { for rows.Next() {
var ( var (
post Post post Post
tags, series sql.NullString description, tags, series sql.NullString
) )
err := rows.Scan( err := rows.Scan(
&post.ID, &post.Title, &post.Description, &tags, &series, &post.ID, &post.Title, &description, &tags, &series,
&post.Body, &post.Body,
) )
@ -125,6 +125,7 @@ func (s *draftStore) get(
return nil, fmt.Errorf("scanning row: %w", err) return nil, fmt.Errorf("scanning row: %w", err)
} }
post.Description = description.String
post.Series = series.String post.Series = series.String
if tags.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 // we will now try updating the post, and ensure it updates properly
post.Title = "something else" post.Title = "something else"
post.Description = "some description"
post.Series = "whatever" post.Series = "whatever"
post.Body = "anything" post.Body = "anything"
post.Tags = []string{"bar", "baz"} 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`, body=excluded.body`,
post.ID, post.ID,
post.Title, post.Title,
post.Description, &sql.NullString{String: post.Description, Valid: len(post.Description) > 0},
&sql.NullString{String: post.Series, Valid: post.Series != ""}, &sql.NullString{String: post.Series, Valid: post.Series != ""},
nowSQL, nowSQL,
post.Body, post.Body,
@ -202,12 +202,12 @@ func (s *store) get(
var ( var (
post StoredPost post StoredPost
series, tag sql.NullString description, tag, series sql.NullString
publishedAt, lastUpdatedAt sql.NullInt64 publishedAt, lastUpdatedAt sql.NullInt64
) )
err := rows.Scan( err := rows.Scan(
&post.ID, &post.Title, &post.Description, &series, &tag, &post.ID, &post.Title, &description, &series, &tag,
&publishedAt, &lastUpdatedAt, &publishedAt, &lastUpdatedAt,
&post.Body, &post.Body,
) )
@ -216,6 +216,7 @@ func (s *store) get(
return nil, fmt.Errorf("scanning row: %w", err) return nil, fmt.Errorf("scanning row: %w", err)
} }
post.Description = description.String
post.Series = series.String post.Series = series.String
if tag.String != "" { if tag.String != "" {

View File

@ -37,10 +37,9 @@ func TestNewID(t *testing.T) {
func testPost(i int) Post { func testPost(i int) Post {
istr := strconv.Itoa(i) istr := strconv.Itoa(i)
return Post{ return Post{
ID: istr, ID: istr,
Title: istr, Title: istr,
Description: istr, Body: istr,
Body: istr,
} }
} }
@ -127,6 +126,7 @@ func TestStore(t *testing.T) {
newNow := h.clock.Now().UTC() newNow := h.clock.Now().UTC()
post.Title = "something else" post.Title = "something else"
post.Description = "some description"
post.Series = "whatever" post.Series = "whatever"
post.Body = "anything" post.Body = "anything"
post.Tags = []string{"bar", "baz"} 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 // SQLDB is a sqlite3 database which can be used by storage interfaces within