- 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>
175 lines
4.8 KiB
Go
175 lines
4.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/mchus/quoteforge/internal/middleware"
|
|
"github.com/mchus/quoteforge/internal/models"
|
|
"github.com/mchus/quoteforge/internal/services"
|
|
)
|
|
|
|
type ExportHandler struct {
|
|
exportService *services.ExportService
|
|
configService *services.ConfigurationService
|
|
componentService *services.ComponentService
|
|
}
|
|
|
|
func NewExportHandler(
|
|
exportService *services.ExportService,
|
|
configService *services.ConfigurationService,
|
|
componentService *services.ComponentService,
|
|
) *ExportHandler {
|
|
return &ExportHandler{
|
|
exportService: exportService,
|
|
configService: configService,
|
|
componentService: componentService,
|
|
}
|
|
}
|
|
|
|
type ExportRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Items []struct {
|
|
LotName string `json:"lot_name" binding:"required"`
|
|
Quantity int `json:"quantity" binding:"required,min=1"`
|
|
UnitPrice float64 `json:"unit_price"`
|
|
} `json:"items" binding:"required,min=1"`
|
|
Notes string `json:"notes"`
|
|
}
|
|
|
|
func (h *ExportHandler) ExportCSV(c *gin.Context) {
|
|
var req ExportRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data := h.buildExportData(&req)
|
|
|
|
csvData, err := h.exportService.ToCSV(data)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s_%s.csv", req.Name, time.Now().Format("20060102"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
c.Data(http.StatusOK, "text/csv; charset=utf-8", csvData)
|
|
}
|
|
|
|
func (h *ExportHandler) ExportXLSX(c *gin.Context) {
|
|
var req ExportRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data := h.buildExportData(&req)
|
|
|
|
xlsxData, err := h.exportService.ToXLSX(data)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s_%s.xlsx", req.Name, time.Now().Format("20060102"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", xlsxData)
|
|
}
|
|
|
|
func (h *ExportHandler) buildExportData(req *ExportRequest) *services.ExportData {
|
|
items := make([]services.ExportItem, len(req.Items))
|
|
var total float64
|
|
|
|
for i, item := range req.Items {
|
|
itemTotal := item.UnitPrice * float64(item.Quantity)
|
|
items[i] = services.ExportItem{
|
|
LotName: item.LotName,
|
|
Quantity: item.Quantity,
|
|
UnitPrice: item.UnitPrice,
|
|
TotalPrice: itemTotal,
|
|
}
|
|
total += itemTotal
|
|
}
|
|
|
|
return &services.ExportData{
|
|
Name: req.Name,
|
|
Items: items,
|
|
Total: total,
|
|
Notes: req.Notes,
|
|
CreatedAt: time.Now(),
|
|
}
|
|
}
|
|
|
|
func (h *ExportHandler) ExportConfigCSV(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
config, err := h.configService.GetByUUID(uuid, userID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data := h.configToExportData(config)
|
|
|
|
csvData, err := h.exportService.ToCSV(data)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s_%s.csv", config.Name, config.CreatedAt.Format("20060102"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
c.Data(http.StatusOK, "text/csv; charset=utf-8", csvData)
|
|
}
|
|
|
|
func (h *ExportHandler) ExportConfigXLSX(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
config, err := h.configService.GetByUUID(uuid, userID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
data := h.configToExportData(config)
|
|
|
|
xlsxData, err := h.exportService.ToXLSX(data)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
filename := fmt.Sprintf("%s_%s.xlsx", config.Name, config.CreatedAt.Format("20060102"))
|
|
c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=%s", filename))
|
|
c.Data(http.StatusOK, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", xlsxData)
|
|
}
|
|
|
|
func (h *ExportHandler) configToExportData(config *models.Configuration) *services.ExportData {
|
|
items := make([]services.ExportItem, len(config.Items))
|
|
var total float64
|
|
|
|
for i, item := range config.Items {
|
|
itemTotal := item.UnitPrice * float64(item.Quantity)
|
|
items[i] = services.ExportItem{
|
|
LotName: item.LotName,
|
|
Quantity: item.Quantity,
|
|
UnitPrice: item.UnitPrice,
|
|
TotalPrice: itemTotal,
|
|
}
|
|
total += itemTotal
|
|
}
|
|
|
|
return &services.ExportData{
|
|
Name: config.Name,
|
|
Items: items,
|
|
Total: total,
|
|
Notes: config.Notes,
|
|
CreatedAt: config.CreatedAt,
|
|
}
|
|
}
|