diff --git a/controller/controller.go b/controller/controller.go index c28343b..cde9429 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -13,6 +13,7 @@ func New(inventory *model.Inventory) *Controller { return &Controller{inventory: inventory} } +// Método principal de entrada da CLI func (c *Controller) Execute() { for { view.ShowMenu() @@ -30,6 +31,8 @@ func (c *Controller) Execute() { c.listAllProducts() case "3", "atualizar": c.updateProduct() + case "4", "deletar": + c.deleteProduct() default: view.ShowMessage("Sistema encerrado!", nil) return @@ -62,3 +65,14 @@ func (c *Controller) updateProduct() { view.ShowMessage("Produto %d atualizado com sucesso!\n", []any{updated.ID}) } + +func (c *Controller) deleteProduct() { + productID := view.ReadDeleteProductInput() + + if err := c.inventory.DeleteProduct(productID); err != nil { + view.ShowError(err) + return + } + + view.ShowMessage("Produto %d deletado com sucesso!", []any{productID}) +} diff --git a/model/inventory.go b/model/inventory.go index 07da279..79d2ba0 100644 --- a/model/inventory.go +++ b/model/inventory.go @@ -29,6 +29,7 @@ type Inventory struct { products map[ProductID]Product } +// função auxiliar para criar a instância do objeto Inventory func NewInventory() *Inventory { return &Inventory{products: make(map[ProductID]Product)} } @@ -86,4 +87,12 @@ func (e *Inventory) UpdateProduct(id ProductID, field, value string) (*Product, return &product, nil } -func (e *Inventory) DeleteProduct() {} +func (e *Inventory) DeleteProduct(id ProductID) error { + if _, ok := e.products[id]; !ok { + return errors.New("Produto não encontrado") + } + + delete(e.products, id) + + return nil +} diff --git a/view/view.go b/view/view.go index 8609afb..1c5db13 100644 --- a/view/view.go +++ b/view/view.go @@ -83,6 +83,13 @@ func ReadEditProductInput() (model.ProductID, string) { return model.ProductID(productIDInt), field } +func ReadDeleteProductInput() model.ProductID { + fmt.Print("Informe o Id do produto que deseja deletar: ") + productID, _ := reader.ReadString('\n') + productIDInt, _ := strconv.Atoi(strings.TrimSpace(productID)) + + return model.ProductID(productIDInt) +} func ReadNewValueInput(field string) string { fmt.Printf("Informe o novo valor para %s: ", field)