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
// CustomerRepository is a interface that defines the rules around what a customer repository | |
// Has to be able to perform | |
type CustomerRepository interface { | |
Get(uuid.UUID) (Customer, error) | |
Add(Customer) error | |
Update(Customer) error | |
} |
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
func Test_MongoTavern(t *testing.T) { | |
// Create OrderService | |
products := init_products(t) | |
os, err := NewOrderService( | |
WithMongoCustomerRepository("mongodb://localhost:27017"), | |
WithMemoryProductRepository(products), | |
) | |
if err != nil { | |
t.Error(err) |
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
func WithMongoCustomerRepository(connectionString string) OrderConfiguration { | |
return func(os *OrderService) error { | |
// Create the mongo repo, if we needed parameters, such as connection strings they could be inputted here | |
cr, err := mongo.New(context.Background(), connectionString) | |
if err != nil { | |
return err | |
} | |
os.customers = cr | |
return nil | |
} |
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" |
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
package services | |
import ( | |
"testing" | |
"github.com/google/uuid" | |
"github.com/percybolmer/ddd-go/aggregate" | |
) | |
func Test_Tavern(t *testing.T) { |
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
package services | |
import ( | |
"log" | |
"github.com/google/uuid" | |
) | |
// TavernConfiguration is an alias that takes a pointer and modifies the Tavern | |
type TavernConfiguration func(os *Tavern) error |
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
package services | |
import ( | |
"testing" | |
"github.com/google/uuid" | |
"github.com/percybolmer/ddd-go/aggregate" | |
) | |
func init_products(t *testing.T) []aggregate.Product { |
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
// CreateOrder will chaintogether all repositories to create a order for a customer | |
// will return the collected price of all Products | |
func (o *OrderService) CreateOrder(customerID uuid.UUID, productIDs []uuid.UUID) (float64, error) { | |
// Get the customer | |
c, err := o.customers.Get(customerID) | |
if err != nil { | |
return 0, err | |
} | |
// Get each Product, Ouchie, We need a ProductRepository |
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
// WithMemoryProductRepository adds a in memory product repo and adds all input products | |
func WithMemoryProductRepository(products []aggregate.Product) OrderConfiguration { | |
return func(os *OrderService) error { | |
// Create the memory repo, if we needed parameters, such as connection strings they could be inputted here | |
pr := prodmemory.New() | |
// Add Items to repo | |
for _, p := range products { | |
err := pr.Add(p) | |
if err != nil { |
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
// OrderService is a implementation of the OrderService | |
type OrderService struct { | |
customers customer.CustomerRepository | |
products product.ProductRepository | |
} |