Skip to content

Commit

Permalink
pfs-162: Refactors handlers so their names and files are more uniform (
Browse files Browse the repository at this point in the history
  • Loading branch information
denialanderror authored Oct 18, 2024
1 parent f09a7c1 commit 221b402
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"testing"
)

func TestDownloadSuccess(t *testing.T) {
func TestRequestReportHandlerSuccess(t *testing.T) {
form := url.Values{
"reportType": {"AccountsReceivable"},
"reportJournalType": {""},
Expand All @@ -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}
Expand All @@ -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}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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},
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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()))
Expand All @@ -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"},
Expand All @@ -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}
Expand All @@ -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)
Expand Down
12 changes: 7 additions & 5 deletions finance-admin/internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 221b402

Please sign in to comment.