From 91a94f001e9b0ee6acb3bedea0d2ba0265679365 Mon Sep 17 00:00:00 2001 From: Tobias Lindberg Date: Tue, 9 Feb 2021 09:44:14 +0100 Subject: [PATCH] adding endpoint /cars//updates --- src/TeslaMateAPICarsUpdates.go | 116 +++++++++++++++++++++++++++++++++ src/webserver.go | 18 +++++ 2 files changed, 134 insertions(+) create mode 100644 src/TeslaMateAPICarsUpdates.go diff --git a/src/TeslaMateAPICarsUpdates.go b/src/TeslaMateAPICarsUpdates.go new file mode 100644 index 0000000..4ddb1e6 --- /dev/null +++ b/src/TeslaMateAPICarsUpdates.go @@ -0,0 +1,116 @@ +package main + +import ( + "encoding/json" + "fmt" + "log" + + _ "github.com/lib/pq" +) + +// TeslaMateAPICarsUpdates func +func TeslaMateAPICarsUpdates(CarID int, ResultPage int, ResultShow int) (string, bool) { + + // creating structs for /cars//updates + // Car struct - child of Data + type Car struct { + CarID int `json:"id"` // smallint + } + // Updates struct - child of Data + type Updates struct { + ID int `json:"updates_id"` // smallint + StartDate string `json:"start_date"` // string + EndDate string `json:"end_date"` // string + Version string `json:"version"` // string + } + // Data struct - child of JSONData + type Data struct { + Car Car `json:"car"` + Updates []Updates `json:"updates"` + } + // JSONData struct - main + type JSONData struct { + Data Data `json:"data"` + } + + // creating required vars + var UpdatesData []Updates + var ValidResponse bool // default is false + + // calculate offset based on page (page 0 is not possible, since first page is minimum 1) + if ResultPage > 0 { + ResultPage-- + } else { + ResultPage = 0 + } + ResultPage = (ResultPage * ResultShow) + + // getting data from database + query := ` + SELECT + id, + start_date, + end_date, + version + FROM updates + WHERE car_id = $1 + ORDER BY start_date DESC + LIMIT $2 OFFSET $3;` + rows, err := db.Query(query, CarID, ResultShow, ResultPage) + + // checking for errors in query + if err != nil { + log.Fatal(err) + } + + // defer closing rows + defer rows.Close() + + // looping through all results + for rows.Next() { + + // creating update object based on struct + update := Updates{} + + // scanning row and putting values into the update + err = rows.Scan( + &update.ID, + &update.StartDate, + &update.EndDate, + &update.Version, + ) + + // checking for errors after scanning + if err != nil { + log.Fatal(err) + } + + // appending update to UpdatesData + UpdatesData = append(UpdatesData, update) + ValidResponse = true + } + + // checking for errors in the rows result + err = rows.Err() + if err != nil { + log.Fatal(err) + } + + // + // build the data-blob + jsonData := JSONData{ + Data{ + Car: Car{ + CarID: CarID, + }, + Updates: UpdatesData, + }, + } + + // print readable output to log + log.Printf("data for /cars/%d/updates created:", CarID) + + js, _ := json.Marshal(jsonData) + fmt.Printf("%s\n", js) + return string(js), ValidResponse +} diff --git a/src/webserver.go b/src/webserver.go index 122bf8e..caee3ae 100644 --- a/src/webserver.go +++ b/src/webserver.go @@ -38,6 +38,24 @@ func main() { } }) + // /cars//updates endpoint + r.GET("/cars/:CarID/updates", func(c *gin.Context) { + // getting CarID param from URL + CarID := convertStringToInteger(c.Param("CarID")) + // query options to modify query when collecting data + ResultPage := convertStringToInteger(c.DefaultQuery("page", "1")) + ResultShow := convertStringToInteger(c.DefaultQuery("show", "100")) + // TeslaMateAPICarsUpdates to get data + result, ValidResponse := TeslaMateAPICarsUpdates(CarID, ResultPage, ResultShow) + + c.Header("Content-Type", "application/json") + if ValidResponse { + c.String(http.StatusOK, result) + } else { + c.String(http.StatusNotFound, result) + } + }) + // /ping endpoint r.GET("/ping", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{