/
post.go
109 lines (93 loc) · 3.05 KB
/
post.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package handlers
import (
"fmt"
"net/http"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/csv"
"github.com/getfider/fider/app/pkg/markdown"
"github.com/getfider/fider/app/pkg/web"
)
// Index is the default home page
func Index() web.HandlerFunc {
return func(c *web.Context) error {
c.SetCanonicalURL("")
searchPosts := &query.SearchPosts{
Query: c.QueryParam("query"),
View: c.QueryParam("view"),
Limit: c.QueryParam("limit"),
Tags: c.QueryParamAsArray("tags"),
}
getAllTags := &query.GetAllTags{}
countPerStatus := &query.CountPostPerStatus{}
if err := bus.Dispatch(c, searchPosts, getAllTags, countPerStatus); err != nil {
return c.Failure(err)
}
description := ""
if c.Tenant().WelcomeMessage != "" {
description = markdown.PlainText(c.Tenant().WelcomeMessage)
} else {
description = "We'd love to hear what you're thinking about. What can we do better? This is the place for you to vote, discuss and share posts."
}
return c.Page(http.StatusOK, web.Props{
Page: "Home/Home.page",
Description: description,
Data: web.Map{
"posts": searchPosts.Result,
"tags": getAllTags.Result,
"countPerStatus": countPerStatus.Result,
},
})
}
}
// PostDetails shows details of given Post by id
func PostDetails() web.HandlerFunc {
return func(c *web.Context) error {
number, err := c.ParamAsInt("number")
if err != nil {
return c.NotFound()
}
getPost := &query.GetPostByNumber{Number: number}
if err := bus.Dispatch(c, getPost); err != nil {
return c.Failure(err)
}
if c.Param("slug") != getPost.Result.Slug {
return c.Redirect(fmt.Sprintf("/posts/%d/%s", getPost.Result.Number, getPost.Result.Slug))
}
isSubscribed := &query.UserSubscribedTo{PostID: getPost.Result.ID}
getComments := &query.GetCommentsByPost{Post: getPost.Result}
getAllTags := &query.GetAllTags{}
listVotes := &query.ListPostVotes{PostID: getPost.Result.ID, Limit: 6, IncludeEmail: false}
getAttachments := &query.GetAttachments{Post: getPost.Result}
if err := bus.Dispatch(c, getAllTags, getComments, listVotes, isSubscribed, getAttachments); err != nil {
return c.Failure(err)
}
return c.Page(http.StatusOK, web.Props{
Page: "ShowPost/ShowPost.page",
Title: getPost.Result.Title,
Description: markdown.PlainText(getPost.Result.Description),
Data: web.Map{
"comments": getComments.Result,
"subscribed": isSubscribed.Result,
"post": getPost.Result,
"tags": getAllTags.Result,
"votes": listVotes.Result,
"attachments": getAttachments.Result,
},
})
}
}
// ExportPostsToCSV returns a CSV with all posts
func ExportPostsToCSV() web.HandlerFunc {
return func(c *web.Context) error {
allPosts := &query.GetAllPosts{}
if err := bus.Dispatch(c, allPosts); err != nil {
return c.Failure(err)
}
bytes, err := csv.FromPosts(allPosts.Result)
if err != nil {
return c.Failure(err)
}
return c.Attachment("posts.csv", "text/csv", bytes)
}
}