-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathpost.controller.go
119 lines (92 loc) · 3.03 KB
/
post.controller.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
package controllers
import (
"net/http"
"strconv"
"strings"
"github.com/gin-gonic/gin"
"github.com/wpcodevo/golang-mongodb/models"
"github.com/wpcodevo/golang-mongodb/services"
)
type PostController struct {
postService services.PostService
}
func NewPostController(postService services.PostService) PostController {
return PostController{postService}
}
func (pc *PostController) CreatePost(ctx *gin.Context) {
var post *models.CreatePostRequest
if err := ctx.ShouldBindJSON(&post); err != nil {
ctx.JSON(http.StatusBadRequest, err.Error())
return
}
newPost, err := pc.postService.CreatePost(post)
if err != nil {
if strings.Contains(err.Error(), "title already exists") {
ctx.JSON(http.StatusConflict, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusCreated, gin.H{"status": "success", "data": newPost})
}
func (pc *PostController) UpdatePost(ctx *gin.Context) {
postId := ctx.Param("postId")
var post *models.UpdatePost
if err := ctx.ShouldBindJSON(&post); err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
updatedPost, err := pc.postService.UpdatePost(postId, post)
if err != nil {
if strings.Contains(err.Error(), "Id exists") {
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": "success", "data": updatedPost})
}
func (pc *PostController) FindPostById(ctx *gin.Context) {
postId := ctx.Param("postId")
post, err := pc.postService.FindPostById(postId)
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": "success", "data": post})
}
func (pc *PostController) FindPosts(ctx *gin.Context) {
var page = ctx.DefaultQuery("page", "1")
var limit = ctx.DefaultQuery("limit", "10")
intPage, err := strconv.Atoi(page)
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
intLimit, err := strconv.Atoi(limit)
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
posts, err := pc.postService.FindPosts(intPage, intLimit)
if err != nil {
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusOK, gin.H{"status": "success", "results": len(posts), "data": posts})
}
func (pc *PostController) DeletePost(ctx *gin.Context) {
postId := ctx.Param("postId")
err := pc.postService.DeletePost(postId)
if err != nil {
if strings.Contains(err.Error(), "Id exists") {
ctx.JSON(http.StatusNotFound, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusBadGateway, gin.H{"status": "fail", "message": err.Error()})
return
}
ctx.JSON(http.StatusNoContent, nil)
}