feat: atualizando os produtos
This commit is contained in:
@@ -25,15 +25,40 @@ func (c *Controller) Execute() {
|
|||||||
|
|
||||||
switch input {
|
switch input {
|
||||||
case "1", "adicionar":
|
case "1", "adicionar":
|
||||||
productInput := view.ReadProdutoInput()
|
c.AddProduct()
|
||||||
p := c.inventory.AdicionarProduto(productInput)
|
|
||||||
view.ShowMessage("Produto criado com ID %d: %s\n", []any{p.ID, p.Name})
|
|
||||||
case "2", "listar":
|
case "2", "listar":
|
||||||
products := c.inventory.ListarProdutos()
|
c.listAllProducts()
|
||||||
view.ShowAllProducts(products)
|
case "3", "atualizar":
|
||||||
|
c.updateProduct()
|
||||||
default:
|
default:
|
||||||
view.ShowMessage("Obrigado por utilizar o programa!", nil)
|
view.ShowMessage("Sistema encerrado!", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Controller) AddProduct() {
|
||||||
|
productInput := view.ReadAddProductInput()
|
||||||
|
p := c.inventory.AddProduct(productInput)
|
||||||
|
view.ShowMessage("Produto criado com ID %d: %s\n", []any{p.ID, p.Name})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) listAllProducts() {
|
||||||
|
products := c.inventory.FetchProducts()
|
||||||
|
view.ShowAllProducts(products)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Controller) updateProduct() {
|
||||||
|
productID, field := view.ReadEditProductInput()
|
||||||
|
|
||||||
|
value := view.ReadNewValueInput(field)
|
||||||
|
|
||||||
|
updated, err := c.inventory.UpdateProduct(productID, field, value)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
view.ShowError(err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
view.ShowMessage("Produto %d atualizado com sucesso!\n", []any{updated.ID})
|
||||||
|
}
|
||||||
|
|||||||
+52
-12
@@ -1,7 +1,17 @@
|
|||||||
package model
|
package model
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ProductID int
|
||||||
|
|
||||||
|
var productID ProductID = 1
|
||||||
|
|
||||||
type Product struct {
|
type Product struct {
|
||||||
ID int
|
ID ProductID
|
||||||
Name string
|
Name string
|
||||||
Category string
|
Category string
|
||||||
Price float64
|
Price float64
|
||||||
@@ -16,34 +26,64 @@ type ProductInput struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Inventory struct {
|
type Inventory struct {
|
||||||
products []Product
|
products map[ProductID]Product
|
||||||
nextID int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInventory() *Inventory {
|
func NewInventory() *Inventory {
|
||||||
return &Inventory{}
|
return &Inventory{products: make(map[ProductID]Product)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Inventory) AdicionarProduto(i *ProductInput) *Product {
|
func (e *Inventory) AddProduct(i *ProductInput) *Product {
|
||||||
e.nextID += 1
|
|
||||||
|
|
||||||
product := Product{
|
product := Product{
|
||||||
ID: e.nextID,
|
ID: productID,
|
||||||
Name: i.Name,
|
Name: i.Name,
|
||||||
Category: i.Category,
|
Category: i.Category,
|
||||||
Price: i.Price,
|
Price: i.Price,
|
||||||
Quantity: i.Quantity,
|
Quantity: i.Quantity,
|
||||||
}
|
}
|
||||||
|
|
||||||
e.products = append(e.products, product)
|
e.products[productID] = product
|
||||||
|
|
||||||
|
productID += 1
|
||||||
|
|
||||||
return &product
|
return &product
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Inventory) ListarProdutos() []Product {
|
func (e *Inventory) FetchProducts() map[ProductID]Product {
|
||||||
return e.products
|
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() {}
|
||||||
|
|||||||
+28
-10
@@ -10,6 +10,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var reader = bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
func ShowMenu() {
|
func ShowMenu() {
|
||||||
fmt.Print(`
|
fmt.Print(`
|
||||||
============ CONTROLE DE ESTOQUE ==============
|
============ CONTROLE DE ESTOQUE ==============
|
||||||
@@ -37,7 +39,6 @@ func ShowMessage(msg string, args []any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadMenuInput() (string, error) {
|
func ReadMenuInput() (string, error) {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
|
||||||
input, err := reader.ReadString('\n')
|
input, err := reader.ReadString('\n')
|
||||||
input = strings.TrimSpace(input)
|
input = strings.TrimSpace(input)
|
||||||
|
|
||||||
@@ -48,9 +49,7 @@ func ReadMenuInput() (string, error) {
|
|||||||
return input, nil
|
return input, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadProdutoInput() *model.ProductInput {
|
func ReadAddProductInput() *model.ProductInput {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
|
||||||
|
|
||||||
fmt.Print("Nome do produto: ")
|
fmt.Print("Nome do produto: ")
|
||||||
name, _ := reader.ReadString('\n')
|
name, _ := reader.ReadString('\n')
|
||||||
|
|
||||||
@@ -73,7 +72,26 @@ func ReadProdutoInput() *model.ProductInput {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ShowAllProducts(products []model.Product) {
|
func ReadEditProductInput() (model.ProductID, string) {
|
||||||
|
fmt.Print("Informe o Id do produto que deseja atualizar: ")
|
||||||
|
productID, _ := reader.ReadString('\n')
|
||||||
|
productIDInt, _ := strconv.Atoi(strings.TrimSpace(productID))
|
||||||
|
|
||||||
|
fmt.Print("Informe o nome do campo que deseja atualizar (nome, categoria, preco, quantidade): ")
|
||||||
|
field, _ := reader.ReadString('\n')
|
||||||
|
field = strings.TrimSpace(field)
|
||||||
|
|
||||||
|
return model.ProductID(productIDInt), field
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadNewValueInput(field string) string {
|
||||||
|
fmt.Printf("Informe o novo valor para %s: ", field)
|
||||||
|
value, _ := reader.ReadString('\n')
|
||||||
|
|
||||||
|
return strings.TrimSpace(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ShowAllProducts(products map[model.ProductID]model.Product) {
|
||||||
for _, p := range products {
|
for _, p := range products {
|
||||||
id := p.ID
|
id := p.ID
|
||||||
name := p.Name
|
name := p.Name
|
||||||
@@ -83,11 +101,11 @@ func ShowAllProducts(products []model.Product) {
|
|||||||
|
|
||||||
fmt.Printf(
|
fmt.Printf(
|
||||||
"==============================================="+"\n"+
|
"==============================================="+"\n"+
|
||||||
"PRODUTO: %d"+"\n"+
|
"PRODUCT ID: %d"+"\n"+
|
||||||
"NOME: %s"+"\n"+
|
"NAME: %s"+"\n"+
|
||||||
"CATEGORIA: %s"+"\n"+
|
"CATEGORY: %s"+"\n"+
|
||||||
"PREÇO: %.2f"+"\n"+
|
"PRICE: %.2f"+"\n"+
|
||||||
"QUANTIDADE: %d"+"\n"+
|
"QUANTITY: %d"+"\n"+
|
||||||
"==============================================="+"\n",
|
"==============================================="+"\n",
|
||||||
id, name, category, price, quantity)
|
id, name, category, price, quantity)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user