Skip to content

Instantly share code, notes, and snippets.

View percybolmer's full-sized avatar

ProgrammingPercy percybolmer

View GitHub Profile
// 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
}
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)
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
}
// 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"
package services
import (
"testing"
"github.com/google/uuid"
"github.com/percybolmer/ddd-go/aggregate"
)
func Test_Tavern(t *testing.T) {
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
package services
import (
"testing"
"github.com/google/uuid"
"github.com/percybolmer/ddd-go/aggregate"
)
func init_products(t *testing.T) []aggregate.Product {
@percybolmer
percybolmer / Go-DDD-CreateOrder.go
Last active September 4, 2021 18:04
#ddd-tavern
// 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
// 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 {
// OrderService is a implementation of the OrderService
type OrderService struct {
customers customer.CustomerRepository
products product.ProductRepository
}