refatorando a arquitetura
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (a *App) handleAdd() {
|
||||
name := a.readLine("Nome do produto: ")
|
||||
category := a.readLine("Categoria do produto: ")
|
||||
priceStr := a.readLine("Preço: ")
|
||||
|
||||
price, err := strconv.ParseFloat(priceStr, 64)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Preço inválido.")
|
||||
return
|
||||
}
|
||||
|
||||
qtyStr := a.readLine("Quantidade: ")
|
||||
qty, err := strconv.Atoi(qtyStr)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Quantidade inválida.")
|
||||
return
|
||||
}
|
||||
|
||||
product, err := a.service.CreateProduct(name, category, price, qty)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Erro ao adicionar:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Produto adicionado: #%d %s\n", product.ID, product.Name)
|
||||
}
|
||||
|
||||
func (a *App) handleList() {
|
||||
products := a.service.ListProducts()
|
||||
|
||||
if len(products) == 0 {
|
||||
fmt.Println("Nenhum produto cadastrado.")
|
||||
return
|
||||
}
|
||||
|
||||
for _, p := range products {
|
||||
fmt.Printf(
|
||||
"==============================================="+"\n"+
|
||||
"ID PRODUTO: %d"+"\n"+
|
||||
"NOME: %s"+"\n"+
|
||||
"CATEGORIA: %s"+"\n"+
|
||||
"PREÇO: %.2f"+"\n"+
|
||||
"QUANTIDADE: %d"+"\n"+
|
||||
"==============================================="+"\n",
|
||||
p.ID, p.Name, p.Category, p.Price, p.Quantity)
|
||||
}
|
||||
}
|
||||
|
||||
func (a *App) handleUpdate() {
|
||||
idStr := a.readLine("Informe o Id do produto que deseja atualizar: ")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
fmt.Println("ID inválido.")
|
||||
return
|
||||
}
|
||||
|
||||
field := a.readLine("Informe o campo que deseja atualizar (nome, categoria, preco, quantidade): ")
|
||||
value := a.readLine(fmt.Sprintf("Informe o novo valor para %s: ", field))
|
||||
|
||||
product, err := a.service.UpdateProduct(id, field, value)
|
||||
if err != nil {
|
||||
fmt.Println("Erro ao atualizar:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Produto %d atualizado com sucesso!\n", product.ID)
|
||||
}
|
||||
|
||||
func (a *App) handleDelete() {
|
||||
idStr := a.readLine("Informe o Id do produto que deseja deletar: ")
|
||||
id, err := strconv.Atoi(idStr)
|
||||
if err != nil {
|
||||
fmt.Println("ID inválido.")
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.service.DeleteProduct(id); err != nil {
|
||||
fmt.Println("Erro ao deletar:", err)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Printf("Produto %d deletado com sucesso!\n", id)
|
||||
}
|
||||
Reference in New Issue
Block a user