Last active
December 6, 2022 13:20
-
-
Save percybolmer/1847cbc5e3df4e6ec0eeb988c6642d93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Mongo is a mongo implementation of the Customer Repository | |
| package mongo | |
| import ( | |
| "context" | |
| "time" | |
| "github.com/google/uuid" | |
| "github.com/percybolmer/ddd-go/aggregate" | |
| "go.mongodb.org/mongo-driver/bson" | |
| "go.mongodb.org/mongo-driver/mongo" | |
| "go.mongodb.org/mongo-driver/mongo/options" | |
| ) | |
| type MongoRepository struct { | |
| db *mongo.Database | |
| // customer is used to store customers | |
| customer *mongo.Collection | |
| } | |
| // mongoCustomer is an internal type that is used to store a CustomerAggregate | |
| // we make an internal struct for this to avoid coupling this mongo implementation to the customeraggregate. | |
| // Mongo uses bson so we add tags for that | |
| type mongoCustomer struct { | |
| ID uuid.UUID `bson:"id"` | |
| Name string `bson:"name"` | |
| } | |
| // NewFromCustomer takes in a aggregate and converts into internal structure | |
| func NewFromCustomer(c aggregate.Customer) mongoCustomer { | |
| return mongoCustomer{ | |
| ID: c.GetID(), | |
| Name: c.GetName(), | |
| } | |
| } | |
| // ToAggregate converts into a aggregate.Customer | |
| // this could validate all values present etc | |
| func (m mongoCustomer) ToAggregate() aggregate.Customer { | |
| c := aggregate.Customer{} | |
| c.SetID(m.ID) | |
| c.SetName(m.Name) | |
| return c | |
| } | |
| // Create a new mongodb repository | |
| func New(ctx context.Context, connectionString string) (*MongoRepository, error) { | |
| client, err := mongo.Connect(ctx, options.Client().ApplyURI(connectionString)) | |
| if err != nil { | |
| return nil, err | |
| } | |
| // Find tavern DB | |
| db := client.Database("ddd") | |
| customers := db.Collection("customers") | |
| return &MongoRepository{ | |
| db: db, | |
| customer: customers, | |
| }, nil | |
| } | |
| func (mr *MongoRepository) Get(id uuid.UUID) (aggregate.Customer, error) { | |
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
| defer cancel() | |
| result := mr.customer.FindOne(ctx, bson.M{"id": id}) | |
| var c mongoCustomer | |
| err := result.Decode(&c) | |
| if err != nil { | |
| return aggregate.Customer{}, err | |
| } | |
| // Convert to aggregate | |
| return c.ToAggregate(), nil | |
| } | |
| func (mr *MongoRepository) Add(c aggregate.Customer) error { | |
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
| defer cancel() | |
| internal := NewFromCustomer(c) | |
| _, err := mr.customer.InsertOne(ctx, internal) | |
| if err != nil { | |
| return err | |
| } | |
| return nil | |
| } | |
| func (mr *MongoRepository) Update(c aggregate.Customer) error { | |
| panic("to implement") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment