-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment-controller.go
124 lines (98 loc) · 2.95 KB
/
comment-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
120
121
122
123
124
package controller
import (
"github.com/hacktiv8-fp-golang/final-project-02/internal/helper"
"github.com/hacktiv8-fp-golang/final-project-02/internal/model"
"github.com/hacktiv8-fp-golang/final-project-02/internal/service"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
func CreateComment(context *gin.Context) {
var comment model.Comment
if err := context.ShouldBindJSON(&comment); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.JSON(errorHandler.Status(), errorHandler)
return
}
userData := context.MustGet("userData").(jwt.MapClaims)
userID := uint(userData["id"].(float64))
createComment, err := service.CommentService.CreateComment(&comment, userID)
if err != nil {
context.JSON(err.Status(), err)
return
}
context.JSON(http.StatusCreated, gin.H{
"id": createComment.ID,
"message": createComment.Message,
"photo_id": createComment.PhotoID,
"user_id": createComment.UserID,
"created_at": createComment.CreatedAt,
})
}
func GetComment(context *gin.Context) {
userData := context.MustGet("userData").(jwt.MapClaims)
userId := uint(userData["id"].(float64))
results, err := service.CommentService.GetComment(userId)
if err != nil {
context.JSON(err.Status(), err)
return
}
comments := make([]gin.H, 0, len(results))
for _, result := range results {
comment := gin.H{
"id": result.ID,
"message": result.Message,
"photo_id": result.PhotoID,
"user_id": result.UserID,
"updated_at": result.UpdatedAt,
"created_at": result.CreatedAt,
"User": gin.H{
"id": result.User.ID,
"email": result.User.Email,
"username": result.User.Username,
},
"Photo": gin.H{
"id": result.Photo.ID,
"title": result.Photo.Title,
"caption": result.Photo.Caption,
"photo_url": result.Photo.PhotoURL,
"user_id": result.Photo.UserID,
},
}
comments = append(comments, comment)
}
context.JSON(http.StatusOK, comments)
}
func UpdateComment(context *gin.Context) {
var message model.CommentUpdate
if err := context.ShouldBindJSON(&message); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.JSON(errorHandler.Status(), errorHandler)
return
}
ID, _ := helper.GetIdParam(context, "commentId")
commentID := uint(ID)
Update, err := service.CommentService.UpdateComment(&message, commentID)
if err != nil {
context.JSON(err.Status(), err)
return
}
context.JSON(http.StatusOK, gin.H{
"id": Update.ID,
"message": Update.Message,
"user_id": Update.UserID,
"updated_at": Update.CreatedAt,
})
}
func DeleteComment(context *gin.Context) {
ID, _ := helper.GetIdParam(context, "commentId")
commentID := uint(ID)
_, err := service.CommentService.DeleteComment(commentID)
if err != nil {
context.JSON(err.Status(), err)
return
}
context.JSON(http.StatusOK, gin.H{
"message": "Your comment has been successfully deleted",
})
}