feat: atualizando os produtos

This commit is contained in:
2026-07-15 00:28:28 -03:00
parent 0315a20b2a
commit 08c83157b1
3 changed files with 111 additions and 28 deletions
+52 -12
View File
@@ -1,7 +1,17 @@
package model
import (
"errors"
"strconv"
"strings"
)
type ProductID int
var productID ProductID = 1
type Product struct {
ID int
ID ProductID
Name string
Category string
Price float64
@@ -16,34 +26,64 @@ type ProductInput struct {
}
type Inventory struct {
products []Product
nextID int
products map[ProductID]Product
}
func NewInventory() *Inventory {
return &Inventory{}
return &Inventory{products: make(map[ProductID]Product)}
}
func (e *Inventory) AdicionarProduto(i *ProductInput) *Product {
e.nextID += 1
func (e *Inventory) AddProduct(i *ProductInput) *Product {
product := Product{
ID: e.nextID,
ID: productID,
Name: i.Name,
Category: i.Category,
Price: i.Price,
Quantity: i.Quantity,
}
e.products = append(e.products, product)
e.products[productID] = product
productID += 1
return &product
}
func (e *Inventory) ListarProdutos() []Product {
func (e *Inventory) FetchProducts() map[ProductID]Product {
return e.products
}
func (e *Inventory) AtualizarProduto() {}
func (e *Inventory) UpdateProduct(id ProductID, field, value string) (*Product, error) {
product, ok := e.products[id]
func (e *Inventory) DeletarProduto() {}
if !ok {
return nil, errors.New("Produto não encontrado")
}
switch strings.ToLower(field) {
case "nome", "name":
product.Name = value
case "categoria", "category":
product.Category = value
case "preco", "preço", "price":
price, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, errors.New("Preço inválido")
}
product.Price = price
case "quantidade", "quantity":
quantity, err := strconv.Atoi(value)
if err != nil {
return nil, errors.New("Quantidade inválida")
}
product.Quantity = quantity
default:
return nil, errors.New("Campo inválido")
}
e.products[id] = product
return &product, nil
}
func (e *Inventory) DeleteProduct() {}