-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdb_review.go
288 lines (254 loc) · 8.51 KB
/
db_review.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"context"
"errors"
"strings"
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
// searchReviewV2 함수는 Review를 검색한다.
func searchReviewV2(client *mongo.Client, searchword string) ([]Review, error) {
var results []Review
if searchword == "" {
return results, nil
}
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
allQueries := []bson.M{}
for _, word := range strings.Split(searchword, " ") {
if len(word) < 2 { // 한글자의 단어는 무시한다.
continue
}
orQueries := []bson.M{}
if strings.HasPrefix(word, "createtime:") {
if strings.TrimPrefix(word, "createtime:") != "" {
orQueries = append(orQueries, bson.M{"createtime": &primitive.Regex{Pattern: strings.TrimPrefix(word, "createtime:")}})
orQueries = append(orQueries, bson.M{"createtime": &primitive.Regex{Pattern: strings.TrimPrefix(word, "createtime:")}})
}
} else if strings.HasPrefix(word, "status:") {
orQueries = append(orQueries, bson.M{"status": &primitive.Regex{Pattern: strings.TrimPrefix(word, "status:")}})
} else if strings.HasPrefix(word, "itemstatus:") {
orQueries = append(orQueries, bson.M{"itemstatus": &primitive.Regex{Pattern: strings.TrimPrefix(word, "itemstatus:")}})
} else if strings.HasPrefix(word, "project:") {
orQueries = append(orQueries, bson.M{"project": &primitive.Regex{Pattern: strings.TrimPrefix(word, "project:")}})
} else if strings.HasPrefix(word, "name:") {
orQueries = append(orQueries, bson.M{"name": &primitive.Regex{Pattern: strings.TrimPrefix(word, "name:")}})
} else if strings.HasPrefix(word, "task:") {
orQueries = append(orQueries, bson.M{"task": &primitive.Regex{Pattern: strings.TrimPrefix(word, "task:")}})
} else {
orQueries = append(orQueries, bson.M{"project": &primitive.Regex{Pattern: word}})
orQueries = append(orQueries, bson.M{"name": &primitive.Regex{Pattern: word}})
orQueries = append(orQueries, bson.M{"task": &primitive.Regex{Pattern: word}})
orQueries = append(orQueries, bson.M{"updatetime": &primitive.Regex{Pattern: word}})
orQueries = append(orQueries, bson.M{"author": &primitive.Regex{Pattern: word}})
orQueries = append(orQueries, bson.M{"path": &primitive.Regex{Pattern: word}})
orQueries = append(orQueries, bson.M{"description": &primitive.Regex{Pattern: word}})
}
if len(orQueries) == 0 {
return results, nil
}
allQueries = append(allQueries, bson.M{"$or": orQueries})
}
q := bson.M{"$and": allQueries}
findOptions := options.Find().SetSort(bson.D{{Key: "createtime", Value: -1}})
cur, err := collection.Find(ctx, q, findOptions)
if err != nil {
return results, err
}
for cur.Next(ctx) {
var result Review
err := cur.Decode(&result)
if err != nil {
return results, err
}
results = append(results, result)
}
return results, nil
}
// RmProjectReviewV2 함수는 해당 프로젝트의 Review 데이터를 DB에서 삭제한다.
func RmProjectReviewV2(client *mongo.Client, project string) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := collection.DeleteMany(ctx, bson.M{"project": &primitive.Regex{Pattern: project}})
if err != nil {
return err
}
return nil
}
func addReviewV2(client *mongo.Client, r Review) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
num, err := collection.CountDocuments(ctx, bson.M{"_id": r.ID})
if err != nil {
return err
}
if num != 0 {
return errors.New(r.ID.Hex() + " ID를 가진 문서가 이미 DB에 존재합니다.")
}
r.Createtime = time.Now().Format(time.RFC3339)
_, err = collection.InsertOne(ctx, r)
if err != nil {
return err
}
return nil
}
func getReviewV2(client *mongo.Client, id string) (Review, error) {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
r := Review{}
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return r, err
}
err = collection.FindOne(ctx, bson.M{"_id": objID}).Decode(&r)
if err != nil {
return r, err
}
return r, nil
}
func setReviewProcessStatusV2(client *mongo.Client, id, status string) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
_, err = collection.UpdateByID(ctx, objID, bson.M{"$set": bson.M{"processstatus": status}})
if err != nil {
return err
}
return nil
}
func setErrReviewV2(client *mongo.Client, id, log string) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
_, err = collection.UpdateByID(ctx, objID, bson.M{"$set": bson.M{"processstatus": "error", "log": log}})
if err != nil {
return err
}
return nil
}
func setReviewPathV2(client *mongo.Client, id, path string) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
_, err = collection.UpdateByID(ctx, objID, bson.M{"$set": bson.M{"path": path}})
if err != nil {
return err
}
return nil
}
func setReviewItemV2(client *mongo.Client, r Review) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
update := bson.D{{Key: "$set", Value: r}}
_, err := collection.UpdateByID(ctx, r.ID, update)
if err != nil {
return err
}
return nil
}
func setReviewItemStatusV2(client *mongo.Client, id, itemstatus string) error {
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
update := bson.M{"$set": bson.M{"itemstatus": itemstatus}}
_, err = collection.UpdateByID(ctx, objID, update)
if err != nil {
return err
}
return nil
}
func addReviewCommentV2(client *mongo.Client, id string, cmt Comment) error {
if cmt.Text == "" {
return errors.New("need comment")
}
collection := client.Database(*flagDBName).Collection("review")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
objID, err := primitive.ObjectIDFromHex(id)
if err != nil {
return err
}
// 먼저 문서를 조회하여 'comments' 필드 상태 확인
var result bson.M
if err := collection.FindOne(ctx, bson.M{"_id": objID}).Decode(&result); err != nil {
return err
}
// 'comments' 필드가 null이라면 빈 배열로 초기화하는 업데이트 실행
if result["comments"] == nil {
update := bson.M{"$set": bson.M{"comments": []Comment{cmt}}}
if _, err := collection.UpdateByID(ctx, objID, update); err != nil {
return err
}
} else {
// 'comments' 필드가 null이 아니라면 댓글 추가
update := bson.M{"$push": bson.M{"comments": cmt}}
if _, err := collection.UpdateByID(ctx, objID, update); err != nil {
return err
}
}
return nil
}
func EditReviewCommentV2(client *mongo.Client, id, date, text, media string, frame int) error {
reviewItem, err := getReviewV2(client, id)
if err != nil {
return err
}
var newComments []Comment
for _, comment := range reviewItem.Comments {
if comment.Date == date {
comment.Text = text
comment.Media = media
comment.Frame = frame
}
newComments = append(newComments, comment)
}
reviewItem.Comments = newComments
err = setReviewItemV2(client, reviewItem)
if err != nil {
return err
}
return nil
}
func RmReviewCommentV2(client *mongo.Client, id, date string) error {
reviewItem, err := getReviewV2(client, id)
if err != nil {
return err
}
var newComments []Comment
for _, comment := range reviewItem.Comments {
if comment.Date == date {
continue
}
newComments = append(newComments, comment)
}
reviewItem.Comments = newComments
err = setReviewItemV2(client, reviewItem)
if err != nil {
return err
}
return nil
}