- Go module with Gin, GORM, JWT, excelize dependencies - Configuration loading from YAML with all settings - GORM models for users, categories, components, configurations, alerts - Repository layer for all entities - Services: auth (JWT), pricing (median/average/weighted), components, quotes, configurations, export (CSV/XLSX), alerts - Middleware: JWT auth, role-based access, CORS - HTTP handlers for all API endpoints - Main server with dependency injection and graceful shutdown Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mchus/quoteforge/internal/repository"
|
|
"github.com/mchus/quoteforge/internal/services"
|
|
)
|
|
|
|
type ComponentHandler struct {
|
|
componentService *services.ComponentService
|
|
}
|
|
|
|
func NewComponentHandler(componentService *services.ComponentService) *ComponentHandler {
|
|
return &ComponentHandler{componentService: componentService}
|
|
}
|
|
|
|
func (h *ComponentHandler) List(c *gin.Context) {
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
perPage, _ := strconv.Atoi(c.DefaultQuery("per_page", "20"))
|
|
|
|
filter := repository.ComponentFilter{
|
|
Category: c.Query("category"),
|
|
Vendor: c.Query("vendor"),
|
|
Search: c.Query("search"),
|
|
HasPrice: c.Query("has_price") == "true",
|
|
}
|
|
|
|
result, err := h.componentService.List(filter, page, perPage)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, result)
|
|
}
|
|
|
|
func (h *ComponentHandler) Get(c *gin.Context) {
|
|
lotName := c.Param("lot_name")
|
|
|
|
component, err := h.componentService.GetByLotName(lotName)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "component not found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, component)
|
|
}
|
|
|
|
func (h *ComponentHandler) GetCategories(c *gin.Context) {
|
|
categories, err := h.componentService.GetCategories()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, categories)
|
|
}
|
|
|
|
func (h *ComponentHandler) GetVendors(c *gin.Context) {
|
|
category := c.Query("category")
|
|
|
|
vendors, err := h.componentService.GetVendors(category)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, vendors)
|
|
}
|