- Add price_updated_at field to qt_configurations table to track when prices were last updated - Add RefreshPrices() method in configuration service to update all component prices with current values from database - Add POST /api/configs/:uuid/refresh-prices API endpoint for price updates - Add "Refresh Prices" button in configurator UI next to Save button - Display last price update timestamp in human-readable format (e.g., "5 min ago", "2 hours ago") - Create migration 004_add_price_updated_at.sql for database schema update - Update CLAUDE.md documentation with new API endpoint and schema changes - Add MIGRATION_PRICE_REFRESH.md with detailed migration instructions Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
241 lines
6.0 KiB
Go
241 lines
6.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"git.mchus.pro/mchus/quoteforge/internal/middleware"
|
|
"git.mchus.pro/mchus/quoteforge/internal/services"
|
|
)
|
|
|
|
type ConfigurationHandler struct {
|
|
configService *services.ConfigurationService
|
|
exportService *services.ExportService
|
|
}
|
|
|
|
func NewConfigurationHandler(
|
|
configService *services.ConfigurationService,
|
|
exportService *services.ExportService,
|
|
) *ConfigurationHandler {
|
|
return &ConfigurationHandler{
|
|
configService: configService,
|
|
exportService: exportService,
|
|
}
|
|
}
|
|
|
|
func (h *ConfigurationHandler) List(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
perPage, _ := strconv.Atoi(c.DefaultQuery("per_page", "20"))
|
|
|
|
configs, total, err := h.configService.ListByUser(userID, page, perPage)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"configurations": configs,
|
|
"total": total,
|
|
"page": page,
|
|
"per_page": perPage,
|
|
})
|
|
}
|
|
|
|
func (h *ConfigurationHandler) Create(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
|
|
var req services.CreateConfigRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
config, err := h.configService.Create(userID, &req)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, config)
|
|
}
|
|
|
|
func (h *ConfigurationHandler) Get(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
config, err := h.configService.GetByUUID(uuid, userID)
|
|
if err != nil {
|
|
status := http.StatusNotFound
|
|
if err == services.ErrConfigForbidden {
|
|
status = http.StatusForbidden
|
|
}
|
|
c.JSON(status, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, config)
|
|
}
|
|
|
|
func (h *ConfigurationHandler) Update(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
var req services.CreateConfigRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
config, err := h.configService.Update(uuid, userID, &req)
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if err == services.ErrConfigNotFound {
|
|
status = http.StatusNotFound
|
|
} else if err == services.ErrConfigForbidden {
|
|
status = http.StatusForbidden
|
|
}
|
|
c.JSON(status, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, config)
|
|
}
|
|
|
|
func (h *ConfigurationHandler) Delete(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
err := h.configService.Delete(uuid, userID)
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if err == services.ErrConfigNotFound {
|
|
status = http.StatusNotFound
|
|
} else if err == services.ErrConfigForbidden {
|
|
status = http.StatusForbidden
|
|
}
|
|
c.JSON(status, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "deleted"})
|
|
}
|
|
|
|
type RenameConfigRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
}
|
|
|
|
func (h *ConfigurationHandler) Rename(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
var req RenameConfigRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
config, err := h.configService.Rename(uuid, userID, req.Name)
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if err == services.ErrConfigNotFound {
|
|
status = http.StatusNotFound
|
|
} else if err == services.ErrConfigForbidden {
|
|
status = http.StatusForbidden
|
|
}
|
|
c.JSON(status, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, config)
|
|
}
|
|
|
|
type CloneConfigRequest struct {
|
|
Name string `json:"name" binding:"required"`
|
|
}
|
|
|
|
func (h *ConfigurationHandler) Clone(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
var req CloneConfigRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
config, err := h.configService.Clone(uuid, userID, req.Name)
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if err == services.ErrConfigNotFound {
|
|
status = http.StatusNotFound
|
|
} else if err == services.ErrConfigForbidden {
|
|
status = http.StatusForbidden
|
|
}
|
|
c.JSON(status, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, config)
|
|
}
|
|
|
|
func (h *ConfigurationHandler) RefreshPrices(c *gin.Context) {
|
|
userID := middleware.GetUserID(c)
|
|
uuid := c.Param("uuid")
|
|
|
|
config, err := h.configService.RefreshPrices(uuid, userID)
|
|
if err != nil {
|
|
status := http.StatusInternalServerError
|
|
if err == services.ErrConfigNotFound {
|
|
status = http.StatusNotFound
|
|
} else if err == services.ErrConfigForbidden {
|
|
status = http.StatusForbidden
|
|
}
|
|
c.JSON(status, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, config)
|
|
}
|
|
|
|
// func (h *ConfigurationHandler) ExportJSON(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, err := h.configService.ExportJSON(uuid, userID)
|
|
// if err != nil {
|
|
// c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
// return
|
|
// }
|
|
//
|
|
// filename := fmt.Sprintf("%s %s SPEC.json", config.CreatedAt.Format("2006-01-02"), config.Name)
|
|
// c.Header("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
|
// c.Data(http.StatusOK, "application/json", data)
|
|
// }
|
|
|
|
// func (h *ConfigurationHandler) ImportJSON(c *gin.Context) {
|
|
// userID := middleware.GetUserID(c)
|
|
//
|
|
// data, err := io.ReadAll(c.Request.Body)
|
|
// if err != nil {
|
|
// c.JSON(http.StatusBadRequest, gin.H{"error": "failed to read body"})
|
|
// return
|
|
// }
|
|
//
|
|
// config, err := h.configService.ImportJSON(userID, data)
|
|
// if err != nil {
|
|
// c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
// return
|
|
// }
|
|
//
|
|
// c.JSON(http.StatusCreated, config)
|
|
// }
|