/
customer.service.go
42 lines (33 loc) · 1.02 KB
/
customer.service.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
package services
import (
"context"
"time"
"github.com/siddhardhk24/bank_customer_service/interfaces"
"github.com/siddhardhk24/bank_customer_service/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type CustomerService struct {
CustomerCollection *mongo.Collection
ctx context.Context
}
func InitCustomerService(collection *mongo.Collection, ctx context.Context) interfaces.ICustomer {
return &CustomerService{collection, ctx}
}
// CreateCustomer implements interfaces.ICustomer.
func (p *CustomerService) CreateCustomer(customer *models.Customer) (*models.DBResponse, error) {
customer.CreatedAt = time.Now()
customer.UpdatedAt = customer.CreatedAt
customer.IsActive = true
res, err := p.CustomerCollection.InsertOne(p.ctx, &customer)
if err != nil {
return nil, err
}
var newUser *models.DBResponse
query := bson.M{"_id": res.InsertedID}
err = p.CustomerCollection.FindOne(p.ctx, query).Decode(&newUser)
if err != nil {
return nil, err
}
return newUser, nil
}