diff --git a/internal/models/monthlyReport.go b/internal/models/monthlyReport.go index 4d19007dcce7091512e1c08414b019d40827f06a..fca176d2c6a44cc44ca83a1ead3d2dfd02b24aa8 100644 --- a/internal/models/monthlyReport.go +++ b/internal/models/monthlyReport.go @@ -3,6 +3,7 @@ package models import ( "encoding/json" "net/http" + "strconv" "time" "forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common" @@ -32,8 +33,16 @@ type MonthlyReport struct { func (dh *DataHandler) GetMonthlyReport(w http.ResponseWriter, r *http.Request) { year, month, err := common.YearMonthFromRequest(r) if err != nil { - http.Error(w, err.Error(), http.StatusBadRequest) - return + year, err = strconv.Atoi(r.URL.Query().Get("year")) + if err != nil { + http.Error(w, "year in query is not an integer", http.StatusBadRequest) + return + } + month, err = strconv.Atoi(r.URL.Query().Get("month")) + if err != nil { + http.Error(w, "month in query is not an integer", http.StatusBadRequest) + return + } } monthlyReport, err := dh.getMonthlyReport(year, month) diff --git a/internal/rootmux/rootmux.go b/internal/rootmux/rootmux.go index 84307853fc0f8bb6e6803b2f4674d3625708fabd..ed176b2cc460be899746c0f9e4df1037239c0b6b 100644 --- a/internal/rootmux/rootmux.go +++ b/internal/rootmux/rootmux.go @@ -36,7 +36,7 @@ func CreateRootMux() RootMux { r.HandleFunc("/Logout", m.HandleLogout) r.Handle("/api/common/WhoAmI", auth.ValidateAuthMiddleware(auth.WhoAmI(), []string{"*"}, false)) - r.HandleFunc("/api/common/monthlyReport", dh.GetCurrentMonthlyReport).Methods(http.MethodGet) + r.HandleFunc("/api/common/monthlyReport", dh.GetMonthlyReport).Methods(http.MethodGet) r.HandleFunc("/api/common/monthlyReport/{year}/{month}", dh.GetMonthlyReport).Methods(http.MethodGet) apiAdmin := r.PathPrefix("/api/admin").Subrouter() diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go index 9fa492dc35d827089db7d6e0023fa7ff99e3260b..1d1e0d5faca97af165c7d4355e75783932a23767 100644 --- a/internal/rootmux/rootmux_test.go +++ b/internal/rootmux/rootmux_test.go @@ -94,8 +94,10 @@ func unloggedTests(t *testing.T) { // Try to create a monthlyNews (must fail) do("PUT", "/api/admin/monthlyNews", noH, monthlyNewsStr, http.StatusUnauthorized, "error extracting token") - // Try to get the most recent monthlyReport (must fail because not found) - do("GET", "/api/common/monthlyReport", noH, "", http.StatusNotFound, "") + // Try to get a monthlyReport (must fail because no parameters are given) + do("GET", "/api/common/monthlyReport", noH, "", http.StatusBadRequest, "") + // Try to get a monthlyReport (must fail because not found) + do("GET", "/api/common/monthlyReport?year=2021&month=12", noH, "", http.StatusNotFound, "") } /**