Skip to content

Commit

Permalink
chore: add p/demo/blog.EditBlog
Browse files Browse the repository at this point in the history
Signed-off-by: Manfred Touron <[email protected]>
  • Loading branch information
moul committed Oct 30, 2022
1 parent 9f287e9 commit 410d974
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
58 changes: 39 additions & 19 deletions examples/gno.land/p/demo/blog/blog.gno
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,53 @@ func (b Blog) Render(path string) string {
}

func (b *Blog) NewPost(author std.Address, slug, title, body string, tags []string) error {
title = strings.TrimSpace(title)
body = strings.TrimSpace(body)
_, found := b.Posts.Get(slug)
if found {
return errors.New("slug already exists.")
}

if title == "" {
post := Post{
Author: author,
Slug: slug,
Title: title,
Body: body,
Tags: tags,
CreatedAt: time.Now(),
}
return b.prepareAndSetPost(&post)
}

func (b *Blog) UpdatePost(slug, title, body string, tags []string) error {
post, found := b.Posts.Get(slug)
if !found {
return errors.New("slug does not exist.")
}
post.Slug = slug
post.Title = title
post.Body = body
post.Tags = tags
return b.prepareAndSetPost(&post)
}

func (b *Blog) prepareAndSetPost(post *Post) error {
post.Title = strings.TrimSpace(post.Title)
post.Body = strings.TrimSpace(post.Body)

if post.Title == "" {
return errors.New("title is missing.")
}
if body == "" {
if post.Body == "" {
return errors.New("body is missing.")
}
if slug == "" {
if post.Slug == "" {
return errors.New("slug is missing.")
}
post := b.GetPost(slug)
if post != nil {
return errors.New("slug already exists.")
}

// more input sanitization?
b.Posts.Set(slug, &Post{
Blog: b,
Slug: slug,
Title: title,
Body: body,
CreatedAt: time.Now(),
Author: author,
Tags: tags,
})

post.Blog = b
post.UpdatedAt = time.Now()

b.Posts.Set(post.Slug, post)
return nil
}

Expand Down
10 changes: 10 additions & 0 deletions examples/gno.land/r/gnoland/blog/gnoblog.gno
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ func ModAddPost(slug, title, body, tags string) {
checkErr(err)
}

func ModEditPost(slug, title, body, tags string) {
assertIsModerator()

tagList := strings.Split(tags, ",")
err := b.EditPost(slug, title, body, tagList)
if err != nil {
panic(err)
}
}

func AddComment(postSlug, comment string) {
assertIsCommenter()
assertNotInPause()
Expand Down

0 comments on commit 410d974

Please sign in to comment.