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
150package storage
import (
"context"
"time"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
internalErrors "github.com/devchallenge/article-similarity/internal/errors"
"github.com/devchallenge/article-similarity/internal/model"
)
const (
maxArticles = 1000
collectionArticles = "articles"
collectionAutoincrement = "autoincrement"
)
type Storage struct {
collectionArticle *mongo.Collection
collectionAutoincrement *mongo.Collection
}
func New(mc *mongo.Client, database string) *Storage {
db := mc.Database(database)
return &Storage{
collectionArticle: db.Collection(collectionArticles),
collectionAutoincrement: db.Collection(collectionAutoincrement),
}
}
func (s *Storage) NextArticleID(ctx context.Context) (int, error) {
inc, err := s.autoincrement(ctx, collectionArticles)
if err != nil {
return 0, errors.WithStack(err)
}
return inc.Counter, nil
}
func (s *Storage) CreateArticle(ctx context.Context, id int, content string, duplicateIDs []int, isUnique bool) error {
art := article{
ID: id,
Content: content,
DuplicateIDs: duplicateIDs,
IsUnique: isUnique,
}
ma, err := bson.Marshal(&art)
if err != nil {
return errors.Wrap(err, "failed to marshal")
}
if _, err := s.collectionArticle.InsertOne(ctx, ma); err != nil {
return errors.Wrap(err, "failed to insert")
}
return nil
}
func (s *Storage) UpdateArticle(ctx context.Context, id int, duplicateIDs []int) error {
filter := bson.D{{Key: "id", Value: id}}
update := bson.M{
"$set": bson.M{"duplicate_ids": duplicateIDs},
}
if err := s.collectionArticle.FindOneAndUpdate(ctx, filter, update, nil).Err(); err != nil {
return errors.Wrap(err, "failed to update")
}
return nil
}
func (s *Storage) ArticleByID(ctx context.Context, id int) (model.Article, error) {
res := s.collectionArticle.FindOne(ctx, bson.D{{Key: "id", Value: id}})
if errors.Is(res.Err(), mongo.ErrNoDocuments) {
return model.Article{}, errors.Wrap(internalErrors.ErrNotFound, "not found")
}
if res.Err() != nil {
return model.Article{}, errors.Wrap(res.Err(), "failed to find")
}
art := article{}
if err := res.Decode(&art); err != nil {
return model.Article{}, errors.Wrap(err, "failed to decode")
}
return toModelArticle(art), nil
}
func (s *Storage) AllArticles(ctx context.Context) ([]model.Article, error) {
return s.articles(ctx, bson.D{})
}
func (s *Storage) UniqueArticles(ctx context.Context) ([]model.Article, error) {
return s.articles(ctx, bson.D{{Key: "is_unique", Value: true}})
}
func (s *Storage) articles(ctx context.Context, filter bson.D) ([]model.Article, error) {
articles := make([]model.Article, 0, maxArticles)
cur, err := s.collectionArticle.Find(ctx, filter)
if err != nil {
return nil, errors.Wrap(err, "failed to find documents")
}
for cur.TryNext(ctx) && len(articles) != maxArticles {
art := article{}
if err := cur.Decode(&art); err != nil {
return nil, errors.Wrap(err, "failed to cursor decode")
}
articles = append(articles, toModelArticle(art))
}
return articles, nil
}
func toModelArticle(art article) model.Article {
return model.Article{
ID: art.ID,
Content: art.Content,
DuplicateIDs: art.DuplicateIDs,
IsUnique: art.IsUnique,
}
}
func (s *Storage) autoincrement(ctx context.Context, collection string) (*autoincrement, error) {
opts := options.FindOneAndUpdate().SetReturnDocument(options.After).SetUpsert(true)
doc := &autoincrement{}
filter := bson.M{"collection": collection}
update := bson.M{
"$inc": bson.M{"counter": 1},
"$set": bson.M{"updated_at": time.Now()},
}
if err := s.collectionAutoincrement.FindOneAndUpdate(ctx, filter, update, opts).
Decode(&doc); err != nil {
return nil, errors.Wrap(err, "failed to find one and update")
}
return doc, nil
}