/
device_token_repository.go
75 lines (58 loc) · 1.82 KB
/
device_token_repository.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
package repositories
import (
"context"
"github.com/febrihidayan/go-architecture-monorepo/services/notification/domain/entities"
"github.com/febrihidayan/go-architecture-monorepo/services/notification/internal/repositories/mongo/mappers"
"github.com/febrihidayan/go-architecture-monorepo/services/notification/internal/repositories/mongo/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type DeviceTokenRepository struct {
db *mongo.Database
}
func NewDeviceTokenRepository(db *mongo.Database) DeviceTokenRepository {
return DeviceTokenRepository{db: db}
}
func (x *DeviceTokenRepository) Create(ctx context.Context, payload *entities.DeviceToken) error {
_, err := x.db.Collection(models.DeviceToken{}.TableName()).InsertOne(ctx, mappers.ToModelDeviceToken(payload))
if err != nil {
return err
}
return nil
}
func (x *DeviceTokenRepository) All(ctx context.Context, params *entities.DeviceTokenQueryParams) ([]*entities.DeviceToken, error) {
var (
results []*entities.DeviceToken
filter = mongo.Pipeline{}
match bson.D
)
if params.UserId != "" {
match = append(match, bson.D{{"user_id", params.UserId}}...)
}
if len(match) > 0 {
filter = append(filter, mongo.Pipeline{
bson.D{{
"$match", match,
}},
}...)
}
cursor, err := x.db.Collection(models.DeviceToken{}.TableName()).Aggregate(ctx, filter)
if err != nil {
return nil, err
}
defer cursor.Close(ctx)
for cursor.Next(ctx) {
var item models.DeviceToken
if err := cursor.Decode(&item); err != nil {
return nil, err
}
results = append(results, mappers.ToDomainDeviceToken(&item))
}
return results, nil
}
func (x *DeviceTokenRepository) Delete(ctx context.Context, id string) error {
_, err := x.db.Collection(models.DeviceToken{}.TableName()).DeleteOne(ctx, bson.M{
"_id": id,
})
return err
}