actions.gno
2.23 Kb ยท 115 lines
1package blog
2
3import (
4 "chain/runtime"
5 "strings"
6
7 "gno.land/p/lou/blog"
8 "gno.land/p/moul/md"
9 "gno.land/p/moul/txlink"
10)
11
12func CreatePost(_ realm, slug, title, body, publicationDate, authors, tags string) {
13 authorsField := strings.Split(authors, " ")
14 tagsField := strings.Split(tags, " ")
15
16 post, err := blog.NewPost(
17 slug,
18 title,
19 body,
20 publicationDate,
21 runtime.OriginCaller().String(),
22 authorsField,
23 tagsField,
24 )
25 if err != nil {
26 panic(err)
27 }
28 actionLinks := md.Link("Like", txlink.Call("LikePostBySlug", "slug", post.Slug()))
29 post.SetPreviewFooter(actionLinks + "\n\n")
30 if err := myBlog.AddPost(post); err != nil {
31 panic(err)
32 }
33}
34
35func UpdatePost(_ realm, id, slug, title, body, publicationDate, authors, tags string) {
36 var err error
37 var post *blog.Post
38 authorsField := strings.Split(authors, " ")
39 tagsField := strings.Split(tags, " ")
40
41 _, err = myBlog.GetPostById(id)
42 if err != nil {
43 panic(err)
44 }
45 post, err = blog.NewPost(
46 slug,
47 title,
48 body,
49 publicationDate,
50 runtime.OriginCaller().String(),
51 authorsField,
52 tagsField,
53 )
54 if err != nil {
55 panic(err)
56 }
57 if err := myBlog.UpdatePostById(id, post); err != nil {
58 panic(err)
59 }
60}
61
62func UpdatePostBySlug(_ realm, slug, title, body, publicationDate, authors, tags string) {
63 var err error
64 var post *blog.Post
65 authorsField := strings.Split(authors, " ")
66 tagsField := strings.Split(tags, " ")
67
68 _, err = myBlog.GetPostBySlug(slug)
69 if err != nil {
70 panic(err)
71 }
72 post, err = blog.NewPost(
73 slug,
74 title,
75 body,
76 publicationDate,
77 runtime.OriginCaller().String(),
78 authorsField,
79 tagsField,
80 )
81 if err != nil {
82 panic(err)
83 }
84 if err := myBlog.UpdatePostBySlug(slug, post); err != nil {
85 panic(err)
86 }
87}
88
89func DeletePost(_ realm, slug string) {
90 if err := myBlog.DeletePostBySlug(slug); err != nil {
91 panic(err)
92 }
93}
94
95func ToggleLikePostBySlug(_ realm, slug string) {
96 if err := myBlog.LikePostBySlug(slug); err != nil {
97 panic(err)
98 }
99}
100
101func AddCommentToPostBySlug(_ realm, slug, comment string) {
102 var err error
103 post, err := myBlog.GetPostBySlug(slug)
104 if err != nil {
105 panic(err)
106 }
107
108 comm, err := blog.NewComment(runtime.PreviousRealm().Address().String(), comment)
109 if err != nil {
110 panic(err)
111 }
112 if err := post.AddComment(comm); err != nil {
113 panic(err)
114 }
115}