From 221b402dbf36e580c020c57e39138f7b4ed1eb7a Mon Sep 17 00:00:00 2001 From: Sam Joseph Date: Fri, 18 Oct 2024 09:15:26 +0100 Subject: [PATCH] pfs-162: Refactors handlers so their names and files are more uniform (#18) --- .../server/{download.go => post_request_report.go} | 4 ++-- ...{download_test.go => post_request_report_test.go} | 12 ++++++------ .../internal/server/{upload.go => post_upload.go} | 8 ++++---- .../server/{upload_test.go => post_upload_test.go} | 12 ++++++------ finance-admin/internal/server/server.go | 12 +++++++----- ...ng_letters.go => tab_annual_invoicing_letters.go} | 4 ++-- .../server/{get_downloads.go => tab_downloads.go} | 4 ++-- .../server/{get_uploads.go => tab_uploads.go} | 4 ++-- 8 files changed, 31 insertions(+), 29 deletions(-) rename finance-admin/internal/server/{download.go => post_request_report.go} (90%) rename finance-admin/internal/server/{download_test.go => post_request_report_test.go} (88%) rename finance-admin/internal/server/{upload.go => post_upload.go} (83%) rename finance-admin/internal/server/{upload_test.go => post_upload_test.go} (88%) rename finance-admin/internal/server/{get_annual_invoicing_letters.go => tab_annual_invoicing_letters.go} (70%) rename finance-admin/internal/server/{get_downloads.go => tab_downloads.go} (86%) rename finance-admin/internal/server/{get_uploads.go => tab_uploads.go} (77%) diff --git a/finance-admin/internal/server/download.go b/finance-admin/internal/server/post_request_report.go similarity index 90% rename from finance-admin/internal/server/download.go rename to finance-admin/internal/server/post_request_report.go index 8fbde47..e076154 100644 --- a/finance-admin/internal/server/download.go +++ b/finance-admin/internal/server/post_request_report.go @@ -7,11 +7,11 @@ import ( "net/http" ) -type GetDownloadHandler struct { +type RequestReportHandler struct { router } -func (h *GetDownloadHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { +func (h *RequestReportHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { ctx := getContext(r) params := r.Form diff --git a/finance-admin/internal/server/download_test.go b/finance-admin/internal/server/post_request_report_test.go similarity index 88% rename from finance-admin/internal/server/download_test.go rename to finance-admin/internal/server/post_request_report_test.go index 8839476..341b53f 100644 --- a/finance-admin/internal/server/download_test.go +++ b/finance-admin/internal/server/post_request_report_test.go @@ -11,7 +11,7 @@ import ( "testing" ) -func TestDownloadSuccess(t *testing.T) { +func TestRequestReportHandlerSuccess(t *testing.T) { form := url.Values{ "reportType": {"AccountsReceivable"}, "reportJournalType": {""}, @@ -37,14 +37,14 @@ func TestDownloadSuccess(t *testing.T) { appVars.EnvironmentVars.Prefix = "prefix" - sut := GetDownloadHandler{ro} + sut := RequestReportHandler{ro} err := sut.render(appVars, w, r) assert.Nil(t, err) } -func TestDownloadValidationErrors(t *testing.T) { +func TestRequestReportHandlerValidationErrors(t *testing.T) { assert := assert.New(t) client := &mockApiClient{} ro := &mockRoute{client: client} @@ -67,13 +67,13 @@ func TestDownloadValidationErrors(t *testing.T) { Path: "/add", } - sut := GetDownloadHandler{ro} + sut := RequestReportHandler{ro} err := sut.render(appVars, w, r) assert.Nil(err) assert.Equal("422 Unprocessable Entity", w.Result().Status) } -func TestDownloadStatusError(t *testing.T) { +func TestRequestReportHandlerStatusError(t *testing.T) { assert := assert.New(t) client := &mockApiClient{} ro := &mockRoute{client: client} @@ -92,7 +92,7 @@ func TestDownloadStatusError(t *testing.T) { Path: "/add", } - sut := GetDownloadHandler{ro} + sut := RequestReportHandler{ro} err := sut.render(appVars, w, r) assert.Nil(err) assert.Equal(http.StatusInternalServerError, w.Result().StatusCode) diff --git a/finance-admin/internal/server/upload.go b/finance-admin/internal/server/post_upload.go similarity index 83% rename from finance-admin/internal/server/upload.go rename to finance-admin/internal/server/post_upload.go index 892dc38..3e75b00 100644 --- a/finance-admin/internal/server/upload.go +++ b/finance-admin/internal/server/post_upload.go @@ -9,11 +9,11 @@ import ( "net/http" ) -type UploadHandler struct { +type UploadFormHandler struct { router } -func (h *UploadHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { +func (h *UploadFormHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { ctx := getContext(r) reportUploadType := r.PostFormValue("reportUploadType") @@ -43,7 +43,7 @@ func (h *UploadHandler) render(v AppVars, w http.ResponseWriter, r *http.Request } // handleError simplifies repetitive error handling in the render method. -func (h *UploadHandler) handleError(w http.ResponseWriter, r *http.Request, msg string, code int) error { +func (h *UploadFormHandler) handleError(w http.ResponseWriter, r *http.Request, msg string, code int) error { fileError := model.ValidationErrors{ "FileUpload": map[string]string{"required": msg}, } @@ -53,7 +53,7 @@ func (h *UploadHandler) handleError(w http.ResponseWriter, r *http.Request, msg } // handleUploadError processes specific upload-related errors. -func (h *UploadHandler) handleUploadError(w http.ResponseWriter, r *http.Request, err error) error { +func (h *UploadFormHandler) handleUploadError(w http.ResponseWriter, r *http.Request, err error) error { var ( valErr model.ValidationError stErr api.StatusError diff --git a/finance-admin/internal/server/upload_test.go b/finance-admin/internal/server/post_upload_test.go similarity index 88% rename from finance-admin/internal/server/upload_test.go rename to finance-admin/internal/server/post_upload_test.go index c6ce35a..537fe28 100644 --- a/finance-admin/internal/server/upload_test.go +++ b/finance-admin/internal/server/post_upload_test.go @@ -10,7 +10,7 @@ import ( "testing" ) -func TestUploadNoFileUploaded(t *testing.T) { +func TestUploadFormHandlerNoFileUploaded(t *testing.T) { form := url.Values{ "reportUploadType": {"DEBT_CHASE"}, "uploadDate": {"2024-01-24"}, @@ -20,7 +20,7 @@ func TestUploadNoFileUploaded(t *testing.T) { client := mockApiClient{} ro := &mockRoute{client: client} - sut := UploadHandler{ro} + sut := UploadFormHandler{ro} w := httptest.NewRecorder() r, _ := http.NewRequest(http.MethodPost, "/uploads", strings.NewReader(form.Encode())) @@ -39,7 +39,7 @@ func TestUploadNoFileUploaded(t *testing.T) { assert.Nil(t, err) } -func TestUploadSuccess(t *testing.T) { +func TestUploadFormHandlerSuccess(t *testing.T) { form := url.Values{ "reportUploadType": {"DEBT_CHASE"}, "uploadDate": {"2024-01-24"}, @@ -58,12 +58,12 @@ func TestUploadSuccess(t *testing.T) { } appVars.EnvironmentVars.Prefix = "prefix" - sut := UploadHandler{ro} + sut := UploadFormHandler{ro} err := sut.render(appVars, w, r) assert.Nil(t, err) } -func TestUploadValidationErrors(t *testing.T) { +func TestUploadFormHandlerValidationErrors(t *testing.T) { assert := assert.New(t) client := &mockApiClient{} ro := &mockRoute{client: client} @@ -86,7 +86,7 @@ func TestUploadValidationErrors(t *testing.T) { Path: "/uploads", } - sut := UploadHandler{ro} + sut := UploadFormHandler{ro} err := sut.render(appVars, w, r) assert.Nil(err) assert.Equal("400 Bad Request", w.Result().Status) diff --git a/finance-admin/internal/server/server.go b/finance-admin/internal/server/server.go index e9b280e..d2b882a 100644 --- a/finance-admin/internal/server/server.go +++ b/finance-admin/internal/server/server.go @@ -38,12 +38,14 @@ func New(logger *slog.Logger, client ApiClient, templates map[string]*template.T mux.Handle(pattern, telemetry.Middleware(logger)(errors(h))) } - handleMux("GET /downloads", &GetDownloadsHandler{&route{client: client, tmpl: templates["downloads.gotmpl"], partial: "downloads"}}) - handleMux("GET /uploads", &GetUploadsHandler{&route{client: client, tmpl: templates["uploads.gotmpl"], partial: "uploads"}}) - handleMux("GET /annual-invoicing-letters", &GetAnnualInvoicingLettersHandler{&route{client: client, tmpl: templates["annual_invoicing_letters.gotmpl"], partial: "annual-invoicing-letters"}}) - handleMux("GET /download", &GetDownloadHandler{&route{client: client, tmpl: templates["downloads.gotmpl"], partial: "error-summary"}}) + // tabs + handleMux("GET /downloads", &DownloadsTabHandler{&route{client: client, tmpl: templates["downloads.gotmpl"], partial: "downloads"}}) + handleMux("GET /uploads", &UploadsTabHandler{&route{client: client, tmpl: templates["uploads.gotmpl"], partial: "uploads"}}) + handleMux("GET /annual-invoicing-letters", &AnnualInvoicingLettersTabHandler{&route{client: client, tmpl: templates["annual_invoicing_letters.gotmpl"], partial: "annual-invoicing-letters"}}) - handleMux("POST /uploads", &UploadHandler{&route{client: client, tmpl: templates["uploads.gotmpl"], partial: "error-summary"}}) + //forms + handleMux("POST /request-report", &RequestReportHandler{&route{client: client, tmpl: templates["downloads.gotmpl"], partial: "error-summary"}}) + handleMux("POST /uploads", &UploadFormHandler{&route{client: client, tmpl: templates["uploads.gotmpl"], partial: "error-summary"}}) mux.Handle("/health-check", healthCheck()) diff --git a/finance-admin/internal/server/get_annual_invoicing_letters.go b/finance-admin/internal/server/tab_annual_invoicing_letters.go similarity index 70% rename from finance-admin/internal/server/get_annual_invoicing_letters.go rename to finance-admin/internal/server/tab_annual_invoicing_letters.go index 713e092..acaf004 100644 --- a/finance-admin/internal/server/get_annual_invoicing_letters.go +++ b/finance-admin/internal/server/tab_annual_invoicing_letters.go @@ -8,11 +8,11 @@ type GetAnnualInvoicingLettersVars struct { AppVars } -type GetAnnualInvoicingLettersHandler struct { +type AnnualInvoicingLettersTabHandler struct { router } -func (h *GetAnnualInvoicingLettersHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { +func (h *AnnualInvoicingLettersTabHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { data := GetAnnualInvoicingLettersVars{v} data.selectTab("annual-invoicing-letters") return h.execute(w, r, data) diff --git a/finance-admin/internal/server/get_downloads.go b/finance-admin/internal/server/tab_downloads.go similarity index 86% rename from finance-admin/internal/server/get_downloads.go rename to finance-admin/internal/server/tab_downloads.go index 945eaac..6e1ccd2 100644 --- a/finance-admin/internal/server/get_downloads.go +++ b/finance-admin/internal/server/tab_downloads.go @@ -14,11 +14,11 @@ type GetDownloadsVars struct { AppVars } -type GetDownloadsHandler struct { +type DownloadsTabHandler struct { router } -func (h *GetDownloadsHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { +func (h *DownloadsTabHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { data := GetDownloadsVars{ model.ReportsTypes, model.ReportJournalTypes, diff --git a/finance-admin/internal/server/get_uploads.go b/finance-admin/internal/server/tab_uploads.go similarity index 77% rename from finance-admin/internal/server/get_uploads.go rename to finance-admin/internal/server/tab_uploads.go index 427e32b..12b9d09 100644 --- a/finance-admin/internal/server/get_uploads.go +++ b/finance-admin/internal/server/tab_uploads.go @@ -10,11 +10,11 @@ type GetUploadsVars struct { AppVars } -type GetUploadsHandler struct { +type UploadsTabHandler struct { router } -func (h *GetUploadsHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { +func (h *UploadsTabHandler) render(v AppVars, w http.ResponseWriter, r *http.Request) error { data := GetUploadsVars{shared.ReportUploadTypes, v} data.selectTab("uploads") return h.execute(w, r, data)