-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommentService.go
162 lines (137 loc) · 3.67 KB
/
CommentService.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
156
157
158
159
160
161
162
package services
import (
"context"
"time"
"github.com/gocraft/dbr/v2"
"github.com/rzknugraha/zorro-mark/helpers"
"github.com/rzknugraha/zorro-mark/infrastructures"
"github.com/rzknugraha/zorro-mark/models"
"github.com/rzknugraha/zorro-mark/repositories"
"github.com/sirupsen/logrus"
)
// ICommentService is
type ICommentService interface {
GetCommentByDocID(ctx context.Context, docID int) (res *helpers.JSONResponse, err error)
StoreComment(ctx context.Context, userID int, dataComments models.Comment) (res *helpers.JSONResponse, err error)
}
// CommentService is
type CommentService struct {
CommentRepository repositories.ICommentRepository
DocumentsRepository repositories.IDocumentsRepository
DB infrastructures.ISQLConnection
}
// InitCommentService init
func InitCommentService() *CommentService {
commentRepositories := new(repositories.CommentRepository)
commentRepositories.DB = &infrastructures.SQLConnection{}
documentsRepositories := new(repositories.DocumentsRepository)
documentsRepositories.DB = &infrastructures.SQLConnection{}
CommentService := new(CommentService)
CommentService.CommentRepository = commentRepositories
CommentService.DocumentsRepository = documentsRepositories
return CommentService
}
// GetCommentByDocID is
func (s *CommentService) GetCommentByDocID(ctx context.Context, docID int) (res *helpers.JSONResponse, err error) {
// init conditionDoc
conditionDoc := map[string]interface{}{
"id": docID,
}
_, err = s.DocumentsRepository.GetDoc(ctx, conditionDoc)
if err != nil {
if err == dbr.ErrNotFound {
res = &helpers.JSONResponse{
Code: 4400,
Message: "Document not found for comment",
Data: nil,
}
} else {
res = &helpers.JSONResponse{
Code: 5500,
Message: "Error Get Comment",
Data: nil,
}
}
return
}
// init condition
condition := map[string]interface{}{
"id_document": docID,
}
dataComments, err := s.CommentRepository.GetCommentByDocID(ctx, condition)
if err != nil {
res = &helpers.JSONResponse{
Code: 5500,
Message: "Error Get Comment",
Data: dataComments,
}
return
}
if len(dataComments) > 0 {
res = &helpers.JSONResponse{
Code: 2200,
Message: "Success",
Data: dataComments,
}
} else {
res = &helpers.JSONResponse{
Code: 4400,
Message: "Not Found",
Data: dataComments,
}
}
return
}
// StoreComment to store comment from user post
func (s *CommentService) StoreComment(ctx context.Context, userID int, dataComments models.Comment) (res *helpers.JSONResponse, err error) {
dataComments.IDUser = userID
TimeNow := time.Now()
dataComments.CreatedAt = TimeNow.Format("2006-01-02 15:04:05")
// init conditionDoc
conditionDoc := map[string]interface{}{
"id": dataComments.IDDocument,
}
_, err = s.DocumentsRepository.GetDoc(ctx, conditionDoc)
if err != nil {
if err == dbr.ErrNotFound {
res = &helpers.JSONResponse{
Code: 4400,
Message: "Document not found for comment",
Data: nil,
}
} else {
res = &helpers.JSONResponse{
Code: 5500,
Message: "Error Get Comment",
Data: nil,
}
}
return
}
tx, err := s.CommentRepository.Tx()
if err != nil {
logrus.WithFields(logrus.Fields{
"code": 5500,
"error": err,
"data": dataComments,
}).Error("[Service StoreComment] error create tx")
return
}
defer tx.RollbackUnlessCommitted()
_, err = s.CommentRepository.StoreComment(ctx, tx, dataComments)
if err != nil {
res = &helpers.JSONResponse{
Code: 5500,
Message: "Error When Store",
Data: dataComments,
}
return
}
res = &helpers.JSONResponse{
Code: 2200,
Message: "Success",
Data: nil,
}
tx.Commit()
return
}