165 lines
6.4 KiB
Go
165 lines
6.4 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"go-crm/internal/db"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
func ListCustomers(w http.ResponseWriter, r *http.Request) {
|
|
accountID, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
limit, _ := strconv.Atoi(r.URL.Query().Get("limit"))
|
|
offset, _ := strconv.Atoi(r.URL.Query().Get("offset"))
|
|
clientID, _ := strconv.ParseInt(r.URL.Query().Get("client_id"), 10, 64)
|
|
if limit == 0 {
|
|
limit = 20
|
|
}
|
|
|
|
customers, err := db.ListCustomers(DB, accountID, clientID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
clients, err := db.ListClients(DB, accountID, limit, offset)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
var clientOptions string
|
|
for _, c := range clients {
|
|
clientOptions += `<option value="` + strconv.FormatInt(c.ClientID, 10) + `">` + c.Name + `</option>`
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><html><head><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}</style></head><body><h1>Customers</h1><button class="btn" onclick="document.getElementById('customerForm').style.display='block'">Add Customer</button><div id="customerForm" style="display:none; margin-top:1rem;"><form hx-post="/customers" hx-target="#customerList"><input type="text" name="name" placeholder="Name" required><input type="tel" name="phone" placeholder="Phone"><input type="date" name="birth_date" placeholder="Birth Date"><input type="text" name="instagram" placeholder="Instagram"><select name="client_id" required><option value="">Select Client</option>` + clientOptions + `</select><button type="submit">Add</button></form></div><table><thead><tr><th>Name</th><th>Phone</th><th>Birth Date</th><th>Instagram</th><th>Actions</th></tr></thead><tbody id="customerList">`))
|
|
for _, c := range customers {
|
|
w.Write([]byte(`<tr><td>` + c.Name + `</td><td>` + c.Phone + `</td><td>` + c.BirthDate + `</td><td>` + c.Instagram + `</td><td><a href="/customers/` + strconv.FormatInt(c.CustomerID, 10) + `">View</a><button type="button" onclick="document.getElementById('editCust` + strconv.FormatInt(c.CustomerID, 10) + `').style.display='block'">Edit</button><form method="DELETE" style="display:inline" hx-delete="/customers/` + strconv.FormatInt(c.CustomerID, 10) + `" hx-target="closest tr"><button type="submit">Delete</button></form></td></tr><tr id="editCust` + strconv.FormatInt(c.CustomerID, 10) + `" style="display:none"><td colspan="5"><form hx-put="/customers/` + strconv.FormatInt(c.CustomerID, 10) + `" hx-target="#customerList" hx-swap="innerHTML"><input type="text" name="name" value="` + c.Name + `"><input type="tel" name="phone" value="` + c.Phone + `"><input type="date" name="birth_date" value="` + c.BirthDate + `"><input type="text" name="instagram" value="` + c.Instagram + `"><select name="client_id"><option value="` + strconv.FormatInt(c.ClientID, 10) + `">` + c.Name + `</option>` + clientOptions + `</select><button type="submit">Save</button></form></td></tr>`))
|
|
}
|
|
w.Write([]byte(`</tbody></table></body></html>`))
|
|
}
|
|
|
|
func CreateCustomer(w http.ResponseWriter, r *http.Request) {
|
|
isInternal := r.Header.Get("X-Internal-Secret") == "internal-secret"
|
|
|
|
var accountID int64
|
|
var clientID int64
|
|
var ok bool
|
|
|
|
if isInternal {
|
|
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
accountID = clientID
|
|
ok = true
|
|
} else {
|
|
accountID, ok = requireAuth(w, r)
|
|
if ok {
|
|
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
}
|
|
}
|
|
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
r.ParseForm()
|
|
if clientID == 0 {
|
|
clientID, _ = strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
}
|
|
|
|
if !isInternal {
|
|
var checkID int64
|
|
err := DB.QueryRow("SELECT client_id FROM clients WHERE client_id = ? AND account_id = ?", clientID, accountID).Scan(&checkID)
|
|
if err != nil {
|
|
http.Error(w, "Invalid client", http.StatusBadRequest)
|
|
return
|
|
}
|
|
}
|
|
|
|
customer := db.Customer{
|
|
ClientID: clientID,
|
|
Name: r.FormValue("name"),
|
|
Phone: r.FormValue("phone"),
|
|
BirthDate: r.FormValue("birth_date"),
|
|
Instagram: r.FormValue("instagram"),
|
|
CreatedAt: time.Now().Unix(),
|
|
}
|
|
|
|
if phone := r.FormValue("phone"); phone != "" {
|
|
var existingID int64
|
|
err := DB.QueryRow(
|
|
"SELECT customer_id FROM customers WHERE client_id = ? AND phone = ?",
|
|
clientID, phone,
|
|
).Scan(&existingID)
|
|
if err == nil {
|
|
_, err = DB.Exec(
|
|
"UPDATE customers SET name = ?, birth_date = ?, instagram = ? WHERE customer_id = ?",
|
|
r.FormValue("name"), r.FormValue("birth_date"), r.FormValue("instagram"), existingID,
|
|
)
|
|
if err == nil {
|
|
w.Header().Set("HX-Refresh", "true")
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
if err := customer.Create(DB); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func ViewCustomer(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
var c db.Customer
|
|
err := DB.QueryRow("SELECT customer_id, client_id, name, phone, birth_date, instagram, created_at FROM customers WHERE customer_id = ?", id).Scan(&c.CustomerID, &c.ClientID, &c.Name, &c.Phone, &c.BirthDate, &c.Instagram, &c.CreatedAt)
|
|
if err != nil {
|
|
http.Error(w, "Customer not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<!DOCTYPE html><body><h1>` + c.Name + `</h1><p>Phone: ` + c.Phone + `</p><p>Birth Date: ` + c.BirthDate + `</p><p>Instagram: ` + c.Instagram + `</p><a href="/customers">Back</a></body></html>`))
|
|
}
|
|
|
|
func UpdateCustomer(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
r.ParseForm()
|
|
clientID, _ := strconv.ParseInt(r.FormValue("client_id"), 10, 64)
|
|
_, err := DB.Exec("UPDATE customers SET client_id=?, name=?, phone=?, birth_date=?, instagram=? WHERE customer_id=?", clientID, r.FormValue("name"), r.FormValue("phone"), r.FormValue("birth_date"), r.FormValue("instagram"), id)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
w.Header().Set("HX-Refresh", "true")
|
|
}
|
|
|
|
func DeleteCustomer(w http.ResponseWriter, r *http.Request) {
|
|
_, ok := requireAuth(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
|
|
DB.Exec("DELETE FROM customers WHERE customer_id = ?", id)
|
|
} |