diff --git a/backend/internal/api/handlers.go b/backend/internal/api/handlers.go index d1fb3be..3522f1a 100644 --- a/backend/internal/api/handlers.go +++ b/backend/internal/api/handlers.go @@ -160,8 +160,9 @@ func (h *Handlers) Status(w http.ResponseWriter, r *http.Request) { resp := statusResponse{Daemon: ds} if h.store != nil { + var statsStore statsReader = h.store currency := h.currentBaseCurrency(r.Context()) - if stats, err := h.store.GetStats(r.Context(), currency); err == nil { + if stats, err := statsStore.GetStats(r.Context(), currency); err == nil { resp.Stats = stats } } diff --git a/backend/internal/api/handlers_config.go b/backend/internal/api/handlers_config.go index bf77397..f5da7a9 100644 --- a/backend/internal/api/handlers_config.go +++ b/backend/internal/api/handlers_config.go @@ -61,6 +61,7 @@ func (h *Handlers) SetBaseCurrency(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusServiceUnavailable, "database not connected") return } + var configStore appConfigWriter = h.store var body struct { BaseCurrency string `json:"base_currency"` } @@ -79,7 +80,7 @@ func (h *Handlers) SetBaseCurrency(w http.ResponseWriter, r *http.Request) { return } } - if err := h.store.SetAppConfig(r.Context(), "base_currency", currency); err != nil { + if err := configStore.SetAppConfig(r.Context(), "base_currency", currency); err != nil { h.logger.Error("set base currency", "error", err) writeError(w, http.StatusInternalServerError, "failed to update base currency") return @@ -99,8 +100,9 @@ func (h *Handlers) GetScanInterval(w http.ResponseWriter, r *http.Request) { writeError(w, http.StatusServiceUnavailable, "database not connected") return } + var configStore appConfigReader = h.store val := strconv.Itoa(h.scanInterval) - if dbVal, err := h.store.GetAppConfig(r.Context(), "scan_interval"); err == nil && dbVal != "" { + if dbVal, err := configStore.GetAppConfig(r.Context(), "scan_interval"); err == nil && dbVal != "" { val = dbVal } writeJSON(w, http.StatusOK, map[string]string{"scan_interval": val}) @@ -119,11 +121,12 @@ func (h *Handlers) GetScanInterval(w http.ResponseWriter, r *http.Request) { // @Failure 500 {object} ErrorResponse // @Failure 503 {object} ErrorResponse // @Router /config/scan-interval [put] -func (h *Handlers) SetScanInterval(w http.ResponseWriter, r *http.Request) { //nolint:dupl // same shape as SetLookbackDays; different key and bounds +func (h *Handlers) SetScanInterval(w http.ResponseWriter, r *http.Request) { if h.store == nil { writeError(w, http.StatusServiceUnavailable, "database not connected") return } + var configStore appConfigWriter = h.store var body struct { ScanInterval string `json:"scan_interval"` } @@ -136,7 +139,7 @@ func (h *Handlers) SetScanInterval(w http.ResponseWriter, r *http.Request) { //n writeError(w, http.StatusBadRequest, "scan_interval must be an integer between 10 and 3600 seconds") return } - if err := h.store.SetAppConfig(r.Context(), "scan_interval", body.ScanInterval); err != nil { + if err := configStore.SetAppConfig(r.Context(), "scan_interval", body.ScanInterval); err != nil { h.logger.Error("set scan interval", "error", err) writeError(w, http.StatusInternalServerError, "failed to update scan interval") return @@ -176,7 +179,7 @@ func (h *Handlers) GetLookbackDays(w http.ResponseWriter, r *http.Request) { // @Failure 500 {object} ErrorResponse // @Failure 503 {object} ErrorResponse // @Router /config/lookback-days [put] -func (h *Handlers) SetLookbackDays(w http.ResponseWriter, r *http.Request) { //nolint:dupl // same shape as SetScanInterval; different key and bounds +func (h *Handlers) SetLookbackDays(w http.ResponseWriter, r *http.Request) { if h.store == nil { writeError(w, http.StatusServiceUnavailable, "database not connected") return diff --git a/backend/internal/api/http_helpers.go b/backend/internal/api/http_helpers.go index f7b8ee6..5bf6d20 100644 --- a/backend/internal/api/http_helpers.go +++ b/backend/internal/api/http_helpers.go @@ -14,7 +14,8 @@ import ( // currentBaseCurrency returns the base currency from the DB, falling back to INR. func (h *Handlers) currentBaseCurrency(ctx context.Context) string { if h.store != nil { - if val, err := h.store.GetAppConfig(ctx, "base_currency"); err == nil && val != "" { + var reader appConfigReader = h.store + if val, err := reader.GetAppConfig(ctx, "base_currency"); err == nil && val != "" { return val } } diff --git a/backend/internal/api/store_capabilities.go b/backend/internal/api/store_capabilities.go new file mode 100644 index 0000000..fdb73a6 --- /dev/null +++ b/backend/internal/api/store_capabilities.go @@ -0,0 +1,19 @@ +package api + +import ( + "context" + + "github.com/ArionMiles/expensor/backend/internal/store" +) + +type appConfigReader interface { + GetAppConfig(ctx context.Context, key string) (string, error) +} + +type appConfigWriter interface { + SetAppConfig(ctx context.Context, key, value string) error +} + +type statsReader interface { + GetStats(ctx context.Context, baseCurrency string) (*store.Stats, error) +} diff --git a/backend/internal/api/store_capabilities_test.go b/backend/internal/api/store_capabilities_test.go new file mode 100644 index 0000000..e78d0d3 --- /dev/null +++ b/backend/internal/api/store_capabilities_test.go @@ -0,0 +1,15 @@ +package api + +import ( + "github.com/ArionMiles/expensor/backend/internal/store" +) + +var ( + _ appConfigReader = (*store.Store)(nil) + _ appConfigWriter = (*store.Store)(nil) + _ statsReader = (*store.Store)(nil) + + _ appConfigReader = (*store.InstrumentedStore)(nil) + _ appConfigWriter = (*store.InstrumentedStore)(nil) + _ statsReader = (*store.InstrumentedStore)(nil) +)