From 062236e5e484cc11691f27981264b119db825ce7 Mon Sep 17 00:00:00 2001 From: Daniel Ponte Date: Sun, 24 Nov 2024 21:18:11 -0500 Subject: [PATCH] Export endpoint --- pkg/rest/talkgroups.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pkg/rest/talkgroups.go b/pkg/rest/talkgroups.go index 9845764..21b443e 100644 --- a/pkg/rest/talkgroups.go +++ b/pkg/rest/talkgroups.go @@ -1,6 +1,7 @@ package rest import ( + "fmt" "net/http" "dynatron.me/x/stillbox/internal/forms" @@ -32,6 +33,8 @@ func (tga *talkgroupAPI) Subrouter() http.Handler { r.Post("/import", tga.tgImport) + r.Post("/export", tga.tgExport) + return r } @@ -156,6 +159,25 @@ func (tga *talkgroupAPI) put(w http.ResponseWriter, r *http.Request) { respond(w, r, record) } +func (tga *talkgroupAPI) tgExport(w http.ResponseWriter, r *http.Request) { + var expJob xport.ExportJob + ctx := r.Context() + + err := forms.Unmarshal(r, &expJob, forms.WithAcceptBlank(), forms.WithOmitEmpty()) + if err != nil { + wErr(w, r, badRequest(err)) + return + } + + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=stillbox_%s", expJob.TemplateFileName)) + w.Header().Set("Content-Type", "text/xml") + + err = expJob.Export(ctx, w) + if err != nil { + wErr(w, r, autoError(err)) + } +} + func (tga *talkgroupAPI) tgImport(w http.ResponseWriter, r *http.Request) { var impJob xport.ImportJob err := forms.Unmarshal(r, &impJob, forms.WithTag("json"), forms.WithAcceptBlank(), forms.WithOmitEmpty())