Skip to content
Snippets Groups Projects
Commit 26149aa0 authored by Rémi PAILHAREY's avatar Rémi PAILHAREY :fork_knife_plate:
Browse files

Merge branch 'dev'

parent cbcaa99c
No related branches found
No related tags found
No related merge requests found
......@@ -14,7 +14,7 @@
"host": "127.0.0.1",
"program": "${workspaceFolder}/main.go",
"env": {
"REDIRECT_URL": "https://localhost:1443/OAuth2Callback",
"REDIRECT_URL": "http://localhost:1443/OAuth2Callback",
"CLIENT_ID": "foo",
"CLIENT_SECRET": "bar",
"AUTH_URL": "http://localhost:8090/auth",
......@@ -26,10 +26,11 @@
"INMEMORY_TOKEN_LIFE_DAYS": "2",
"DEBUG_MODE": "true",
"HTTPS_PORT": "1443",
"DATABASE_USER": "root",
"DATABASE_USER": "",
"DATABASE_PASSWORD": "password",
"DATABASE_NAME": "backoffice",
"DATABASE_HOST": "127.0.0.1",
"MOCK_OAUTH2": "true"
},
"showLog": true
},
......
......@@ -27,3 +27,23 @@ However this backend can be run in standalone :
- Run `docker-compose up -d`
Once deployed, you can access to a Swagger documentation of the API on https://${HOSTNAME}/swagger/index.html
## Launch locally
To launch it locally :
- Run `docker-compose up -d`
In local you can access phpmyadmin interface to manage the database : http://localhost:8008
## Launch locally in standalone for development
To launch the backend for development goal:
- edit *main.go* file, uncomment the last line "log.Fatal(http.ListenAndServe(":"+strconv.Itoa(httpsPort), rootMux.Router))" and comment the line above "log.Fatal(http.ListenAndServeTLS(":"+strconv.Itoa(httpsPort), "./dev_certificates/localhost.crt", "./dev_certificates/localhost.key", rootMux.Router))"
- This way you disable https so make sure your requests goes on http adresses
- in *vscode/launch.json* set "REDIRECT_URL" to "http://localhost:1443/OAuth2Callback",
- also comment the port and host values
- if you comment the DATABASE_USER line, it will launches with a sqlite database instead of mysql
- launch the app by pressing F5 on vscode, you will see the logs on the debug console.
- this way you won't have the login every time you relaunch the backend for development
LOAD DATA LOCAL INFILE '/fluidprices.CSV' INTO TABLE prices
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
\ No newline at end of file
fluid_type,price,start_date,end_date,id,created_at,updated_at
0,0.1256,2012-07-23T00:00:00Z,2013-07-31T23:59:59Z,1,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1329,2013-08-01T00:00:00Z,2014-10-31T23:59:59Z,2,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1401,2014-01-11T00:00:00Z,2015-07-31T23:59:59Z,3,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1437,2015-08-01T00:00:00Z,2016-07-31T23:59:59Z,4,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1503,2016-08-01T00:00:00Z,2017-07-31T23:59:59Z,5,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1546,2017-08-01T00:00:00Z,2018-01-31T23:59:59Z,6,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1555,2018-02-01T00:00:00Z,2018-07-31T23:59:59Z,7,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.145,2018-08-01T00:00:00Z,2019-05-31T23:59:59Z,8,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1531,2019-06-01T00:00:00Z,2019-07-31T23:59:59Z,9,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1524,2019-08-01T00:00:00Z,2020-01-31T23:59:59Z,10,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1546,2020-02-01T00:00:00Z,2020-07-31T23:59:59Z,11,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1557,2020-08-01T00:00:00Z,2021-01-31T23:59:59Z,12,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1582,2021-02-01T00:00:00Z,2021-07-31T23:59:59Z,13,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.1558,2021-08-01T00:00:00Z,2022-01-31T23:59:59Z,14,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
0,0.174,2022-02-01T00:00:00Z,,15,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.0030735,2012-01-01T00:00:00Z,2012-12-31T23:59:59Z,16,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.0031483,2013-01-01T00:00:00Z,2013-12-31T23:59:59Z,17,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.0031381,2014-01-01T00:00:00Z,2014-12-31T23:59:59Z,18,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.00307,2015-01-01T00:00:00Z,2015-12-31T23:59:59Z,19,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.0031,2016-01-01T00:00:00Z,2016-12-31T23:59:59Z,20,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.00311,2017-01-01T00:00:00Z,2017-12-31T23:59:59Z,21,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.00313,2018-01-01T00:00:00Z,2018-12-31T23:59:59Z,22,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.00313,2019-01-01T00:00:00Z,2019-12-31T23:59:59Z,23,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.00315,2020-01-01T00:00:00Z,2020-12-31T23:59:59Z,24,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
1,0.00319,2021-01-01T00:00:00Z,,25,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0919,2017-01-01T00:00:00Z,2017-01-31T23:59:59Z,26,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0915,2017-02-01T00:00:00Z,2017-02-28T23:59:59Z,27,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0932,2017-03-01T00:00:00Z,2017-03-31T23:59:59Z,28,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0927,2017-04-01T00:00:00Z,2017-04-30T23:59:59Z,29,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0906,2017-05-01T00:00:00Z,2017-05-31T23:59:59Z,30,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0906,2017-06-01T00:00:00Z,2017-06-30T23:59:59Z,31,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0788,2017-07-01T00:00:00Z,2017-07-31T23:59:59Z,32,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0783,2017-08-01T00:00:00Z,2017-08-31T23:59:59Z,33,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0783,2017-09-01T00:00:00Z,2017-09-30T23:59:59Z,34,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0791,2017-10-01T00:00:00Z,2017-10-31T23:59:59Z,35,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0806,2017-11-01T00:00:00Z,2017-11-30T23:59:59Z,36,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0812,2017-12-01T00:00:00Z,2017-12-31T23:59:59Z,37,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0857,2018-01-01T00:00:00Z,2018-01-31T23:59:59Z,38,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0866,2018-02-01T00:00:00Z,2018-02-28T23:59:59Z,39,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0847,2018-03-01T00:00:00Z,2018-03-31T23:59:59Z,40,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0839,2018-04-01T00:00:00Z,2018-04-30T23:59:59Z,41,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0842,2018-05-01T00:00:00Z,2018-05-31T23:59:59Z,42,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0855,2018-06-01T00:00:00Z,2018-06-30T23:59:59Z,43,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0959,2018-07-01T00:00:00Z,2018-07-31T23:59:59Z,44,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0961,2018-08-01T00:00:00Z,2018-08-31T23:59:59Z,45,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0967,2018-09-01T00:00:00Z,2018-09-30T23:59:59Z,46,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0989,2018-10-01T00:00:00Z,2018-10-31T23:59:59Z,47,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.1031,2018-11-01T00:00:00Z,2018-11-30T23:59:59Z,48,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.1013,2018-12-01T00:00:00Z,2018-12-31T23:59:59Z,49,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0999,2019-01-01T00:00:00Z,2019-01-31T23:59:59Z,50,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0993,2019-02-01T00:00:00Z,2019-02-28T23:59:59Z,51,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0993,2019-03-01T00:00:00Z,2019-03-31T23:59:59Z,52,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0977,2019-04-01T00:00:00Z,2019-04-30T23:59:59Z,53,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0973,2019-05-01T00:00:00Z,2019-05-31T23:59:59Z,54,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0969,2019-06-01T00:00:00Z,2019-06-30T23:59:59Z,55,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0795,2019-07-01T00:00:00Z,2019-07-31T23:59:59Z,56,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0791,2019-08-01T00:00:00Z,2019-08-31T23:59:59Z,57,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0785,2019-09-01T00:00:00Z,2019-09-30T23:59:59Z,58,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.077,2019-10-01T00:00:00Z,2019-10-31T23:59:59Z,59,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0789,2019-11-01T00:00:00Z,2019-11-30T23:59:59Z,60,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0793,2019-12-01T00:00:00Z,2019-12-31T23:59:59Z,61,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0787,2020-01-01T00:00:00Z,2020-01-31T23:59:59Z,62,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0765,2020-02-01T00:00:00Z,2020-02-29T23:59:59Z,70,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0736,2020-03-01T00:00:00Z,2020-03-31T23:59:59Z,71,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.071,2020-04-01T00:00:00Z,2020-04-30T23:59:59Z,72,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0703,2020-05-01T00:00:00Z,2020-05-31T23:59:59Z,73,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0687,2020-06-01T00:00:00Z,2020-06-30T23:59:59Z,74,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0698,2020-07-01T00:00:00Z,2020-07-31T23:59:59Z,75,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0705,2020-08-01T00:00:00Z,2020-08-31T23:59:59Z,76,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0709,2020-09-01T00:00:00Z,2020-09-30T23:59:59Z,77,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0735,2020-10-01T00:00:00Z,2020-10-31T23:59:59Z,78,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0745,2020-11-01T00:00:00Z,2020-11-30T23:59:59Z,79,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0759,2020-12-01T00:00:00Z,2020-12-31T23:59:59Z,80,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.076,2021-01-01T00:00:00Z,2021-01-31T23:59:59Z,81,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0782,2021-02-01T00:00:00Z,2021-02-28T23:59:59Z,82,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0818,2021-03-01T00:00:00Z,2021-03-31T23:59:59Z,83,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.079,2021-04-01T00:00:00Z,2021-04-30T23:59:59Z,84,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0797,2021-05-01T00:00:00Z,2021-05-31T23:59:59Z,85,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0826,2021-06-01T00:00:00Z,2021-06-30T23:59:59Z,86,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0895,2021-07-01T00:00:00Z,2021-07-31T23:59:59Z,87,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.0934,2021-08-01T00:00:00Z,2021-08-31T23:59:59Z,88,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.1002,2021-09-01T00:00:00Z,2021-09-30T23:59:59Z,89,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
2,0.1121,2021-10-01T00:00:00Z,,90,2000-01-01T00:00:00Z,2000-01-01T00:00:00Z
\ No newline at end of file
# Init first prices data
## Local
If the script is not working (problem with secure_file_priv variable), use the import in phpMyAdmin:
- Ignore the request for the first line
- Select format "CVS using LOAD DATA"
- Choose the right column separator ","
- Then launch the execution
## Rec/Prod
- Connect to mysql docker container
```
docker exec -it <container-id> bash
```
- Open mysql shell
```
mysql --local-infile=1 -uroot -p backoffice < /dbinit/dbinit.sql
```
......@@ -13,6 +13,8 @@ services:
interval: 5s
timeout: 10s
retries: 60
volumes:
- ./dbinit:/dbinit
phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
......
......@@ -4,10 +4,10 @@ go 1.15
require (
github.com/gorilla/mux v1.8.0
github.com/swaggo/http-swagger v1.1.1
github.com/swaggo/swag v1.7.1
golang.org/x/oauth2 v0.0.0-20210819190943-2bc19b11175f
gorm.io/driver/mysql v1.1.2
gorm.io/driver/sqlite v1.1.4
gorm.io/gorm v1.21.14
github.com/swaggo/http-swagger v1.2.7
github.com/swaggo/swag v1.8.1
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5
gorm.io/driver/mysql v1.3.3
gorm.io/driver/sqlite v1.3.2
gorm.io/gorm v1.23.5
)
This diff is collapsed.
......@@ -171,3 +171,23 @@ func YearMonthFromRequest(r *http.Request) (year int, month int, err error) {
return year, month, nil
}
//Get fluidType from Request
func FluidTypeFromRequest(r *http.Request) (fluidtype int, err error) {
vars := mux.Vars(r)
fluidTypeStr := vars["fluidtype"]
if fluidTypeStr == "" {
return 100, errors.New("missing query element")
}
fluidtype, err = strconv.Atoi(fluidTypeStr)
if err != nil {
return 100, errors.New("fluidType is not an integer")
}
return fluidtype, nil
}
package models
import (
"encoding/json"
"log"
"net/http"
"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common"
)
type MailSubject struct {
Year int `json:"year"`
Month int `json:"month"`
Subject string `json:"subject"`
}
// GetSingleMailSubject godoc
// @Summary Get details of a specific mailSubject
// @Description Get details of a specific mailSubject
// @Tags mailSubject
// @Produce json
// @Success 200 {object} MailSubject
// @Failure 404 {string} string "Not found"
// @Param year path int true "Year of the mailSubject"
// @Param month path int true "Month of the mailSubject"
// @Router /api/admin/mailSubject/{year}/{month} [get]
func (dh *DataHandler) GetSingleMailSubject(w http.ResponseWriter, r *http.Request) {
year, month, err := common.YearMonthFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
var mailSubject MailSubject
err = dh.db.Where("year = ? AND month = ?", year, month).First(&mailSubject).Error
if err != nil {
// If not found, answer "not found"
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(mailSubject)
}
// SaveMailSubject godoc
// @Summary Create/update a specific mailSubject' content
// @Description Create/update a specific mailSubject' content
// @Tags mailSubject
// @Accept json
// @Produce json
// @Success 200 {object} MailSubject "Updated successfully"
// @Success 201 {object} MailSubject "Created successfully"
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal server error"
// @Param mailSubject body MailSubject true "MailSubject to create/update with new content"
// @Router /api/admin/mailSubject [put]
func (dh *DataHandler) SaveMailSubject(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 mailSubject MailSubject
err := decoder.Decode(&mailSubject)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// Check if this mailSubject exists
err = dh.db.Where("year = ? AND month = ?", mailSubject.Year, mailSubject.Month).First(&MailSubject{}).Error
if err != nil {
// Create a mailSubject
err = dh.db.Create(&MailSubject{
Year: mailSubject.Year,
Month: mailSubject.Month,
Subject: mailSubject.Subject,
}).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(mailSubject)
log.Printf("| new mailSubject | year : %d month : %d | %v", mailSubject.Year, mailSubject.Month, r.RemoteAddr)
return
} else {
// Update object
err = dh.db.Model(&MailSubject{}).Where("year = ? AND month = ?", mailSubject.Year, mailSubject.Month).Update("subject", mailSubject.Subject).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(mailSubject)
log.Printf("| updated mailSubject | year : %d month : %d | %v", mailSubject.Year, mailSubject.Month, r.RemoteAddr)
}
}
// DeleteMailSubject godoc
// @Summary Delete a specific mailSubject
// @Description Delete a specific mailSubject
// @Tags mailSubject
// @Produce json
// @Success 200 {string} string "successful delete"
// @Failure 404 {string} string "Not found"
// @Param year path int true "Year of the mailSubject"
// @Param month path int true "Month of the mailSubject"
// @Router /api/admin/mailSubject/{year}/{month} [delete]
func (dh *DataHandler) DeleteMailSubject(w http.ResponseWriter, r *http.Request) {
year, month, err := common.YearMonthFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = dh.db.Where("year = ? AND month = ?", year, month).Delete(&MailSubject{}).Error
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("successful delete"))
}
......@@ -38,10 +38,12 @@ func NewDataHandler() *DataHandler {
}
// Migrate the schema
db.AutoMigrate(&MailSubject{})
db.AutoMigrate(&MonthlyInfo{})
db.AutoMigrate(&MonthlyNews{})
db.AutoMigrate(&Poll{})
db.AutoMigrate(&PartnersInfo{})
db.AutoMigrate(&Price{})
// Create default partner status
db.Create(&PartnersInfo{
......@@ -50,6 +52,5 @@ func NewDataHandler() *DataHandler {
EGLFailure: false,
NotificationActivated: false,
})
return &DataHandler{db: db}
}
......@@ -2,6 +2,7 @@ package models
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
......@@ -12,6 +13,7 @@ import (
type MonthlyReport struct {
Year int `json:"year"`
Month int `json:"month"`
Subject string `json:"subject"`
Info string `json:"info"`
Image string `json:"image"`
NewsTitle string `json:"newsTitle"`
......@@ -84,6 +86,13 @@ func (dh *DataHandler) getMonthlyReport(year int, month int) (monthlyReport Mont
return MonthlyReport{}, err
}
var mailSubject MailSubject
err = dh.db.Where("year = ? AND month = ?", year, month).First(&mailSubject).Error
if err != nil {
// Build default object
mailSubject.Subject = fmt.Sprintf("[Ecolyo] Votre bilan %s %v", getMonthNameWithPrep(month-1), year)
}
var monthlyNews MonthlyNews
dh.db.Where("year = ? AND month = ?", year, month).First(&monthlyNews)
......@@ -93,6 +102,7 @@ func (dh *DataHandler) getMonthlyReport(year int, month int) (monthlyReport Mont
monthlyReport = MonthlyReport{
Year: year,
Month: month,
Subject: mailSubject.Subject,
Info: monthlyInfo.Info,
Image: monthlyInfo.Image,
NewsTitle: monthlyNews.Title,
......@@ -103,3 +113,21 @@ func (dh *DataHandler) getMonthlyReport(year int, month int) (monthlyReport Mont
return monthlyReport, nil
}
func getMonthNameWithPrep(month int) string {
frenchMonths := []string{
"de janvier",
"de février",
"de mars",
"d'avril",
"de mai",
"de juin",
"de juillet",
"d'août",
"de septembre",
"d'octobre",
"de novembre",
"de décembre",
}
return frenchMonths[month]
}
package models
import (
"encoding/json"
"log"
"net/http"
"time"
"forge.grandlyon.com/web-et-numerique/llle_project/backoffice-server/internal/common"
"gorm.io/gorm"
)
type Price struct {
FluidType int `json:"fluidType"`
Price float32 `json:"price"`
StartDate string `json:"startDate"`
EndDate string `json:"endDate"`
gorm.Model
}
// GetPrices godoc
// @Summary Get all prices
// @Description Get all prices
// @Tags prices
// @Produce json
// @Success 200 {object} Prices
// @Failure 404 {string} string "Not found"
// @Router /api/common/prices [get]
func (dh *DataHandler) GetAllPrices(w http.ResponseWriter, r *http.Request) {
var prices []Price
dh.db.Find(&prices)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(prices)
log.Printf("| Get all prices | %v", r.RemoteAddr)
}
// GetPricesByFluid godoc
// @Summary Get all prices for a given fluid
// @Description Get all prices for a given fluid
// @Tags prices
// @Produce json
// @Success 200 {object} Prices
// @Failure 404 {string} string "Not found"
// @Param fluidtype path int true "FluidType requested"
// @Router /api/common/prices/fluidType [get]
func (dh *DataHandler) GetPricesByFluid(w http.ResponseWriter, r *http.Request) {
fluidtype, err := common.FluidTypeFromRequest(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var price []Price
w.Header().Set("Content-Type", "application/json")
err = dh.db.Where("fluid_type = ? ", fluidtype).Order("start_date desc").Find(&price).Error
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
json.NewEncoder(w).Encode(price)
log.Printf("| Get prices by fluid : %v | %v",fluidtype, r.RemoteAddr)
}
// SavePrice godoc
// @Summary Add a new price entry
// @Description Add a new price entry
// @Tags prices
// @Accept json
// @Produce json
// @Success 200 {object} Prices "Updated successfully"
// @Failure 400 {string} string "Bad Request"
// @Failure 500 {string} string "Internal server error"
// @Param price body Price true "Price to create/update with new content"
// @Router /api/admin/price [put]
func (dh *DataHandler) SavePrice(w http.ResponseWriter, r *http.Request) {
if r.Body == http.NoBody {
http.Error(w, "request body price is empty", http.StatusBadRequest)
return
}
decoder := json.NewDecoder(r.Body)
var price Price
err := decoder.Decode(&price)
if err != nil {
println("JSON decoding error")
w.WriteHeader(http.StatusInternalServerError)
return
}
// Check if this price exists
err = dh.db.Where("fluid_type = ? AND start_date = ?", price.FluidType, price.StartDate).First(&Price{}).Error
if err != nil {
println("Price does not exist in database")
//Gets the last price for this fluid
var lastPrice Price
err = dh.db.Where("fluid_type = ?", price.FluidType).Order("start_date desc").Limit(1).Find(&lastPrice).Error
if err != nil {
println("Failed to get last price for this fluid")
w.WriteHeader(http.StatusInternalServerError)
return
}
// Replace previous endDate from null to startDate - 1 minute
t, _ := time.Parse(time.RFC3339, price.StartDate)
updatedEndDate := t.Add(-1 * time.Minute).Format(time.RFC3339)
lastPrice.EndDate = updatedEndDate
dh.db.Save(&lastPrice)
log.Printf("updated last price")
// Create a new price
err = dh.db.Create(&Price{
FluidType: price.FluidType,
Price: price.Price,
StartDate: price.StartDate,
EndDate: "",
}).Error
if err != nil {
println("Failed to create price")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(price)
log.Printf("| new price | fluidType : %d price : %v | %v", price.FluidType, price.Price, r.RemoteAddr)
return
} else {
var editablePrices []Price
priceToUpdate := price
isEditable := false
// If price is not included in the last 3 editable prices, throw error
err = dh.db.Where("fluid_type = ? ", price.FluidType).Order("start_date desc").Limit(3).Find(&editablePrices).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
for _, p := range editablePrices {
if p.StartDate == price.StartDate {
isEditable = true
}
}
if isEditable == false {
w.WriteHeader(http.StatusForbidden)
log.Printf("Unallowed to edit price because is not included in the the last 3 editable prices")
return
}
// Update existing price
err = dh.db.Model(&Price{}).Where("start_date = ? AND fluid_type = ?", priceToUpdate.StartDate, priceToUpdate.FluidType).Update("price", priceToUpdate.Price).Error
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(priceToUpdate)
log.Printf("| Updated price | fluidType : %d price : %v | startDate : %v | %v", priceToUpdate.FluidType, priceToUpdate.Price, priceToUpdate.StartDate, r.RemoteAddr)
}
}
......@@ -43,10 +43,16 @@ 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)
r.HandleFunc("/api/common/prices", dh.GetAllPrices).Methods(http.MethodGet)
r.HandleFunc("/api/common/prices/{fluidtype}", dh.GetPricesByFluid).Methods(http.MethodGet)
apiAdmin := r.PathPrefix("/api/admin").Subrouter()
apiAdmin.Use(auth.AdminAuthMiddleware)
apiAdmin.HandleFunc("/mailSubject/{year}/{month}", dh.GetSingleMailSubject).Methods(http.MethodGet)
apiAdmin.HandleFunc("/mailSubject", dh.SaveMailSubject).Methods(http.MethodPut)
apiAdmin.HandleFunc("/mailSubject/{year}/{month}", dh.DeleteMailSubject).Methods(http.MethodDelete)
apiAdmin.HandleFunc("/monthlyNews", dh.GetAllMonthlyNews).Methods(http.MethodGet)
apiAdmin.HandleFunc("/monthlyNews/{year}/{month}", dh.GetSingleMonthlyNews).Methods(http.MethodGet)
apiAdmin.HandleFunc("/monthlyNews", dh.SaveMonthlyNews).Methods(http.MethodPut)
......@@ -66,6 +72,8 @@ func CreateRootMux() RootMux {
apiAdmin.HandleFunc("/imageNames", file.GetEcogestureImages).Methods(http.MethodGet)
apiAdmin.HandleFunc("/prices", dh.SavePrice).Methods(http.MethodPut)
// Swagger
r.PathPrefix("/swagger").Handler(httpSwagger.WrapHandler)
......
......@@ -18,11 +18,13 @@ import (
var (
oAuth2Server *httptest.Server
monthlyInfo = models.MonthlyInfo{Year: 2021, Month: 0, Info: "Informations du mois", Image: "imagebase64"}
mailSubject = models.MailSubject{Year: 2021, Month: 1, Subject: "[Ecolyo] Newsletter"}
mailSubjectStr string
monthlyInfo = models.MonthlyInfo{Year: 2021, Month: 1, Info: "Informations du mois", Image: "imagebase64"}
monthlyInfoStr string
monthlyNews = models.MonthlyNews{Year: 2021, Month: 0, Title: "", Content: "Nouvelles fonctionnalités"}
monthlyNews = models.MonthlyNews{Year: 2021, Month: 1, Title: "", Content: "Nouvelles fonctionnalités"}
monthlyNewsStr string
newPoll = models.Poll{Year: 2021, Month: 0, Question: "pollQuestion", Link: "pollLink"}
newPoll = models.Poll{Year: 2021, Month: 1, Question: "pollQuestion", Link: "pollLink"}
newPollStr string
partnersInfo = models.PartnersInfo{ID: 1, GRDFFailure: false, EnedisFailure: false, EGLFailure: true, NotificationActivated: true}
partnersInfoStr string
......@@ -49,6 +51,8 @@ func TestMain(m *testing.M) {
tokens.Init("../configs/tokenskey.json", true)
// Convert example objects to string
mailSubjectBytes, _ := json.Marshal(mailSubject)
mailSubjectStr = string(mailSubjectBytes)
monthlyNewsBytes, _ := json.Marshal(monthlyNews)
monthlyNewsStr = string(monthlyNewsBytes)
monthlyInfoBytes, _ := json.Marshal(monthlyInfo)
......@@ -124,45 +128,50 @@ func adminTests(t *testing.T) {
// Try to create a monthlyNews without body (must fail)
do("PUT", "/api/admin/monthlyNews", xsrfHeader, "", http.StatusBadRequest, "request body is empty")
// Try to get a monthlyNews before it is created (must fail because not found)
do("GET", "/api/admin/monthlyNews/2021/0", xsrfHeader, "", http.StatusNotFound, "")
do("GET", "/api/admin/monthlyNews/2021/1", xsrfHeader, "", http.StatusNotFound, "")
// Try to create a monthlyNews (must pass)
do("PUT", "/api/admin/monthlyNews", xsrfHeader, monthlyNewsStr, http.StatusCreated, `{"year":2021,"month":0,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`)
do("PUT", "/api/admin/monthlyNews", xsrfHeader, monthlyNewsStr, http.StatusCreated, `{"year":2021,"month":1,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`)
// Try to update a monthlyNews (must pass)
do("PUT", "/api/admin/monthlyNews", xsrfHeader, monthlyNewsStr, http.StatusOK, `{"year":2021,"month":0,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`)
do("PUT", "/api/admin/monthlyNews", xsrfHeader, monthlyNewsStr, http.StatusOK, `{"year":2021,"month":1,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`)
// Try to get the monthlyNews created (must pass)
do("GET", "/api/admin/monthlyNews/2021/0", xsrfHeader, "", http.StatusOK, `{"year":2021,"month":0,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`)
do("GET", "/api/admin/monthlyNews/2021/1", xsrfHeader, "", http.StatusOK, `{"year":2021,"month":1,"title":"Les nouveautés du service","content":"Nouvelles fonctionnalités"`)
// Try to get the monthlyReport (must fail because monthlyInfo not found)
do("GET", "/api/common/monthlyReport/2021/0", noH, "", http.StatusNotFound, "")
do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusNotFound, "")
// Try to create a poll without the XSRF-TOKEN (must fail)
do("PUT", "/api/admin/poll", noH, newPollStr, http.StatusUnauthorized, "XSRF protection triggered")
// Try to create a poll without body (must fail)
do("PUT", "/api/admin/poll", xsrfHeader, "", http.StatusBadRequest, "request body is empty")
// Try to get a poll before it is created (must fail because not found')
do("GET", "/api/admin/poll/2021/0", xsrfHeader, "", http.StatusNotFound, "")
do("GET", "/api/admin/poll/2021/1", xsrfHeader, "", http.StatusNotFound, "")
// Try to create a poll (must pass)
do("PUT", "/api/admin/poll", xsrfHeader, newPollStr, http.StatusCreated, newPollStr)
// Try to update a poll (must pass)
do("PUT", "/api/admin/poll", xsrfHeader, newPollStr, http.StatusOK, newPollStr)
// Try to get the poll created (must pass)
do("GET", "/api/admin/poll/2021/0", xsrfHeader, "", http.StatusOK, newPollStr)
do("GET", "/api/admin/poll/2021/1", xsrfHeader, "", http.StatusOK, newPollStr)
// Try to get the monthlyReport (must fail because monthlyInfo not found)
do("GET", "/api/common/monthlyReport/2021/0", noH, "", http.StatusNotFound, "")
do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusNotFound, "")
// Try to create a monthlyInfo without the XSRF-TOKEN (must fail)
do("PUT", "/api/admin/monthlyInfo", noH, monthlyInfoStr, http.StatusUnauthorized, "XSRF protection triggered")
// Try to create a monthlyInfo without body (must fail)
do("PUT", "/api/admin/monthlyInfo", xsrfHeader, "", http.StatusBadRequest, "request body is empty")
// Try to get a monthlyInfo before it is created (must fail because not found)
do("GET", "/api/admin/monthlyInfo/2021/0", xsrfHeader, "", http.StatusNotFound, "")
do("GET", "/api/admin/monthlyInfo/2021/1", xsrfHeader, "", http.StatusNotFound, "")
// Try to create a monthlyInfo (must pass)
do("PUT", "/api/admin/monthlyInfo", xsrfHeader, monthlyInfoStr, http.StatusCreated, monthlyInfoStr)
// Try to update a monthlyInfo (must pass)
do("PUT", "/api/admin/monthlyInfo", xsrfHeader, monthlyInfoStr, http.StatusOK, monthlyInfoStr)
// Try to get the monthlyInfo created (must pass)
do("GET", "/api/admin/monthlyInfo/2021/0", xsrfHeader, "", http.StatusOK, monthlyInfoStr)
do("GET", "/api/admin/monthlyInfo/2021/1", xsrfHeader, "", http.StatusOK, monthlyInfoStr)
// 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"`)
do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de janvier 2021","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 update the partnersInfo (must pass)
do("PUT", "/api/admin/partnersInfo", xsrfHeader, partnersInfoStr, http.StatusOK, partnersInfoStr)
......@@ -170,17 +179,28 @@ func adminTests(t *testing.T) {
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")
do("DELETE", "/api/admin/monthlyNews/2021/1", xsrfHeader, "", http.StatusOK, "successful delete")
// Try to get a monthlyNews after it is deleted (must fail because not found)
do("GET", "/api/admin/monthlyNews/2021/0", xsrfHeader, "", http.StatusNotFound, "")
do("GET", "/api/admin/monthlyNews/2021/1", xsrfHeader, "", http.StatusNotFound, "")
// Try to create a mail subject (must pass)
do("PUT", "/api/admin/mailSubject", xsrfHeader, mailSubjectStr, http.StatusCreated, mailSubjectStr)
// 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":"","newsContent":"","question":"pollQuestion","link":"pollLink"`)
do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Newsletter","info":"Informations du mois","image":"imagebase64","newsTitle":"","newsContent":"","question":"pollQuestion","link":"pollLink"`)
// Try to delete the poll created (must pass)
do("DELETE", "/api/admin/poll/2021/0", xsrfHeader, "", http.StatusOK, "successful delete")
do("DELETE", "/api/admin/poll/2021/1", xsrfHeader, "", http.StatusOK, "successful delete")
// Try to get a poll after it is deleted (must fail because not found)
do("GET", "/api/admin/poll/2021/0", xsrfHeader, "", http.StatusNotFound, "")
do("GET", "/api/admin/poll/2021/1", xsrfHeader, "", http.StatusNotFound, "")
// Try to get the monthlyReport (must pass)
do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Newsletter","info":"Informations du mois","image":"imagebase64","newsTitle":"","newsContent":"","question":"","link":""`)
// Try to delete the mail subject created (must pass)
do("DELETE", "/api/admin/mailSubject/2021/1", xsrfHeader, "", http.StatusOK, "successful delete")
// Try to get a mail subject after it is deleted (must fail because not found)
do("GET", "/api/admin/mailSubject/2021/1", xsrfHeader, "", http.StatusNotFound, "")
// 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":"","newsContent":"","question":"","link":""`)
do("GET", "/api/common/monthlyReport/2021/1", noH, "", http.StatusOK, `{"year":2021,"month":1,"subject":"[Ecolyo] Votre bilan de janvier 2021","info":"Informations du mois","image":"imagebase64","newsTitle":"","newsContent":"","question":"","link":""`)
}
// Try to login (must pass)
do("GET", "/OAuth2Login", noH, "", http.StatusOK, "")
......
......@@ -41,4 +41,5 @@ func main() {
// Serve locally with https
log.Fatal(http.ListenAndServeTLS(":"+strconv.Itoa(httpsPort), "./dev_certificates/localhost.crt", "./dev_certificates/localhost.key", rootMux.Router))
// log.Fatal(http.ListenAndServe(":"+strconv.Itoa(httpsPort), rootMux.Router))
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment