e5623d2f84
- Add Infrastructure Health section with CPU/Memory/Disk panels - Add Service Status section with PM2/Docker panels - Create GrafanaPanel component for iframe embedding - Build passes successfully
108 lines
2.6 KiB
Go
108 lines
2.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"boc/service"
|
|
)
|
|
|
|
// CRMHandlerV2 uses the service layer for business logic
|
|
type CRMHandlerV2 struct {
|
|
service *service.CustomerService
|
|
}
|
|
|
|
func NewCRMHandlerV2(s *service.CustomerService) *CRMHandlerV2 {
|
|
return &CRMHandlerV2{service: s}
|
|
}
|
|
|
|
func (h *CRMHandlerV2) ListCustomers(w http.ResponseWriter, r *http.Request) {
|
|
status := r.URL.Query().Get("status")
|
|
if status == "" {
|
|
status = "active"
|
|
}
|
|
|
|
tenantID := r.Context().Value("tenant_id")
|
|
if tenantID == nil {
|
|
tenantID = "default"
|
|
}
|
|
|
|
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
pageSize, _ := strconv.Atoi(r.URL.Query().Get("page_size"))
|
|
if pageSize < 1 {
|
|
pageSize = 20
|
|
}
|
|
|
|
resp, err := h.service.ListCustomers(r.Context(), tenantID.(string), status, page, pageSize)
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to list customers")
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
func (h *CRMHandlerV2) GetCustomer(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
customer, err := h.service.GetCustomer(r.Context(), id)
|
|
if err != nil {
|
|
writeError(w, http.StatusNotFound, "customer not found")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, customer)
|
|
}
|
|
|
|
func (h *CRMHandlerV2) CreateCustomer(w http.ResponseWriter, r *http.Request) {
|
|
var req service.CreateCustomerRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request")
|
|
return
|
|
}
|
|
|
|
customer, err := h.service.CreateCustomer(r.Context(), &req)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusCreated, map[string]interface{}{
|
|
"id": customer.ID,
|
|
"message": "Customer created",
|
|
})
|
|
}
|
|
|
|
func (h *CRMHandlerV2) UpdateCustomer(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
var req service.UpdateCustomerRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
writeError(w, http.StatusBadRequest, "invalid request")
|
|
return
|
|
}
|
|
|
|
customer, err := h.service.UpdateCustomer(r.Context(), id, &req)
|
|
if err != nil {
|
|
writeError(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
writeJSON(w, http.StatusOK, customer)
|
|
}
|
|
|
|
func (h *CRMHandlerV2) DeleteCustomer(w http.ResponseWriter, r *http.Request) {
|
|
id := chi.URLParam(r, "id")
|
|
if err := h.service.DeleteCustomer(r.Context(), id); err != nil {
|
|
writeError(w, http.StatusInternalServerError, "failed to delete customer")
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]interface{}{
|
|
"message": "Customer deleted",
|
|
})
|
|
}
|