-
Notifications
You must be signed in to change notification settings - Fork 0
/
productDataStoreMongoDb.go
35 lines (29 loc) · 1.14 KB
/
productDataStoreMongoDb.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
package database
import (
"github.com/pkg/errors"
"github.com/zeroberto/products-store/discount-calculator/datastore"
"github.com/zeroberto/products-store/discount-calculator/driver/dbdriver"
"github.com/zeroberto/products-store/discount-calculator/model"
"go.mongodb.org/mongo-driver/bson/primitive"
)
const (
// ProductsCollection represents the name of the product collection in the database
ProductsCollection string = "products"
)
// ProductDataStoreMongoDB is responsible for implementing the ProductDataStore interface,
// using a document database
type ProductDataStoreMongoDB struct {
DBDriver dbdriver.NoSQLDBDriver
}
// FindByID is responsible for obtaining a product according to the given identifier
func (pds *ProductDataStoreMongoDB) FindByID(ID string) (*model.Product, error) {
result, err := pds.DBDriver.GetDocByID(ID, ProductsCollection)
if err != nil {
return nil, &datastore.Error{Cause: errors.Wrap(err, "Could not retrieve product")}
}
product := model.Product{
ID: result.(primitive.M)["_id"].(primitive.ObjectID).Hex(),
PriceInCents: result.(primitive.M)["price_in_cents"].(int32),
}
return &product, nil
}