/
license.go
47 lines (38 loc) · 1.37 KB
/
license.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
package databases
// go generate: mockery --name LicenseDatabase
import (
"context"
"github.com/linesmerrill/police-cad-api/models"
"go.mongodb.org/mongo-driver/mongo/options"
)
const licenseName = "licenses"
// LicenseDatabase contains the methods to use with the license database
type LicenseDatabase interface {
FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*models.License, error)
Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]models.License, error)
}
type licenseDatabase struct {
db DatabaseHelper
}
// NewLicenseDatabase initializes a new instance of license database with the provided db connection
func NewLicenseDatabase(db DatabaseHelper) LicenseDatabase {
return &licenseDatabase{
db: db,
}
}
func (c *licenseDatabase) FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*models.License, error) {
license := &models.License{}
err := c.db.Collection(licenseName).FindOne(ctx, filter).Decode(&license)
if err != nil {
return nil, err
}
return license, nil
}
func (c *licenseDatabase) Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]models.License, error) {
var licenses []models.License
err := c.db.Collection(licenseName).Find(ctx, filter, opts...).Decode(&licenses)
if err != nil {
return nil, err
}
return licenses, nil
}