go-crm initial

This commit is contained in:
2026-04-21 17:34:58 -03:00
parent 5b8107c169
commit f8135d0e1b
24 changed files with 641 additions and 125 deletions

View File

@@ -1,8 +1,6 @@
package handlers
import (
"database/sql"
"encoding/json"
"net/http"
"strconv"
"time"
@@ -31,6 +29,10 @@ func ListClients(w http.ResponseWriter, r *http.Request) {
<head>
<title>Clients</title>
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<style>
.edit-form { display:none; margin-top:0.5rem; padding:0.5rem; border:1px solid #ccc; }
.edit-row { display:none; }
</style>
</head>
<body>
<h1>Clients</h1>
@@ -58,9 +60,20 @@ func ListClients(w http.ResponseWriter, r *http.Request) {
<td>` + c.Email + `</td>
<td>
<a href="/clients/` + strconv.FormatInt(c.ClientID, 10) + `">View</a>
<button type="button" onclick="document.getElementById('editForm` + strconv.FormatInt(c.ClientID, 10) + `').style.display='block'">Edit</button>
<form method="DELETE" style="display:inline" hx-delete="/clients/` + strconv.FormatInt(c.ClientID, 10) + `" hx-target="closest tr">
<button type="submit">Delete</button>
</form>
<tr id="editForm` + strconv.FormatInt(c.ClientID, 10) + `" class="edit-row"><td colspan="4">
<form hx-put="/clients/` + strconv.FormatInt(c.ClientID, 10) + `" hx-target="#clientList" hx-swap="innerHTML">
<input type="text" name="name" value="` + c.Name + `">
<input type="tel" name="phone" value="` + c.Phone + `">
<input type="email" name="email" value="` + c.Email + `">
<input type="text" name="address" value="` + c.Address + `">
<textarea name="notes">` + c.Notes + `</textarea>
<button type="submit">Save</button>
</form>
</td></tr>
</td>
</tr>`))
}
@@ -88,7 +101,7 @@ func CreateClient(w http.ResponseWriter, r *http.Request) {
}
func ViewClient(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10)
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
client, err := db.GetClientByID(DB, id)
if err != nil {
http.Error(w, "Client not found", http.StatusNotFound)
@@ -101,6 +114,7 @@ func ViewClient(w http.ResponseWriter, r *http.Request) {
<head></head>
<body>
<h1>` + client.Name + `</h1>
<p>Client ID: ` + strconv.FormatInt(client.ClientID, 10) + `</p>
<p>Phone: ` + client.Phone + `</p>
<p>Email: ` + client.Email + `</p>
<p>Address: ` + client.Address + `</p>
@@ -110,7 +124,7 @@ func ViewClient(w http.ResponseWriter, r *http.Request) {
}
func UpdateClient(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10)
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
r.ParseForm()
client := db.Client{
ClientID: id,
@@ -126,12 +140,11 @@ func UpdateClient(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(client)
w.Header().Set("HX-Refresh", "true")
}
func DeleteClient(w http.ResponseWriter, r *http.Request) {
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10)
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
if err := (&db.Client{}).Delete(DB, id); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return