diff --git a/examples/gno.land/p/demo/blog/blog.gno b/examples/gno.land/p/demo/blog/blog.gno index 11d656db5ee..19c9f3091c5 100644 --- a/examples/gno.land/p/demo/blog/blog.gno +++ b/examples/gno.land/p/demo/blog/blog.gno @@ -175,8 +175,8 @@ func (b *Blog) prepareAndSetPost(post *Post) error { post.Blog = b post.UpdatedAt = time.Now() - trimmedTitleKey := strings.Replace(post.Title, " ", "", -1) - pubDateKey := post.CreatedAt.Format(time.RFC3339) + trimmedTitleKey := getTitleKey(post.Title) + pubDateKey := getPublishedKey(post.CreatedAt) // Cannot have two posts with same title key if _, found := b.PostsAlphabetical.Get(trimmedTitleKey); found { @@ -196,14 +196,19 @@ func (b *Blog) prepareAndSetPost(post *Post) error { } func (b *Blog) RemovePost(slug string) { - _, exists := b.Posts.Get(slug) + p, exists := b.Posts.Get(slug) if !exists { - panic("post with specified slug does not exist") + panic("post with specified slug doesn't exist") } + post := p.(*Post) + + titleKey := getTitleKey(post.Title) + publishedKey := getPublishedKey(post.CreatedAt) + _, _ = b.Posts.Remove(slug) - _, _ = b.PostsAlphabetical.Remove(slug) - _, _ = b.PostsPublished.Remove(slug) + _, _ = b.PostsAlphabetical.Remove(titleKey) + _, _ = b.PostsPublished.Remove(publishedKey) } func (b *Blog) GetPost(slug string) *Post { diff --git a/examples/gno.land/p/demo/blog/util.gno b/examples/gno.land/p/demo/blog/util.gno index b4a8652af72..99b59772082 100644 --- a/examples/gno.land/p/demo/blog/util.gno +++ b/examples/gno.land/p/demo/blog/util.gno @@ -1,7 +1,18 @@ package blog -import "strings" +import ( + "strings" + "time" +) func breadcrumb(parts []string) string { return "# " + strings.Join(parts, " / ") + "\n\n" } + +func getTitleKey(title string) string { + return strings.Replace(title, " ", "", -1) +} + +func getPublishedKey(t time.Time) string { + return t.Format(time.RFC3339) +} diff --git a/examples/gno.land/r/gnoland/blog/gnoblog_test.gno b/examples/gno.land/r/gnoland/blog/gnoblog_test.gno index 03fa398a26d..d375ccad5c1 100644 --- a/examples/gno.land/r/gnoland/blog/gnoblog_test.gno +++ b/examples/gno.land/r/gnoland/blog/gnoblog_test.gno @@ -166,18 +166,32 @@ Published by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq to Gnoland's Blog } { // Test remove functionality - ModAddPost("testSlug1", "testTitle1", "body1", "2022-05-22T13:17:22Z", "moul", "tag1,tag2") + title := "example title" + slug := "testSlug1" + ModAddPost(slug, title, "body1", "2022-05-25T13:17:22Z", "moul", "tag1,tag2") - got := Render("p/testSlug1") + got := Render("") + + if !strings.Contains(got, title) { + t.Errorf("post was not added properly") + } + + postRender := Render("p/" + slug) + + if !strings.Contains(postRender, title) { + t.Errorf("post not rendered properly") + } + + ModRemovePost(slug) + got = Render("") - if got == "404" { - t.Errorf("did not expect 404") + if strings.Contains(got, title) { + t.Errorf("post was not removed") } - ModRemovePost("testSlug1") - got = Render("p/testSlug1") + postRender = Render("p/" + slug) - assertMDEquals(t, got, "404") + assertMDEquals(t, postRender, "404") } // TODO: pagination.