- CaseService: list (paginated, filterable), get detail (with parties, events, deadline count), create, update, soft-delete (archive) - PartyService: list by case, create, update, delete - Auto-create case_events on case creation, status change, party add, and case archive - Auth middleware now resolves tenant_id from user_tenants table - All operations scoped to tenant_id from auth context
75 lines
2.2 KiB
Go
75 lines
2.2 KiB
Go
package router
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/auth"
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/handlers"
|
|
"mgit.msbls.de/m/KanzlAI-mGMT/internal/services"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
func New(db *sqlx.DB, authMW *auth.Middleware) http.Handler {
|
|
mux := http.NewServeMux()
|
|
|
|
// Services
|
|
caseSvc := services.NewCaseService(db)
|
|
partySvc := services.NewPartyService(db)
|
|
|
|
// Handlers
|
|
caseH := handlers.NewCaseHandler(caseSvc)
|
|
partyH := handlers.NewPartyHandler(partySvc)
|
|
|
|
// Public routes
|
|
mux.HandleFunc("GET /health", handleHealth(db))
|
|
|
|
// Authenticated API routes
|
|
api := http.NewServeMux()
|
|
|
|
// Cases
|
|
api.HandleFunc("GET /api/cases", caseH.List)
|
|
api.HandleFunc("POST /api/cases", caseH.Create)
|
|
api.HandleFunc("GET /api/cases/{id}", caseH.Get)
|
|
api.HandleFunc("PUT /api/cases/{id}", caseH.Update)
|
|
api.HandleFunc("DELETE /api/cases/{id}", caseH.Delete)
|
|
|
|
// Parties (nested under cases for creation/listing, top-level for update/delete)
|
|
api.HandleFunc("GET /api/cases/{id}/parties", partyH.List)
|
|
api.HandleFunc("POST /api/cases/{id}/parties", partyH.Create)
|
|
api.HandleFunc("PUT /api/parties/{partyId}", partyH.Update)
|
|
api.HandleFunc("DELETE /api/parties/{partyId}", partyH.Delete)
|
|
|
|
// Placeholder routes for future phases
|
|
api.HandleFunc("GET /api/deadlines", placeholder("deadlines"))
|
|
api.HandleFunc("GET /api/appointments", placeholder("appointments"))
|
|
api.HandleFunc("GET /api/documents", placeholder("documents"))
|
|
|
|
mux.Handle("/api/", authMW.RequireAuth(api))
|
|
|
|
return mux
|
|
}
|
|
|
|
func handleHealth(db *sqlx.DB) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if err := db.Ping(); err != nil {
|
|
w.WriteHeader(http.StatusServiceUnavailable)
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "error", "error": err.Error()})
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
|
}
|
|
}
|
|
|
|
func placeholder(resource string) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
"status": "not_implemented",
|
|
"resource": resource,
|
|
})
|
|
}
|
|
}
|