diff --git a/cmd/owl/web/editor_handler.go b/cmd/owl/web/editor_handler.go index c095e65..29b17f8 100644 --- a/cmd/owl/web/editor_handler.go +++ b/cmd/owl/web/editor_handler.go @@ -4,6 +4,7 @@ import ( "fmt" "h4kor/owl-blogs" "net/http" + "strings" "sync" "time" @@ -203,6 +204,11 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http content := r.Form.Get("content") draft := r.Form.Get("draft") + // recipe values + recipe_yield := r.Form.Get("yield") + recipe_ingredients := r.Form.Get("ingredients") + recipe_duration := r.Form.Get("duration") + // conditional values reply_url := r.Form.Get("reply_url") bookmark_url := r.Form.Get("bookmark_url") @@ -216,7 +222,7 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http w.Write([]byte(html)) return } - if (post_type == "article" || post_type == "page") && title == "" { + if (post_type == "article" || post_type == "page" || post_type == "recipe") && title == "" { html, _ := owl.RenderUserError(user, owl.ErrorMessage{ Error: "Missing Title", Message: "Articles and Pages must have a title", @@ -257,6 +263,11 @@ func userEditorPostHandler(repo *owl.Repository) func(http.ResponseWriter, *http Bookmark: owl.BookmarkData{ Url: bookmark_url, }, + Recipe: owl.RecipeData{ + Yield: recipe_yield, + Ingredients: strings.Split(recipe_ingredients, "\n"), + Duration: recipe_duration, + }, }, content) if err != nil { diff --git a/cmd/owl/web/editor_test.go b/cmd/owl/web/editor_test.go index edd354d..1086d45 100644 --- a/cmd/owl/web/editor_test.go +++ b/cmd/owl/web/editor_test.go @@ -243,3 +243,40 @@ func TestEditorSendsWebmentions(t *testing.T) { assertions.AssertEqual(t, repo.HttpClient.(*CountMockHttpClient).InvokedPostForm, 1) } + +func TestEditorPostWithSessionRecipe(t *testing.T) { + repo, user := getSingleUserTestRepo() + user.ResetPassword("testpassword") + sessionId := user.CreateNewSession() + + csrfToken := "test_csrf_token" + + // Create Request and Response + form := url.Values{} + form.Add("type", "recipe") + form.Add("title", "testtitle") + form.Add("yield", "2") + form.Add("duration", "1 hour") + form.Add("ingredients", "water\nwheat") + form.Add("content", "testcontent") + form.Add("csrf_token", csrfToken) + + req, err := http.NewRequest("POST", user.EditorUrl(), strings.NewReader(form.Encode())) + req.Header.Add("Content-Type", "application/x-www-form-urlencoded") + req.Header.Add("Content-Length", strconv.Itoa(len(form.Encode()))) + req.AddCookie(&http.Cookie{Name: "csrf_token", Value: csrfToken}) + req.AddCookie(&http.Cookie{Name: "session", Value: sessionId}) + assertions.AssertNoError(t, err, "Error creating request") + rr := httptest.NewRecorder() + router := main.SingleUserRouter(&repo) + router.ServeHTTP(rr, req) + + posts, _ := user.AllPosts() + assertions.AssertEqual(t, len(posts), 1) + post := posts[0] + + assertions.AssertLen(t, post.Meta().Recipe.Ingredients, 2) + + assertions.AssertStatus(t, rr, http.StatusFound) + assertions.AssertEqual(t, rr.Header().Get("Location"), post.FullUrl()) +} diff --git a/embed/editor/editor.html b/embed/editor/editor.html index 6474373..5221919 100644 --- a/embed/editor/editor.html +++ b/embed/editor/editor.html @@ -54,6 +54,35 @@ +
+ Write Recipe +
+

Create new Recipe

+ + + + + + + + + + + + + + + + + + + + +

+ +
+
+
Bookmark
diff --git a/embed/recipe/detail.html b/embed/recipe/detail.html new file mode 100644 index 0000000..8537f74 --- /dev/null +++ b/embed/recipe/detail.html @@ -0,0 +1,59 @@ +
+
+

{{.Title}}

+ + # + Published: + + {{ if .Post.User.Config.AuthorName }} + by + + {{ if .Post.User.AvatarUrl }} + + {{ end }} + {{.Post.User.Config.AuthorName}} + + {{ end }} + +
+
+
+ + +
+ + + {{ if .Post.Meta.Recipe.Yield }} + Servings: {{ .Post.Meta.Recipe.Yield }} + {{ if .Post.Meta.Recipe.Duration }}, {{end}} + + {{ end }} + + {{ if .Post.Meta.Recipe.Duration }} + Prep Time: + {{ end }} + + +

Ingredients

+ +
    + {{ range $ingredient := .Post.Meta.Recipe.Ingredients }} +
  • + {{ $ingredient }} +
  • + {{ end }} +
+ +

Instructions

+ +
+ {{.Content}} +
+
+ +
\ No newline at end of file diff --git a/post.go b/post.go index 2d33fcc..e71ffe3 100644 --- a/post.go +++ b/post.go @@ -72,6 +72,12 @@ type BookmarkData struct { Text string `yaml:"text"` } +type RecipeData struct { + Yield string `yaml:"yield"` + Duration string `yaml:"duration"` + Ingredients []string `yaml:"ingredients"` +} + type PostMeta struct { Type string `yaml:"type"` Title string `yaml:"title"` @@ -81,6 +87,7 @@ type PostMeta struct { Draft bool `yaml:"draft"` Reply ReplyData `yaml:"reply"` Bookmark BookmarkData `yaml:"bookmark"` + Recipe RecipeData `yaml:"recipe"` } func (pm PostMeta) FormattedDate() string { @@ -96,6 +103,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { Draft bool `yaml:"draft"` Reply ReplyData `yaml:"reply"` Bookmark BookmarkData `yaml:"bookmark"` + Recipe RecipeData `yaml:"recipe"` } type S struct { Date string `yaml:"date"` @@ -120,6 +128,7 @@ func (pm *PostMeta) UnmarshalYAML(unmarshal func(interface{}) error) error { pm.Draft = t.Draft pm.Reply = t.Reply pm.Bookmark = t.Bookmark + pm.Recipe = t.Recipe possibleFormats := []string{ "2006-01-02", diff --git a/renderer_test.go b/renderer_test.go index ee49d80..225cea7 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -478,3 +478,28 @@ func TestRenderFooterMenuPost(t *testing.T) { assertions.AssertContains(t, result, "Test Entry") assertions.AssertContains(t, result, post.UrlPath()) } + +func TestRecipePost(t *testing.T) { + repo := getTestRepo(owl.RepoConfig{}) + user, _ := repo.CreateUser("testuser") + post, err := user.CreateNewPost(owl.PostMeta{ + Type: "recipe", + Title: "test recipe", + Recipe: owl.RecipeData{ + Yield: "1 loaf", + Ingredients: []string{ + "1 cup flour", + "1 cup water", + }, + Duration: "1 hour", + }, + }, "") + assertions.AssertNoError(t, err, "Error creating post") + + result, err := owl.RenderPost(post) + assertions.AssertNoError(t, err, "Error rendering post") + assertions.AssertContains(t, result, "1 loaf") + assertions.AssertContains(t, result, "1 cup flour") + assertions.AssertContains(t, result, "1 cup water") + assertions.AssertContains(t, result, "1 hour") +}