feat: deletar produtos
This commit is contained in:
@@ -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})
|
||||
}
|
||||
|
||||
+10
-1
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user