diff --git a/server/controller/api/sensor.go b/server/controller/api/sensor.go index f4e5dc5..1105779 100644 --- a/server/controller/api/sensor.go +++ b/server/controller/api/sensor.go @@ -149,7 +149,21 @@ func HandleSensorByLocationYear(w http.ResponseWriter, r *http.Request, ps httpr s, err := daily_sensor.GetAllSensorInfoByYear(location, year) - response := createResponse(s, err) + var response string + + if err != nil { + log.Println(err) + response = "{message : \"Error loading data from database\"" + } else { + js, err := json.MarshalIndent(s, "", " ") + + if err != nil { + log.Println(err) + response = "{message : \"Error loading data from database\"" + } else { + response = string(js) + } + } fmt.Fprint(w, response) } @@ -164,12 +178,25 @@ func HandleSensorByLocationMonth(w http.ResponseWriter, r *http.Request, ps http s, err := daily_sensor.GetAllSensorInfoByMonth(location, year, monthname) - response := createResponse(s, err) + var response string + + if err != nil { + log.Println(err) + response = "{message : \"Error loading data from database\"" + } else { + js, err := json.MarshalIndent(s, "", " ") + + if err != nil { + log.Println(err) + response = "{message : \"Error loading data from database\"" + } else { + response = string(js) + } + } fmt.Fprint(w, response) } -/* func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { location := ps.ByName("location") @@ -182,7 +209,7 @@ func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Par fmt.Fprint(w, response) } -*/ + func createResponse(s []daily_sensor.Data, err error) string{ var response string @@ -190,10 +217,10 @@ func createResponse(s []daily_sensor.Data, err error) string{ log.Println(err) response = "{message : \"Error loading data from database\"" } else { - js, err1 := json.MarshalIndent(s, "", " ") + js, err := json.MarshalIndent(s, "", " ") - if err1 != nil { - log.Println(err1) + if err != nil { + log.Println(err) response = "{message : \"Error loading data from database\"" } else { response = string(js) diff --git a/server/model/daily_sensor/daily_sensor.go b/server/model/daily_sensor/daily_sensor.go index 0dc8f27..1a5e806 100644 --- a/server/model/daily_sensor/daily_sensor.go +++ b/server/model/daily_sensor/daily_sensor.go @@ -23,7 +23,7 @@ type Data struct { MonthName string `json:"monthname,omitempty" bson:"monthname"` Day int `json:"day,omitempty" bson:"day"` Year int `json:"year,omitempty" bson:"year"` - Updated time.Time `json:"updated" bson:"updated"` + Updated time.Time `json:"updated,omitempty" bson:"updated"` } //convert struct to json string @@ -132,7 +132,6 @@ func GetAllSensorInfo(sensor_location string) ([]Data, error) { c := session.DB(db.Mongo.Info.Database).C(collection) - //err := c.Find(bson.M{"location": sensor_location}).Sort("-year, -month").All(&d) err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}}, {"$sort": bson.M{"year": -1, "month": 1}}}).All(&d) @@ -201,9 +200,13 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string) } } -/* +type UniqueDates struct{ + Dates []Data `json:"dates" bson:"dates"` +} + func GetUniqueSensorDates(sensor_location string) ([]Data, error){ d := []Data{} + temp := UniqueDates{}; if db.Mongo.Connected() == true { @@ -213,9 +216,11 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){ c := session.DB(db.Mongo.Info.Database).C(collection) err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}}, - {"$project": bson.M{""}}, - {"$sort": bson.M{"year": -1, "month": 1}}}).All(&d) - + {"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}}, + }).One(&temp) + + d = temp.Dates; + if err != nil { log.Println(err) return d, nil @@ -227,4 +232,3 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){ return d, errors.New("Query failed") } } -*/ \ No newline at end of file diff --git a/server/route/route.go b/server/route/route.go index 6875f33..90a6efe 100644 --- a/server/route/route.go +++ b/server/route/route.go @@ -20,7 +20,7 @@ func Routes() *httprouter.Router { r.GET("/api/sensor/:location", api.HandleSensorByLocation) r.GET("/api/sensor/:location/:year", api.HandleSensorByLocationYear) r.GET("/api/sensor/:location/:year/:monthname", api.HandleSensorByLocationMonth) - //r.GET("/api/sensor/:uniquedates", api.HandleUniqueDates) + r.GET("/api/uniquedates/:location", api.HandleUniqueDates) r.GET("/discord", controller.DiscordRedirect) r.GET("/vpn", controller.VPNRedirect)