Boa tarde pessoal, tudo bem? Espero que sim!
Seguinte, estou tentando remover os produtos criados durante os testes, mas só consigo deletar os produtos novos, os antigos não deletam.
routes.go:
package routes
import (
"net/http"
ctr "web-store-go/infra/entrypoints/web/controllers"
)
func Routes() {
http.HandleFunc("/", ctr.Index)
http.HandleFunc("/new", ctr.New)
http.HandleFunc("/insert", ctr.Insert)
http.HandleFunc("/delete", ctr.Delete)
}
products.go (Controller):
package controllers
import (
"html/template"
"log"
"net/http"
"strconv"
prd "web-store-go/application/domain/products"
)
var temp = template.Must(template.ParseGlob("infra/templates/*.html"))
[...]
func Delete(writer http.ResponseWriter, request *http.Request) {
log.Println(request)
log.Println(request.URL)
log.Println(request.URL.Query())
log.Println(request.URL.Query().Get("id"))
id := request.URL.Query().Get("id")
prd.DeleteProduct(id)
http.Redirect(writer, request, "/", http.StatusMovedPermanently)
}
product.go (Model)
package products
import (
"log"
rep "web-store-go/infra/config/repository"
)
type Product struct {
Id int
Name string
Description string
Price float64
Quantity int
}
[...]
func DeleteProduct(id string) {
log.Println("ID", id)
delete := "DELETE FROM produtos WHERE id = $1"
db := rep.Conectar()
statement, error := db.Prepare(delete)
if error != nil {
panic(error.Error())
}
statement.Exec(id)
defer db.Close()
}
index.html
{{define "index"}}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script>
<title>Alura loja</title>
</head>
<body>
<nav class="navbar navbar-light bg-light mb-4">
<a class="navbar-brand" href="/">Alura Loja</a>
</nav>
<div class="container">
<section class="card">
<div>
<table class="table table-striped table-hover mb-0">
<thead>
<tr>
<th>Id</th>
<th>Nome</th>
<th>Descrição</th>
<th>Preço</th>
<th>Quantidade</th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<td>{{.Id}}</td>
<td>{{.Name}}</td>
<td>{{.Description}}</td>
<td>{{.Price}}</td>
<td>{{.Quantity}}</td>
<td><a href="/delete?id={{.Id}}" class="btn btn-danger">Remover</a></td>
</tr>
{{end}}
</tbody>
</table>
</div>
</section>
<div class="card-footer">
<a href="/new" class="btn btn-primary mb-2">Novo</a>
</div>
</div>
</body>
</html>
{{end}}
O mais estranho é que, não aparece nenhum erro.
Quando eu crio um produto novo, e removo, funciona. Quando o produto é "antigo", não funciona.
As linhas de log.Println só aparecem quando o produto é novo, quando é antigo não aparecem:
2025/01/19 12:36:01 &{GET /delete?id=24 HTTP/1.1 1 1 map[Accept:[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7] Accept-Encoding:[gzip, deflate, br, zstd] Accept-Language:[pt-BR,pt;q=0.9] Connection:[keep-alive] Referer:[http://localhost:8080/] Sec-Ch-Ua:["Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"] Sec-Ch-Ua-Mobile:[?0] Sec-Ch-Ua-Platform:["Windows"] Sec-Fetch-Dest:[document] Sec-Fetch-Mode:[navigate] Sec-Fetch-Site:[same-origin] Sec-Fetch-User:[?1] Upgrade-Insecure-Requests:[1] User-Agent:[Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36]] {} <nil> 0 [] false localhost:8080 map[] map[] <nil> map[] [::1]:11389 /delete?id=24 <nil> <nil> <nil> /delete 0xc0003029b0 0xc0000705a0 [] map[]}
2025/01/19 12:36:01 /delete?id=24
2025/01/19 12:36:01 map[id:[24]]
2025/01/19 12:36:01 24
2025/01/19 12:36:01 ID 24