Files
Jacob Mathison 8a4029058b Merged in jmathison/demo-table (pull request #213)
Add demo dashboard settings table and CRUD API

* Add demo dashboard settings table and CRUD API

- Migration 00000000000123: create demoDashboardSettings table (id, guid, key, value JSONB, isDeleted, timestamps)
- SQLC queries and repository for create, list, listByGuid, get, update, soft delete
- OpenAPI paths and schemas in queryAPI.yaml; regenerate api.gen.go (embedded spec)
- Handlers in api/queryAPI/demodashboardsettings.go; ConfigProvider GetDBQueries for repo access
- Unit tests for demo dashboard settings CRUD

* Fix sqlc-generated demo dashboard setting types

Align demo dashboard settings handlers with sqlc output (DemoDashboardSetting model + pointer slices) so CI codegen/build succeeds.

* Fix CI gosec and gofmt issues

- Suppress gosec G115 for TotalCount len->int32 cast (consistent with other list endpoints)
- gofmt mock GetDBQueries() in test helpers

* gofmt query API demo settings

* Tighten demo dashboard settings OpenAPI constraints

* Regenerate query API embedded spec

* refactor: generic UI settings (table, API, service) and three-layer architecture

- Replace demo-dashboard-settings with generic UI settings:
  - Table uiSettings with namespace (was guid), key, value JSONB; UNIQUE(namespace, key)
  - Migration 123: create_ui_settings (single migration, no rename)
  - OpenAPI: /ui-settings, UISettingsService, UISetting schemas with namespace
  - API types and handlers: CreateUISetting, ListUISettings, GetUISetting, UpdateUISetting, DeleteUISetting

- Add service layer (architecture fix):
  - internal/uisettings: Service with Create, List, ListByNamespace, Get, Update, SoftDelete
  - Controllers call s.svc.UISettings only; remove GetDBQueries from queryAPI ConfigProvider
  - Wire UISettings in Services, main.go, and testutils

- Repository: uisettings.sql + uisettings.sql.go, UISetting model; remove demodashboardsettings
- Controller: uisettings.go (replaces demodashboardsettings.go), ErrNotFound from uisettings package
- Tests: uisettings_test.go with namespace-based list/cre…
* Add UISetting model and generated queries for uiSettings table

- models.go: add UISetting struct (sqlc 1.30.0)
- uisettings.sql.go: generated CRUD for ui_settings feature

Made-with: Cursor

* Use repository.UiSetting to match sqlc-generated type

- service and API use repository.UiSetting (sqlc struct name for uiSettings table)
- uisettings.sql.go: keep CreateUISetting/GetUISetting etc. from query names, return *UiSetting
- Fixes Docker/CI build undefined repository.UISetting

* Fix import formatting for golangci-lint

* Lint fix

* uisettings list test

* tests fixes

* Merge main: resolve api.gen.go conflicts (UI settings + DeleteDocument)

* Doc updates and elimination of index redundancy


Approved-by: Jay Brown
2026-03-04 23:00:50 +00:00

166 lines
4.4 KiB
Go

package queryapi_test
import (
"encoding/json"
"net/http"
"testing"
queryapi "queryorchestration/api/queryAPI"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/test"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCreateUISetting(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
body := queryapi.UISettingCreate{
Namespace: "demo-dashboard:user1",
Key: "theme",
Value: map[string]interface{}{"dark": true},
}
ctx, rec := createContextWithJSONBody(t, body)
err := cons.CreateUISetting(ctx)
require.NoError(t, err)
assert.Equal(t, http.StatusCreated, rec.Code)
var resp queryapi.UISetting
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
assert.Equal(t, "demo-dashboard:user1", resp.Namespace)
assert.Equal(t, "theme", resp.Key)
assert.False(t, resp.IsDeleted)
assert.NotEqual(t, uuid.Nil, uuid.UUID(resp.Id))
}
func TestListUISettings(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
_, err := cfg.GetDBQueries().CreateUISetting(t.Context(), &repository.CreateUISettingParams{
Namespace: "list-ns",
Key: "k1",
Value: []byte(`{"a":1}`),
})
require.NoError(t, err)
ctx, rec := createContext(t)
params := queryapi.ListUISettingsParams{Namespace: strPtr("list-ns")}
err = cons.ListUISettings(ctx, params)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var resp queryapi.UISettingListResponse
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
require.Len(t, resp.Settings, 1)
assert.Equal(t, "list-ns", resp.Settings[0].Namespace)
assert.Equal(t, int32(1), resp.TotalCount)
}
func TestGetUISetting(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
row, err := cfg.GetDBQueries().CreateUISetting(t.Context(), &repository.CreateUISettingParams{
Namespace: "get-ns",
Key: "key",
Value: []byte(`{}`),
})
require.NoError(t, err)
ctx, _ := createContext(t)
err = cons.GetUISetting(ctx, queryapi.UISettingID(row.ID))
require.NoError(t, err)
}
func TestGetUISetting_NotFound(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
ctx, rec := createContext(t)
err := cons.GetUISetting(ctx, queryapi.UISettingID(uuid.New()))
require.Error(t, err)
httperr, ok := err.(*echo.HTTPError)
require.True(t, ok)
assert.Equal(t, http.StatusNotFound, httperr.Code)
_ = rec
}
func TestUpdateUISetting(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
row, err := cfg.GetDBQueries().CreateUISetting(t.Context(), &repository.CreateUISettingParams{
Namespace: "upd-ns",
Key: "k",
Value: []byte(`{"old":true}`),
})
require.NoError(t, err)
body := queryapi.UISettingUpdate{
Value: map[string]interface{}{"new": true},
}
ctx, rec := createContextWithJSONBody(t, body)
err = cons.UpdateUISetting(ctx, queryapi.UISettingID(row.ID))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var resp queryapi.UISetting
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &resp))
assert.Equal(t, "upd-ns", resp.Namespace)
}
func TestDeleteUISetting(t *testing.T) {
cfg := &ControllerConfig{}
test.CreateDB(t, cfg)
initializeTestConfig(t, cfg)
svc := createControllerServices(cfg)
cons := queryapi.NewControllers(svc, cfg)
row, err := cfg.GetDBQueries().CreateUISetting(t.Context(), &repository.CreateUISettingParams{
Namespace: "del-ns",
Key: "k",
Value: []byte(`{}`),
})
require.NoError(t, err)
ctx, rec := createContext(t)
err = cons.DeleteUISetting(ctx, queryapi.UISettingID(row.ID))
require.NoError(t, err)
assert.Equal(t, http.StatusNoContent, rec.Code)
_, err = cfg.GetDBQueries().GetUISetting(t.Context(), row.ID)
require.Error(t, err) // not found or no rows (soft-deleted)
}
func strPtr(s string) *string { return &s }