-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.go
155 lines (127 loc) · 3.99 KB
/
handlers.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package actions
import (
"log"
"strconv"
buffaloModels "github.com/evgesoch/gofwc/backend/buffalo/models"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/buffalo/render"
packer "github.com/gobuffalo/packr"
)
// Get All Posts
func GetAllPosts(c buffalo.Context) error {
allPosts, err := buffaloModels.GetAllPosts()
if err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(GetAll): Database error, can't fetch posts."))
}
return c.Render(200, r.JSON(allPosts))
}
// Get a Post
func GetPost(c buffalo.Context) error {
postID, err := strconv.Atoi(c.Param("postID"))
if err != nil {
log.Println(err)
return c.Render(400, r.String("PostController(Get): Can't convert URI postID param to int."))
}
post, err := buffaloModels.GetPostByID(postID)
if err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(Get): Database error, can't fetch the post."))
}
if post.ID == 0 {
return c.Render(404, r.String("PostController(Get): This Post doesn't exist in the database."))
}
return c.Render(200, r.JSON(post))
}
// Create a Post
func CreatePost(c buffalo.Context) error {
newPost := new(buffaloModels.Post)
if err := c.Bind(newPost); err != nil {
log.Println(err)
return c.Render(400, r.String("PostController(Post): Cannot parse JSON request body."))
}
newPostID, err := buffaloModels.CreatePost(newPost.Text)
if err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(Post): Database error, can't create new post."))
}
return c.Render(201, r.JSON(map[string]interface{}{
"postID": newPostID,
}),
)
}
// Update a Post
func UpdatePost(c buffalo.Context) error {
postID, err := strconv.Atoi(c.Param("postID"))
if err != nil {
log.Println(err)
return c.Render(400, r.String("PostController(Put): Can't convert URI postID param to int."))
}
allPosts, err := buffaloModels.GetAllPosts()
if err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(GetAll): Database error, can't fetch posts."))
}
if check := checkIfPostExists(postID, allPosts); !check {
return c.Render(404, r.String("PostController(Put): This post doesn't exist."))
}
newPost := new(buffaloModels.Post)
if err := c.Bind(newPost); err != nil {
log.Println(err)
return c.Render(400, r.String("PostController(Put): Cannot parse JSON request body."))
}
if err := buffaloModels.UpdatePostByID(postID, newPost.Text); err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(Put): Database error, can't update the post."))
}
return c.Render(200, r.JSON(map[string]interface{}{
"post_updated": "yes",
}),
)
}
// Delete a Post
func DeletePost(c buffalo.Context) error {
postID, err := strconv.Atoi(c.Param("postID"))
if err != nil {
log.Println(err)
return c.Render(400, r.String("PostController(Delete): Can't convert URI postID param to int."))
}
allPosts, err := buffaloModels.GetAllPosts()
if err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(GetAll): Database error, can't fetch posts."))
}
if check := checkIfPostExists(postID, allPosts); !check {
return c.Render(404, r.String("PostController(Delete): This post doesn't exist."))
}
if err := buffaloModels.DeletePostByID(postID); err != nil {
log.Println(err)
return c.Render(500, r.String("PostController(Delete): Database error, can't delete the post."))
}
return c.Render(204, r.JSON(map[string]interface{}{
"post_deleted": "yes",
}),
)
}
// Serve the index page
func GetIndexPage(c buffalo.Context) error {
rn := render.New(render.Options{
DefaultContentType: "text/html",
HTMLLayout: "index.html",
TemplatesBox: packer.NewBox("../../../frontend"),
})
return c.Render(200, rn.HTML("index"))
}
// Check if a Post exists in the database by ID
func checkIfPostExists(postID int, postsSlice []*buffaloModels.Post) bool {
arr := make([]int, 0)
for _, v := range postsSlice {
arr = append(arr, v.ID)
}
for _, v := range arr {
if postID == v {
return true
}
}
return false
}