rest api uses map[string]interface for adding weather data
This commit is contained in:
parent
641c05afc6
commit
3983a91475
2 changed files with 72 additions and 12 deletions
|
@ -80,7 +80,7 @@ func (api *weatherRestApi) getData(w http.ResponseWriter, r *http.Request) {
|
||||||
http.Error(w, "", http.StatusBadRequest)
|
http.Error(w, "", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
res := storage.GetOnlyQueriedFields(data, query)
|
res := storage.ToMap(storage.GetOnlyQueriedFields(data, query))
|
||||||
json.NewEncoder(w).Encode(res)
|
json.NewEncoder(w).Encode(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,21 +107,28 @@ func (api *weatherRestApi) addDataHandler(w http.ResponseWriter, r *http.Request
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(r.Body)
|
w.Header().Add("content-type", "application/json")
|
||||||
|
|
||||||
var data storage.WeatherData
|
var data = make(map[string]interface{})
|
||||||
err := json.NewDecoder(r.Body).Decode(&data)
|
err := json.NewDecoder(r.Body).Decode(&data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(data)
|
weatherData, err := storage.FromMap(data)
|
||||||
|
|
||||||
err = api.addNewWeatherData(data)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
err = api.addNewWeatherData(*weatherData)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
json.NewEncoder(w).Encode(weatherData.ToMap())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *weatherRestApi) homePageHandler(w http.ResponseWriter, r *http.Request) {
|
func (api *weatherRestApi) homePageHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
@ -19,6 +19,11 @@ const (
|
||||||
Co2Level SensorValueType = "co2level"
|
Co2Level SensorValueType = "co2level"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
SensorId string = "sensorId"
|
||||||
|
TimeStamp string = "timeStamp"
|
||||||
|
)
|
||||||
|
|
||||||
func GetSensorValueTypes() []SensorValueType {
|
func GetSensorValueTypes() []SensorValueType {
|
||||||
return []SensorValueType{Temperature, Pressure, Humidity, Co2Level}
|
return []SensorValueType{Temperature, Pressure, Humidity, Co2Level}
|
||||||
}
|
}
|
||||||
|
@ -54,23 +59,71 @@ func (data *WeatherData) OnlyQueriedValues(query *WeatherQuery) *WeatherData {
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (data *WeatherData) ToStringMap() map[string]string {
|
func (data *WeatherData) ToMap() map[string]interface{} {
|
||||||
mappedData := map[string]string{
|
mappedData := map[string]interface{}{
|
||||||
"sensorId": data.SensorId.String(),
|
"sensorId": data.SensorId.String(),
|
||||||
"timeStamp": data.TimeStamp.String(),
|
"timeStamp": data.TimeStamp.String(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for sensorValueType, value := range data.Values {
|
for sensorValueType, value := range data.Values {
|
||||||
mappedData[string(sensorValueType)] = strconv.FormatFloat(value, 'f', -1, 64)
|
mappedData[string(sensorValueType)] = value //strconv.FormatFloat(value, 'f', -1, 64)
|
||||||
}
|
}
|
||||||
|
|
||||||
return mappedData
|
return mappedData
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetOnlyQueriedFields(dataPoints []*WeatherData, query *WeatherQuery) []map[string]string {
|
func FromMap(value map[string]interface{}) (*WeatherData, error) {
|
||||||
var result []map[string]string
|
var data = new(WeatherData)
|
||||||
|
data.Values = make(map[SensorValueType]float64)
|
||||||
|
var err error
|
||||||
|
|
||||||
|
copy := make(map[string]interface{})
|
||||||
|
for key, value := range value {
|
||||||
|
copy[key] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
idString, ok := copy[SensorId].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("sensorId must be of type string")
|
||||||
|
}
|
||||||
|
data.SensorId, err = uuid.Parse(idString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
delete(copy, SensorId)
|
||||||
|
|
||||||
|
timeStampString, ok := copy[TimeStamp].(string)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("timeStamp must be of type string")
|
||||||
|
}
|
||||||
|
data.TimeStamp, err = time.Parse(time.RFC3339, timeStampString)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
delete(copy, TimeStamp)
|
||||||
|
|
||||||
|
for key, val := range copy {
|
||||||
|
switch v := val.(type) {
|
||||||
|
case float64:
|
||||||
|
data.Values[SensorValueType(key)] = float64(v)
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return data, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetOnlyQueriedFields(dataPoints []*WeatherData, query *WeatherQuery) []*WeatherData {
|
||||||
for _, data := range dataPoints {
|
for _, data := range dataPoints {
|
||||||
result = append(result, data.OnlyQueriedValues(query).ToStringMap())
|
data.OnlyQueriedValues(query)
|
||||||
|
}
|
||||||
|
return dataPoints
|
||||||
|
}
|
||||||
|
|
||||||
|
func ToMap(dataPoints []*WeatherData) []map[string]interface{} {
|
||||||
|
var result = make([]map[string]interface{}, 0)
|
||||||
|
for _, data := range dataPoints {
|
||||||
|
result = append(result, data.ToMap())
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue