diff --git a/.vscode/launch.json b/.vscode/launch.json index 37a6cd151b4d04745ae60bac65519c90ccd4b659..f18772ba75f5f25e46ecd92fd277e404fc600110 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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 }, diff --git a/README.md b/README.md index 0f6ecf87e4c2410a6a2f0d02ef820e64db1b2bdd..7b6ca2b6f5008bc280d79e5e4ad42cd0f428ac3f 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/dbinit/dbinit.sql b/dbinit/dbinit.sql new file mode 100644 index 0000000000000000000000000000000000000000..6b7ac168d08ef9dd4d4894a6537a25f6486d5f94 --- /dev/null +++ b/dbinit/dbinit.sql @@ -0,0 +1,4 @@ +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 diff --git a/dbinit/fluidprices.CSV b/dbinit/fluidprices.CSV new file mode 100644 index 0000000000000000000000000000000000000000..21670990623e0507be87e11a64779b23757ce9ef --- /dev/null +++ b/dbinit/fluidprices.CSV @@ -0,0 +1,84 @@ +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 diff --git a/dbinit/init.md b/dbinit/init.md new file mode 100644 index 0000000000000000000000000000000000000000..21120af53c0319ce892155eb5c04ac5279b4c606 --- /dev/null +++ b/dbinit/init.md @@ -0,0 +1,25 @@ +# 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 + +``` diff --git a/docker-compose.yml b/docker-compose.yml index d14130116f3cc2d8b4be45fc7533b4a7fcc9dd4d..ca2476473ed949939736274e10c63ae3f1224c1b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -13,6 +13,8 @@ services: interval: 5s timeout: 10s retries: 60 + volumes: + - ./dbinit:/dbinit phpmyadmin: image: phpmyadmin/phpmyadmin:latest diff --git a/internal/common/common.go b/internal/common/common.go index 708ba1ca2279bb0f036b7022e33e8bb35a070ac7..3ee2d9635c1200ec11750e2fcbc8a2820c83e302 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -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 +} + + + diff --git a/internal/models/models.go b/internal/models/models.go index d2b690f70dc0fdfbd133c5456f6f49492cf0f9f5..0a49abb05e9c31dc8af13458c2fff6853d3136de 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -42,6 +42,8 @@ func NewDataHandler() *DataHandler { 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} } diff --git a/internal/models/price.go b/internal/models/price.go new file mode 100644 index 0000000000000000000000000000000000000000..013c5d5b41f85cabe738c95949c9425ad05a2a94 --- /dev/null +++ b/internal/models/price.go @@ -0,0 +1,164 @@ +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) + } +} + diff --git a/internal/rootmux/rootmux.go b/internal/rootmux/rootmux.go index 6264fbf04e703022ae8694e927a9773b14816869..ce14660b51263e20e566c3ff55041821ba9d24e7 100644 --- a/internal/rootmux/rootmux.go +++ b/internal/rootmux/rootmux.go @@ -43,6 +43,8 @@ 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) @@ -66,6 +68,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) diff --git a/main.go b/main.go index 2876f92b1b41dd2a4de6d56b89a0d4c3bdd396d3..560052b38e7ebf12f066c79fbe66cb4013b3bbda 100644 --- a/main.go +++ b/main.go @@ -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)) }