diff --git a/internal/handlers/pricing.go b/internal/handlers/pricing.go index 056cce1..9bcc7f0 100644 --- a/internal/handlers/pricing.go +++ b/internal/handlers/pricing.go @@ -68,6 +68,17 @@ func NewPricingHandler( } func (h *PricingHandler) GetStats(c *gin.Context) { + // Check if we're in offline mode + if h.statsRepo == nil || h.alertService == nil { + c.JSON(http.StatusOK, gin.H{ + "new_alerts_count": 0, + "top_components": []interface{}{}, + "trending_components": []interface{}{}, + "offline": true, + }) + return + } + newAlerts, _ := h.alertService.GetNewAlertsCount() topComponents, _ := h.statsRepo.GetTopComponents(10) trendingComponents, _ := h.statsRepo.GetTrendingComponents(10) @@ -86,6 +97,19 @@ type ComponentWithCount struct { } func (h *PricingHandler) ListComponents(c *gin.Context) { + // Check if we're in offline mode + if h.componentRepo == nil { + c.JSON(http.StatusOK, gin.H{ + "components": []ComponentWithCount{}, + "total": 0, + "page": 1, + "per_page": 20, + "offline": true, + "message": "Управление ценами доступно только в онлайн режиме", + }) + return + } + page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) perPage, _ := strconv.Atoi(c.DefaultQuery("per_page", "20")) @@ -213,6 +237,15 @@ func (h *PricingHandler) expandMetaPrices(metaPrices, excludeLot string) []strin } func (h *PricingHandler) GetComponentPricing(c *gin.Context) { + // Check if we're in offline mode + if h.componentRepo == nil || h.pricingService == nil { + c.JSON(http.StatusServiceUnavailable, gin.H{ + "error": "Управление ценами доступно только в онлайн режиме", + "offline": true, + }) + return + } + lotName := c.Param("lot_name") component, err := h.componentRepo.GetByLotName(lotName) @@ -248,6 +281,15 @@ type UpdatePriceRequest struct { } func (h *PricingHandler) UpdatePrice(c *gin.Context) { + // Check if we're in offline mode + if h.db == nil { + c.JSON(http.StatusServiceUnavailable, gin.H{ + "error": "Обновление цен доступно только в онлайн режиме", + "offline": true, + }) + return + } + var req UpdatePriceRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) @@ -409,6 +451,15 @@ func (h *PricingHandler) recalculateSinglePrice(lotName string) { } func (h *PricingHandler) RecalculateAll(c *gin.Context) { + // Check if we're in offline mode + if h.db == nil { + c.JSON(http.StatusServiceUnavailable, gin.H{ + "error": "Пересчёт цен доступен только в онлайн режиме", + "offline": true, + }) + return + } + // Set headers for SSE c.Header("Content-Type", "text/event-stream") c.Header("Cache-Control", "no-cache") @@ -667,6 +718,15 @@ type PreviewPriceRequest struct { } func (h *PricingHandler) PreviewPrice(c *gin.Context) { + // Check if we're in offline mode + if h.db == nil { + c.JSON(http.StatusServiceUnavailable, gin.H{ + "error": "Предпросмотр цены доступен только в онлайн режиме", + "offline": true, + }) + return + } + var req PreviewPriceRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})