generated from coulomb/repo-seed
- Schema: hubs, widgets, widget_versions, interaction_events (append-only trigger), annotations, users — single migration file - Web layer: Types, Routes, FrontController with auth + AutoRefresh layout - Controllers: Hubs (CRUD), Widgets (CRUD + versioning), InteractionEvents (JSON capture, canonical event_type validation), Annotations (threaded, append-only) - Sessions controller for IHP auth - Views: Hubs (index/show/new/edit), Widgets (index/show/new/edit), Annotations (index/new), Sessions (login) - widgetEnvelope helper with full data-* governance attributes - Integration tests: Hub CRUD, Widget versioning, event capture, append-only guard, annotation threading, validation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
75 lines
2.5 KiB
Haskell
75 lines
2.5 KiB
Haskell
module Web.Controller.Hubs where
|
|
|
|
import Web.Types
|
|
import Web.View.Hubs.Index
|
|
import Web.View.Hubs.Show
|
|
import Web.View.Hubs.New
|
|
import Web.View.Hubs.Edit
|
|
import Generated.Types
|
|
import IHP.Prelude
|
|
import IHP.ControllerPrelude
|
|
|
|
instance Controller HubsController where
|
|
beforeAction = ensureIsUser
|
|
|
|
action HubsAction = do
|
|
hubs <- query @Hub |> orderByAsc #createdAt |> fetch
|
|
render IndexView { hubs }
|
|
|
|
action NewHubAction = do
|
|
let hub = newRecord @Hub
|
|
render NewView { hub }
|
|
|
|
action ShowHubAction { hubId } = autoRefresh do
|
|
hub <- fetch hubId
|
|
widgets <- query @Widget
|
|
|> filterWhere (#hubId, hubId)
|
|
|> orderByAsc #name
|
|
|> fetch
|
|
widgetIds <- pure (map (.id) widgets)
|
|
recentEvents <- sqlQuery
|
|
"SELECT * FROM interaction_events WHERE widget_id = ANY(?) ORDER BY occurred_at DESC LIMIT 50"
|
|
(Only (PGArray widgetIds))
|
|
recentAnnotations <- sqlQuery
|
|
"SELECT * FROM annotations WHERE widget_id = ANY(?) ORDER BY created_at DESC LIMIT 20"
|
|
(Only (PGArray widgetIds))
|
|
render ShowView { hub, widgets, recentEvents, recentAnnotations }
|
|
|
|
action CreateHubAction = do
|
|
let hub = newRecord @Hub
|
|
hub
|
|
|> fill @'["slug", "name", "domain"]
|
|
|> validateField #slug nonEmpty
|
|
|> validateField #name nonEmpty
|
|
|> validateField #domain nonEmpty
|
|
|> ifValid \case
|
|
Left hub -> render NewView { hub }
|
|
Right hub -> do
|
|
hub <- createRecord hub
|
|
setSuccessMessage "Hub created"
|
|
redirectTo ShowHubAction { hubId = hub.id }
|
|
|
|
action EditHubAction { hubId } = do
|
|
hub <- fetch hubId
|
|
render EditView { hub }
|
|
|
|
action UpdateHubAction { hubId } = do
|
|
hub <- fetch hubId
|
|
hub
|
|
|> fill @'["slug", "name", "domain"]
|
|
|> validateField #slug nonEmpty
|
|
|> validateField #name nonEmpty
|
|
|> validateField #domain nonEmpty
|
|
|> ifValid \case
|
|
Left hub -> render EditView { hub }
|
|
Right hub -> do
|
|
updateRecord hub
|
|
setSuccessMessage "Hub updated"
|
|
redirectTo ShowHubAction { hubId = hub.id }
|
|
|
|
action DeleteHubAction { hubId } = do
|
|
hub <- fetch hubId
|
|
deleteRecord hub
|
|
setSuccessMessage "Hub deleted"
|
|
redirectTo HubsAction
|