From 8512ebd1050b849c699a53431b063d23ff560abc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?R=C3=A9mi=20Pailharey?= <rpailharey@grandlyon.com>
Date: Mon, 22 Nov 2021 11:23:59 +0100
Subject: [PATCH] fix(MonthlyReport): acces to monthlyReport using query params

---
 internal/models/monthlyReport.go | 13 +++++++++++--
 internal/rootmux/rootmux.go      |  2 +-
 internal/rootmux/rootmux_test.go |  6 ++++--
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/internal/models/monthlyReport.go b/internal/models/monthlyReport.go
index 4d19007..fca176d 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 8430785..ed176b2 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 9fa492d..1d1e0d5 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, "")
 }
 
 /**
-- 
GitLab