generated from amitshekhariitbhu/go-backend-clean-architecture
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment_controller.go
118 lines (96 loc) · 3.12 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
package controller
import (
"github.com/gin-gonic/gin"
"github.com/hongdangcseiu/go-back-end/bootstrap"
"github.com/hongdangcseiu/go-back-end/domain"
"go.mongodb.org/mongo-driver/bson/primitive"
"net/http"
)
type CommentController struct {
CommentUsercase domain.CommentUsecase
Env *bootstrap.Env
}
func (cc *CommentController) Create(c *gin.Context) {
var request domain.Comment
err := c.ShouldBind(&request)
if err != nil {
c.JSON(http.StatusBadRequest, domain.ErrorResponse{Message: err.Error()})
return
}
userID := c.GetString("x-user-id")
postID := c.Param("postID")
timeString := bootstrap.GetTimeNow()
comment := domain.Comment{
ID: primitive.NewObjectID(),
Content: request.Content,
DateCreate: timeString,
}
comment.UserID, err = primitive.ObjectIDFromHex(userID)
if err != nil {
c.JSON(http.StatusBadRequest, domain.ErrorResponse{Message: err.Error()})
return
}
comment.PostID, err = primitive.ObjectIDFromHex(postID)
if err != nil {
c.JSON(http.StatusBadRequest, domain.ErrorResponse{Message: err.Error()})
return
}
err = cc.CommentUsercase.Create(c, &comment)
if err != nil {
c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: err.Error()})
return
}
c.JSON(http.StatusOK, domain.SuccessResponse{
Message: "Comment created successfully",
})
}
func (cc *CommentController) GetCommentByPostID(c *gin.Context) {
postID := c.Param("id")
comments, err := cc.CommentUsercase.GetCommentByPostID(c, postID)
if err != nil {
c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: err.Error()})
return
}
c.JSON(http.StatusOK, comments)
}
func (cc *CommentController) Delete(c *gin.Context) {
userID := c.GetString("x-user-id")
commentID := c.Param("id")
commentToDelete, err := cc.CommentUsercase.GetCommentByID(c, commentID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Comment not found"})
return
}
if commentToDelete.UserID.Hex() != userID {
c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: "Not Authorized!!! "})
return
}
if err := cc.CommentUsercase.Delete(c, &commentToDelete); err != nil {
c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Post Delete successfully"})
}
func (cc *CommentController) Edit(c *gin.Context) {
userID := c.GetString("x-user-id")
commentID := c.Param("id")
var comment domain.Comment
if err := c.BindJSON(&comment); err != nil {
c.JSON(http.StatusBadRequest, domain.ErrorResponse{Message: err.Error()})
return
}
commentToEdit, err := cc.CommentUsercase.GetCommentByID(c, commentID)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "Comment not found"})
return
}
if commentToEdit.UserID.Hex() != userID {
c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: "Not Authorized!!! "})
return
}
if err := cc.CommentUsercase.Edit(c, commentID, &comment); err != nil {
c.JSON(http.StatusInternalServerError, domain.ErrorResponse{Message: err.Error()})
return
}
c.JSON(http.StatusOK, gin.H{"message": "Comment updated successfully"})
}