mirror of
https://github.com/tobiasehlert/teslamateapi.git
synced 2026-02-27 09:54:18 +08:00
adding endpoint /cars/<CarID>/updates
This commit is contained in:
@@ -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/<CarID>/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
|
||||
}
|
||||
@@ -38,6 +38,24 @@ func main() {
|
||||
}
|
||||
})
|
||||
|
||||
// /cars/<CarID>/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{
|
||||
|
||||
Reference in New Issue
Block a user