Created
August 30, 2021 05:56
-
-
Save percybolmer/1f367450d2e7b2d79d101d9d1c30fe45 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
// Package memory is a in memory implementation of the ProductRepository interface. | |
package memory | |
import ( | |
"sync" | |
"github.com/google/uuid" | |
"github.com/percybolmer/ddd-go/aggregate" | |
"github.com/percybolmer/ddd-go/domain/product" | |
) | |
type MemoryProductRepository struct { | |
products map[uuid.UUID]aggregate.Product | |
sync.Mutex | |
} | |
// New is a factory function to generate a new repository of customers | |
func New() *MemoryProductRepository { | |
return &MemoryProductRepository{ | |
products: make(map[uuid.UUID]aggregate.Product), | |
} | |
} | |
// GetAll returns all products as a slice | |
// Yes, it never returns an error, but | |
// A database implementation could return an error for instance | |
func (mpr *MemoryProductRepository) GetAll() ([]aggregate.Product, error) { | |
// Collect all Products from map | |
var products []aggregate.Product | |
for _, product := range mpr.products { | |
products = append(products, product) | |
} | |
return products, nil | |
} | |
// GetByID searches for a product based on it's ID | |
func (mpr *MemoryProductRepository) GetByID(id uuid.UUID) (aggregate.Product, error) { | |
if product, ok := mpr.products[uuid.UUID(id)]; ok { | |
return product, nil | |
} | |
return aggregate.Product{}, product.ErrProductNotFound | |
} | |
// Add will add a new product to the repository | |
func (mpr *MemoryProductRepository) Add(newprod aggregate.Product) error { | |
mpr.Lock() | |
defer mpr.Unlock() | |
if _, ok := mpr.products[newprod.GetID()]; ok { | |
return product.ErrProductAlreadyExist | |
} | |
mpr.products[newprod.GetID()] = newprod | |
return nil | |
} | |
// Update will change all values for a product based on it's ID | |
func (mpr *MemoryProductRepository) Update(upprod aggregate.Product) error { | |
mpr.Lock() | |
defer mpr.Unlock() | |
if _, ok := mpr.products[upprod.GetID()]; !ok { | |
return product.ErrProductNotFound | |
} | |
mpr.products[upprod.GetID()] = upprod | |
return nil | |
} | |
// Delete remove an product from the repository | |
func (mpr *MemoryProductRepository) Delete(id uuid.UUID) error { | |
mpr.Lock() | |
defer mpr.Unlock() | |
if _, ok := mpr.products[id]; !ok { | |
return product.ErrProductNotFound | |
} | |
delete(mpr.products, id) | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment