-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete.go
41 lines (33 loc) · 931 Bytes
/
delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/devbersi/finance-go/models"
"github.com/go-chi/chi"
)
func Delete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
log.Printf("Erro ao tentar fazer o parse: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
rows, err := models.Delete(int64(id))
if err != nil {
log.Printf("Erro ao remover registro: %v", err)
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
if rows > 1 {
log.Printf("Error: foram removidos %d registros", rows)
}
resp :=map[string]any {
"Error": false,
"Message": fmt.Sprintf("Expense removido com sucesso! ID: %v", id),
}
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(resp)
}