65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"controle-de-estoque/model"
|
|
"controle-de-estoque/view"
|
|
)
|
|
|
|
type Controller struct {
|
|
inventory *model.Inventory
|
|
}
|
|
|
|
func New(inventory *model.Inventory) *Controller {
|
|
return &Controller{inventory: inventory}
|
|
}
|
|
|
|
func (c *Controller) Execute() {
|
|
for {
|
|
view.ShowMenu()
|
|
input, err := view.ReadMenuInput()
|
|
|
|
if err != nil {
|
|
view.ShowError(err)
|
|
break
|
|
}
|
|
|
|
switch input {
|
|
case "1", "adicionar":
|
|
c.AddProduct()
|
|
case "2", "listar":
|
|
c.listAllProducts()
|
|
case "3", "atualizar":
|
|
c.updateProduct()
|
|
default:
|
|
view.ShowMessage("Sistema encerrado!", nil)
|
|
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})
|
|
}
|