Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/internal/api/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
13 changes: 8 additions & 5 deletions backend/internal/api/handlers_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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
Expand All @@ -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})
Expand All @@ -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"`
}
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion backend/internal/api/http_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
19 changes: 19 additions & 0 deletions backend/internal/api/store_capabilities.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions backend/internal/api/store_capabilities_test.go
Original file line number Diff line number Diff line change
@@ -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)
)