-
Notifications
You must be signed in to change notification settings - Fork 0
/
post.go
42 lines (33 loc) · 793 Bytes
/
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
package server
import (
"github.com/gin-gonic/gin"
"github.com/luqmansen/blogo/pkg/blogo"
"net/http"
)
func (h handler) GetPost(c *gin.Context) {
posts := h.postService.GetPostMany(10, 0)
c.JSON(http.StatusOK, posts)
}
func (h handler) CreatePost(c *gin.Context) {
var postRequest PostRequest
if err := c.BindJSON(&postRequest); err != nil {
panic(err)
}
var post blogo.Post
marshal(&postRequest, &post)
err := h.postService.CreatePost(&post)
if err != nil {
panic(err)
}
c.JSON(http.StatusCreated, post)
}
func (h handler) FindPostByID(c *gin.Context, postId PostId) {
p := h.postService.GetPostByID(blogo.PostId(postId))
if p == nil {
c.AbortWithStatus(http.StatusNotFound)
return
}
var resp PostResponse
marshal(&p, &resp)
c.JSON(http.StatusOK, resp)
}