From 44dd6724f651c9d90714ad3fe30c82cf61d5ec58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20PAILHAREY?= <rpailharey@grandlyon.com> Date: Wed, 1 Dec 2021 12:58:11 +0000 Subject: [PATCH] Feat/us555 partners check --- .vscode/settings.json | 22 ++++++++ internal/models/models.go | 11 ++++ internal/models/partnersInfo.go | 88 ++++++++++++++++++++++++++++++++ internal/rootmux/rootmux.go | 3 ++ internal/rootmux/rootmux_test.go | 27 +++++++--- 5 files changed, 143 insertions(+), 8 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 internal/models/partnersInfo.go diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f36e437 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,22 @@ +{ + "workbench.colorCustomizations": { + "activityBar.activeBackground": "#65f4ff", + "activityBar.activeBorder": "#ff40f1", + "activityBar.background": "#65f4ff", + "activityBar.foreground": "#15202b", + "activityBar.inactiveForeground": "#15202b99", + "activityBarBadge.background": "#ff40f1", + "activityBarBadge.foreground": "#15202b", + "sash.hoverBorder": "#65f4ff", + "statusBar.background": "#32f0ff", + "statusBar.foreground": "#15202b", + "statusBarItem.hoverBackground": "#00ebfe", + "statusBarItem.remoteBackground": "#32f0ff", + "statusBarItem.remoteForeground": "#15202b", + "titleBar.activeBackground": "#32f0ff", + "titleBar.activeForeground": "#15202b", + "titleBar.inactiveBackground": "#32f0ff99", + "titleBar.inactiveForeground": "#15202b99" + }, + "peacock.color": "#32f0ff" +} \ No newline at end of file diff --git a/internal/models/models.go b/internal/models/models.go index fc72cf8..9fffb4e 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -41,5 +41,16 @@ func NewDataHandler() *DataHandler { db.AutoMigrate(&MonthlyInfo{}) db.AutoMigrate(&MonthlyNews{}) db.AutoMigrate(&Poll{}) + db.AutoMigrate(&PartnersInfo{}) + + // Create default partner status + db.Create(&PartnersInfo{ + Message: "", + GRDFFailure: false, + EnedisFailure: false, + EGLFailure: false, + NotificationActivated: false, + }) + return &DataHandler{db: db} } diff --git a/internal/models/partnersInfo.go b/internal/models/partnersInfo.go new file mode 100644 index 0000000..0996fb1 --- /dev/null +++ b/internal/models/partnersInfo.go @@ -0,0 +1,88 @@ +package models + +import ( + "encoding/json" + "errors" + "log" + "net/http" + + "gorm.io/gorm" +) + +type PartnersInfo struct { + ID uint `gorm:"<-:create"` + Message string `json:"message"` + GRDFFailure bool `json:"grdf_failure"` + EnedisFailure bool `json:"enedis_failure"` + EGLFailure bool `json:"egl_failure"` + NotificationActivated bool `json:"notification_activated"` +} + +// GetPartnersInfo godoc +// @Summary Give status of partners' services +// @Description Give status of partners' services +// @Tags partnersInfo +// @Produce json +// @Success 200 {object} PartnersInfo +// @Failure 404 {string} string "Not found" +// @Router /api/common/partnersInfo [get] +func (dh *DataHandler) GetPartnersInfo(w http.ResponseWriter, r *http.Request) { + var partnersInfo PartnersInfo + err := dh.db.First(&partnersInfo).Error + + if errors.Is(err, gorm.ErrRecordNotFound) { + http.Error(w, "partners status not found", http.StatusNotFound) + return + } + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(partnersInfo) + log.Printf("| get partnersInfo | %v", r.RemoteAddr) +} + +// SavePartnersInfo godoc +// @Summary Update partnersInfo' content +// @Description Update partnersInfo' content +// @Tags partnersInfo +// @Accept json +// @Produce json +// @Success 200 {object} PartnersInfo "Updated successfully" +// @Failure 400 {string} string "Bad Request" +// @Failure 500 {string} string "Internal server error" +// @Param partnersInfo body PartnersInfo true "PartnersInfo to create/update with new content" +// @Router /api/admin/partnersInfo [put] +func (dh *DataHandler) SavePartnersInfo(w http.ResponseWriter, r *http.Request) { + if r.Body == http.NoBody { + http.Error(w, "request body is empty", http.StatusBadRequest) + return + } + + decoder := json.NewDecoder(r.Body) + var partnersInfo PartnersInfo + err := decoder.Decode(&partnersInfo) + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + var updatedPartnersInfo PartnersInfo + + err = dh.db.First(&updatedPartnersInfo).Error + if err != nil { + w.WriteHeader(http.StatusInternalServerError) + return + } + + updatedPartnersInfo.Message = partnersInfo.Message + updatedPartnersInfo.GRDFFailure = partnersInfo.GRDFFailure + updatedPartnersInfo.EnedisFailure = partnersInfo.EnedisFailure + updatedPartnersInfo.EGLFailure = partnersInfo.EGLFailure + updatedPartnersInfo.NotificationActivated = partnersInfo.NotificationActivated + + dh.db.Save(&updatedPartnersInfo) + + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(partnersInfo) + log.Printf("| updated partnersInfo | %v", r.RemoteAddr) + +} diff --git a/internal/rootmux/rootmux.go b/internal/rootmux/rootmux.go index 035192b..6264fbf 100644 --- a/internal/rootmux/rootmux.go +++ b/internal/rootmux/rootmux.go @@ -42,6 +42,7 @@ func CreateRootMux() RootMux { r.HandleFunc("/api/common/monthlyReport", dh.GetMonthlyReport).Methods(http.MethodGet) r.HandleFunc("/api/common/monthlyReport/{year}/{month}", dh.GetMonthlyReport).Methods(http.MethodGet) + r.HandleFunc("/api/common/partnersInfo", dh.GetPartnersInfo).Methods(http.MethodGet) apiAdmin := r.PathPrefix("/api/admin").Subrouter() apiAdmin.Use(auth.AdminAuthMiddleware) @@ -61,6 +62,8 @@ func CreateRootMux() RootMux { apiAdmin.HandleFunc("/poll", dh.SavePoll).Methods(http.MethodPut) apiAdmin.HandleFunc("/poll/{year}/{month}", dh.DeletePoll).Methods(http.MethodDelete) + apiAdmin.HandleFunc("/partnersInfo", dh.SavePartnersInfo).Methods(http.MethodPut) + apiAdmin.HandleFunc("/imageNames", file.GetEcogestureImages).Methods(http.MethodGet) // Swagger diff --git a/internal/rootmux/rootmux_test.go b/internal/rootmux/rootmux_test.go index 1d1e0d5..374f868 100644 --- a/internal/rootmux/rootmux_test.go +++ b/internal/rootmux/rootmux_test.go @@ -17,14 +17,16 @@ import ( ) var ( - oAuth2Server *httptest.Server - monthlyInfo = models.MonthlyInfo{Year: 2021, Month: 0, Info: "Informations du mois", Image: "imagebase64"} - monthlyInfoStr string - monthlyNews = models.MonthlyNews{Year: 2021, Month: 0, Title: "", Content: "Nouvelles fonctionnalités"} - monthlyNewsStr string - newPoll = models.Poll{Year: 2021, Month: 0, Question: "pollQuestion", Link: "pollLink"} - newPollStr string - noH map[string]string + oAuth2Server *httptest.Server + monthlyInfo = models.MonthlyInfo{Year: 2021, Month: 0, Info: "Informations du mois", Image: "imagebase64"} + monthlyInfoStr string + monthlyNews = models.MonthlyNews{Year: 2021, Month: 0, Title: "", Content: "Nouvelles fonctionnalités"} + monthlyNewsStr string + newPoll = models.Poll{Year: 2021, Month: 0, Question: "pollQuestion", Link: "pollLink"} + newPollStr string + partnersInfo = models.PartnersInfo{ID: 1, Message: "EGL est down", GRDFFailure: false, EnedisFailure: false, EGLFailure: true, NotificationActivated: true} + partnersInfoStr string + noH map[string]string ) func TestMain(m *testing.M) { @@ -51,6 +53,8 @@ func TestMain(m *testing.M) { monthlyNewsStr = string(monthlyNewsBytes) monthlyInfoBytes, _ := json.Marshal(monthlyInfo) monthlyInfoStr = string(monthlyInfoBytes) + partnersInfoBytes, _ := json.Marshal(partnersInfo) + partnersInfoStr = string(partnersInfoBytes) newPollBytes, _ := json.Marshal(newPoll) newPollStr = string(newPollBytes) @@ -98,6 +102,8 @@ func unloggedTests(t *testing.T) { 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, "") + // Try to get partnersInfo (must pass) + do("GET", "/api/common/partnersInfo", noH, "", http.StatusOK, `{"ID":1,"message":"","grdf_failure":false,"enedis_failure":false,"egl_failure":false,"notification_activated":false}`) } /** @@ -158,6 +164,11 @@ func adminTests(t *testing.T) { // Try to get the monthlyReport (must pass) do("GET", "/api/common/monthlyReport/2021/0", noH, "", http.StatusOK, `{"year":2021,"month":0,"info":"Informations du mois","image":"imagebase64","newsTitle":"Les nouveautés du service","newsContent":"Nouvelles fonctionnalités","question":"pollQuestion","link":"pollLink"`) + // Try to update the partnersInfo (must pass) + do("PUT", "/api/admin/partnersInfo", xsrfHeader, partnersInfoStr, http.StatusOK, partnersInfoStr) + // Try to get the monthlyInfo created (must pass) + do("GET", "/api/common/partnersInfo", xsrfHeader, "", http.StatusOK, partnersInfoStr) + // Try to delete the monthlyNews created (must pass) do("DELETE", "/api/admin/monthlyNews/2021/0", xsrfHeader, "", http.StatusOK, "successful delete") // Try to get a monthlyNews after it is deleted (must fail because not found) -- GitLab