Skip to content

Commit

Permalink
fix(blog): remove function (#2154)
Browse files Browse the repository at this point in the history
<!-- please provide a detailed description of the changes made in this
pull request. -->

## Description

This PR adds better removing to the blog `Remove` function.

<details><summary>Contributors' checklist...</summary>

- [x] Added new tests, or not needed, or not feasible
- [x] Provided an example (e.g. screenshot) to aid review or the PR is
self-explanatory
- [x] Updated the official documentation or not needed
- [x] No breaking changes were made, or a `BREAKING CHANGE: xxx` message
was included in the description
- [x] Added references to related issues and PRs
- [ ] Provided any useful hints for running manual tests
- [ ] Added new benchmarks to [generated
graphs](https://gnoland.github.io/benchmarks), if any. More info
[here](https://github.com/gnolang/gno/blob/master/.benchmarks/README.md).
</details>
  • Loading branch information
leohhhn authored May 20, 2024
1 parent 228e9d0 commit a7ec2ab
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
17 changes: 11 additions & 6 deletions examples/gno.land/p/demo/blog/blog.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion examples/gno.land/p/demo/blog/util.gno
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 21 additions & 7 deletions examples/gno.land/r/gnoland/blog/gnoblog_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a7ec2ab

Please sign in to comment.