refatorando a arquitetura
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package inmemory
|
||||
|
||||
import (
|
||||
"controle-de-estoque/internal/model"
|
||||
"errors"
|
||||
)
|
||||
|
||||
var ErrNotFound = errors.New("Produto não encontrado!")
|
||||
|
||||
type InventoryRepository struct {
|
||||
products map[int]*model.Product
|
||||
nextID int
|
||||
}
|
||||
|
||||
func NewInventorytRepository() *InventoryRepository {
|
||||
return &InventoryRepository{
|
||||
products: make(map[int]*model.Product),
|
||||
nextID: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *InventoryRepository) Create(p *model.ProductInput) *model.Product {
|
||||
product := model.Product{
|
||||
ID: r.nextID,
|
||||
Name: p.Name,
|
||||
Category: p.Category,
|
||||
Price: p.Price,
|
||||
Quantity: p.Quantity,
|
||||
}
|
||||
|
||||
r.products[r.nextID] = &product
|
||||
|
||||
r.nextID++
|
||||
|
||||
return &product
|
||||
}
|
||||
|
||||
func (r *InventoryRepository) FindByID(id int) (*model.Product, error) {
|
||||
product, ok := r.products[id]
|
||||
|
||||
if !ok {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
return product, nil
|
||||
}
|
||||
|
||||
func (r *InventoryRepository) FindAll() map[int]*model.Product {
|
||||
return r.products
|
||||
}
|
||||
|
||||
func (r *InventoryRepository) Update(p *model.Product) error {
|
||||
if _, ok := r.products[p.ID]; !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
r.products[p.ID] = p
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *InventoryRepository) Delete(id int) error {
|
||||
if _, ok := r.products[id]; !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
delete(r.products, id)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package repository
|
||||
|
||||
import "controle-de-estoque/internal/model"
|
||||
|
||||
type InventoryRepository interface {
|
||||
Create(p *model.ProductInput) *model.Product
|
||||
FindByID(id int) (*model.Product, error)
|
||||
FindAll() map[int]*model.Product
|
||||
Update(p *model.Product) error
|
||||
Delete(id int) error
|
||||
}
|
||||
Reference in New Issue
Block a user