diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2022-10-12 23:43:31 +0200 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2022-10-12 23:43:31 +0200 |
commit | d9570411134273d690e783748dd572696fc14c6f (patch) | |
tree | ca9d0780bdc2708e190a2194b751966546935e39 | |
parent | d959985ed0d3509d1369c15bf0c87c35b47eb1db (diff) |
Make description an optional field
-rw-r--r-- | src/http/posts.go | 3 | ||||
-rw-r--r-- | src/http/tpl/post.html | 2 | ||||
-rw-r--r-- | src/post/draft_post.go | 9 | ||||
-rw-r--r-- | src/post/draft_post_test.go | 1 | ||||
-rw-r--r-- | src/post/post.go | 7 | ||||
-rw-r--r-- | src/post/post_test.go | 8 | ||||
-rw-r--r-- | src/post/sql.go | 14 |
7 files changed, 31 insertions, 13 deletions
diff --git a/src/http/posts.go b/src/http/posts.go index c3f6363..09daac4 100644 --- a/src/http/posts.go +++ b/src/http/posts.go @@ -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 diff --git a/src/http/tpl/post.html b/src/http/tpl/post.html index 23500eb..f10ce94 100644 --- a/src/http/tpl/post.html +++ b/src/http/tpl/post.html @@ -4,9 +4,11 @@ {{ .Payload.Title }} </h1> +{{ if ne .Payload.Description "" }} <p> <em>- {{ .Payload.Description }}</em> </p> +{{ end }} <hr/> diff --git a/src/post/draft_post.go b/src/post/draft_post.go index 61283c3..0cb2ee1 100644 --- a/src/post/draft_post.go +++ b/src/post/draft_post.go @@ -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, @@ -112,12 +112,12 @@ func (s *draftStore) get( for rows.Next() { var ( - post Post - tags, series sql.NullString + post Post + 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 != "" { diff --git a/src/post/draft_post_test.go b/src/post/draft_post_test.go index f404bb0..fde5f95 100644 --- a/src/post/draft_post_test.go +++ b/src/post/draft_post_test.go @@ -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"} diff --git a/src/post/post.go b/src/post/post.go index 03bce6c..1bdf03e 100644 --- a/src/post/post.go +++ b/src/post/post.go @@ -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 != "" { diff --git a/src/post/post_test.go b/src/post/post_test.go index c7f9cdc..7e87200 100644 --- a/src/post/post_test.go +++ b/src/post/post_test.go @@ -37,10 +37,9 @@ func TestNewID(t *testing.T) { func testPost(i int) Post { istr := strconv.Itoa(i) return Post{ - ID: istr, - Title: istr, - Description: istr, - Body: istr, + ID: istr, + Title: 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"} diff --git a/src/post/sql.go b/src/post/sql.go index c768c9a..786ea63 100644 --- a/src/post/sql.go +++ b/src/post/sql.go @@ -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 |