cmc-sales/go/internal/cmc/handlers/customers.go

205 lines
5.2 KiB
Go

package handlers
import (
"database/sql"
"encoding/json"
"net/http"
"strconv"
"code.springupsoftware.com/cmc/cmc-sales/internal/cmc/db"
"github.com/gorilla/mux"
)
type CustomerHandler struct {
queries *db.Queries
}
func NewCustomerHandler(queries *db.Queries) *CustomerHandler {
return &CustomerHandler{queries: queries}
}
func (h *CustomerHandler) List(w http.ResponseWriter, r *http.Request) {
limit := 50
offset := 0
if l := r.URL.Query().Get("limit"); l != "" {
if val, err := strconv.Atoi(l); err == nil {
limit = val
}
}
if o := r.URL.Query().Get("offset"); o != "" {
if val, err := strconv.Atoi(o); err == nil {
offset = val
}
}
customers, err := h.queries.ListCustomers(r.Context(), db.ListCustomersParams{
Limit: int32(limit),
Offset: int32(offset),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(customers)
}
func (h *CustomerHandler) Get(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, "Invalid customer ID", http.StatusBadRequest)
return
}
customer, err := h.queries.GetCustomer(r.Context(), int32(id))
if err != nil {
if err == sql.ErrNoRows {
http.Error(w, "Customer not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(customer)
}
func (h *CustomerHandler) Create(w http.ResponseWriter, r *http.Request) {
// Parse form data for HTMX requests
if err := r.ParseForm(); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
params := db.CreateCustomerParams{
Name: r.FormValue("name"),
TradingName: r.FormValue("trading_name"),
Abn: sql.NullString{String: r.FormValue("abn"), Valid: r.FormValue("abn") != ""},
Notes: r.FormValue("notes"),
DiscountPricingPolicies: r.FormValue("discount_pricing_policies"),
PaymentTerms: r.FormValue("payment_terms"),
CustomerCategoryID: 1, // Default category
Url: r.FormValue("url"),
CountryID: 1, // Default country
}
// Check if this is a JSON request
if r.Header.Get("Content-Type") == "application/json" {
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
result, err := h.queries.CreateCustomer(r.Context(), params)
if err != nil {
if r.Header.Get("HX-Request") == "true" {
w.Header().Set("Content-Type", "text/html")
w.Write([]byte(`<div class="notification is-danger">Error creating customer</div>`))
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id, err := result.LastInsertId()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// If HTMX request, redirect to customer page
if r.Header.Get("HX-Request") == "true" {
w.Header().Set("HX-Redirect", "/customers/"+strconv.FormatInt(id, 10))
w.WriteHeader(http.StatusCreated)
return
}
// JSON response for API
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]interface{}{
"id": id,
})
}
func (h *CustomerHandler) Update(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, "Invalid customer ID", http.StatusBadRequest)
return
}
var params db.UpdateCustomerParams
if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
params.ID = int32(id)
if err := h.queries.UpdateCustomer(r.Context(), params); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *CustomerHandler) Delete(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id, err := strconv.Atoi(vars["id"])
if err != nil {
http.Error(w, "Invalid customer ID", http.StatusBadRequest)
return
}
if err := h.queries.DeleteCustomer(r.Context(), int32(id)); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *CustomerHandler) Search(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
if query == "" {
http.Error(w, "Search query required", http.StatusBadRequest)
return
}
limit := 50
offset := 0
if l := r.URL.Query().Get("limit"); l != "" {
if val, err := strconv.Atoi(l); err == nil {
limit = val
}
}
if o := r.URL.Query().Get("offset"); o != "" {
if val, err := strconv.Atoi(o); err == nil {
offset = val
}
}
customers, err := h.queries.SearchCustomersByName(r.Context(), db.SearchCustomersByNameParams{
CONCAT: query,
CONCAT_2: query,
Limit: int32(limit),
Offset: int32(offset),
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(customers)
}