209 lines
5 KiB
Go
209 lines
5 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 CountryHandler struct {
|
|
queries *db.Queries
|
|
}
|
|
|
|
func NewCountryHandler(queries *db.Queries) *CountryHandler {
|
|
return &CountryHandler{queries: queries}
|
|
}
|
|
|
|
func (h *CountryHandler) 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
|
|
}
|
|
}
|
|
|
|
countries, err := h.queries.ListCountries(r.Context(), db.ListCountriesParams{
|
|
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(countries)
|
|
}
|
|
|
|
func (h *CountryHandler) Get(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid country ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
country, err := h.queries.GetCountry(r.Context(), int32(id))
|
|
if err != nil {
|
|
if err == sql.ErrNoRows {
|
|
http.Error(w, "Country not found", http.StatusNotFound)
|
|
return
|
|
}
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(country)
|
|
}
|
|
|
|
func (h *CountryHandler) 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
|
|
}
|
|
|
|
name := r.FormValue("name")
|
|
|
|
// Check if this is a JSON request
|
|
if r.Header.Get("Content-Type") == "application/json" {
|
|
var params struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
name = params.Name
|
|
}
|
|
|
|
result, err := h.queries.CreateCountry(r.Context(), name)
|
|
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 country</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, return success message
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
w.Header().Set("Content-Type", "text/html")
|
|
w.Write([]byte(`<div class="notification is-success">Country created successfully</div>`))
|
|
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 *CountryHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid country ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
var name string
|
|
if r.Header.Get("Content-Type") == "application/json" {
|
|
var params struct {
|
|
Name string `json:"name"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(¶ms); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
name = params.Name
|
|
} else {
|
|
// Handle form data
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
name = r.FormValue("name")
|
|
}
|
|
|
|
if err := h.queries.UpdateCountry(r.Context(), db.UpdateCountryParams{
|
|
Name: name,
|
|
ID: int32(id),
|
|
}); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *CountryHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
vars := mux.Vars(r)
|
|
id, err := strconv.Atoi(vars["id"])
|
|
if err != nil {
|
|
http.Error(w, "Invalid country ID", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := h.queries.DeleteCountry(r.Context(), int32(id)); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *CountryHandler) CompleteCountry(w http.ResponseWriter, r *http.Request) {
|
|
query := r.URL.Query().Get("term")
|
|
if query == "" {
|
|
query = r.URL.Query().Get("q")
|
|
}
|
|
|
|
if query == "" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode([]interface{}{})
|
|
return
|
|
}
|
|
|
|
countries, err := h.queries.SearchCountriesByName(r.Context(), query)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Format for autocomplete
|
|
var results []map[string]interface{}
|
|
for _, country := range countries {
|
|
results = append(results, map[string]interface{}{
|
|
"id": country.ID,
|
|
"label": country.Name,
|
|
"value": country.Name,
|
|
})
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(results)
|
|
} |