mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-10 09:52:51 +00:00
working on api for getting unique list of dates
This commit is contained in:
@@ -39,7 +39,7 @@ ReactDOM.render((
|
||||
<Route path="/" component={App}>
|
||||
<IndexRoute component={Preview}/>
|
||||
<Route path="post/:category/:post" component={Post}/>
|
||||
<Route path="sensor/:location/:year/:month" component={SensorInfo}/>
|
||||
<Route path="sensor/:location" component={SensorInfo}/>
|
||||
</Route>
|
||||
</Router>
|
||||
</Provider>
|
||||
|
||||
@@ -20,8 +20,8 @@ export default class SensorList extends React.Component {
|
||||
this.openLink = this.openLink.bind(this);
|
||||
}
|
||||
|
||||
openLink(){
|
||||
browserHistory.push("/");
|
||||
openLink(location){
|
||||
browserHistory.push(`/sensor/${location}`);
|
||||
this.props.toggleOff();
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ export default class SensorList extends React.Component {
|
||||
const date = new Date(sensor.updated);
|
||||
|
||||
return (
|
||||
<div key={index} class="row" onClick={this.openLink}>
|
||||
<div key={index} class="row" onClick={() => {this.openLink(sensor.location)}}>
|
||||
<div class="item">
|
||||
<h1>{sensor.temperature}°f</h1>
|
||||
</div>
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
padding: 1em;
|
||||
|
||||
&:hover{
|
||||
background-color: gray;
|
||||
background-color: #D1D1D1;
|
||||
}
|
||||
|
||||
.item + .item{
|
||||
|
||||
@@ -61,6 +61,7 @@ func HandleSensorRequest(w http.ResponseWriter, r *http.Request, ps httprouter.P
|
||||
storedData.Month = int(t.Month())
|
||||
storedData.MonthName = t.Month().String()
|
||||
storedData.Year = t.Year()
|
||||
storedData.Updated = t
|
||||
|
||||
err := storedData.StoreData()
|
||||
|
||||
@@ -134,21 +135,7 @@ func HandleSensorByLocation(w http.ResponseWriter, r *http.Request, ps httproute
|
||||
|
||||
s, err := daily_sensor.GetAllSensorInfo(location)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
response := createResponse(s, err)
|
||||
|
||||
fmt.Fprint(w, response)
|
||||
}
|
||||
@@ -162,21 +149,7 @@ func HandleSensorByLocationYear(w http.ResponseWriter, r *http.Request, ps httpr
|
||||
|
||||
s, err := daily_sensor.GetAllSensorInfoByYear(location, year)
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
response := createResponse(s, err)
|
||||
|
||||
fmt.Fprint(w, response)
|
||||
}
|
||||
@@ -191,21 +164,41 @@ func HandleSensorByLocationMonth(w http.ResponseWriter, r *http.Request, ps http
|
||||
|
||||
s, err := daily_sensor.GetAllSensorInfoByMonth(location, year, monthname)
|
||||
|
||||
response := createResponse(s, err)
|
||||
|
||||
fmt.Fprint(w, response)
|
||||
}
|
||||
|
||||
/*
|
||||
func HandleUniqueDates(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||
|
||||
location := ps.ByName("location")
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
s, err := daily_sensor.GetUniqueSensorDates(location)
|
||||
|
||||
response := createResponse(s, err)
|
||||
|
||||
fmt.Fprint(w, response)
|
||||
}
|
||||
*/
|
||||
func createResponse(s []daily_sensor.Data, err error) string{
|
||||
var response string
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
response = "{message : \"Error loading data from database\""
|
||||
} else {
|
||||
js, err := json.MarshalIndent(s, "", " ")
|
||||
js, err1 := json.MarshalIndent(s, "", " ")
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
if err1 != nil {
|
||||
log.Println(err1)
|
||||
response = "{message : \"Error loading data from database\""
|
||||
} else {
|
||||
response = string(js)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Fprint(w, response)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
@@ -16,22 +16,24 @@ const (
|
||||
|
||||
type Data struct {
|
||||
ID bson.ObjectId `bson:"_id,omitempty"`
|
||||
MaxTemp float64 `json:"maxtemp" bson:"maxtemp"`
|
||||
MinTemp float64 `json:"mintemp" bson:"mintemp"`
|
||||
Location string `json:"location" bson:"location"`
|
||||
Month int `json:"month" bson:"month"`
|
||||
MonthName string `json:"monthname" bson:"monthname"`
|
||||
Day int `json:"day" bson:"day"`
|
||||
Year int `json:"year" bson:"year"`
|
||||
MaxTemp float64 `json:"maxtemp,omitempty" bson:"maxtemp"`
|
||||
MinTemp float64 `json:"mintemp,omitempty" bson:"mintemp"`
|
||||
Location string `json:"location,omitempty" bson:"location"`
|
||||
Month int `json:"month,omitempty" bson:"month"`
|
||||
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"`
|
||||
}
|
||||
|
||||
//convert struct to json string
|
||||
func (s *Data) toJson() string {
|
||||
func (s *Data) ToJson() string {
|
||||
|
||||
b, err := json.MarshalIndent(s, "", " ")
|
||||
|
||||
if err != nil {
|
||||
log.Println(err.Error)
|
||||
log.Println(err)
|
||||
return "{message : \"Error loading data from database\""
|
||||
}
|
||||
|
||||
return string(b)
|
||||
@@ -73,7 +75,7 @@ func (s *Data) UpdateData() error {
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
colQuerier := bson.M{"location": s.Location, "month": s.Month, "monthname": s.MonthName, "day": s.Day, "year": s.Year}
|
||||
change := bson.M{"$set": bson.M{"maxtemp": s.MaxTemp, "mintemp": s.MinTemp}}
|
||||
change := bson.M{"$set": bson.M{"maxtemp": s.MaxTemp, "mintemp": s.MinTemp, "updated": time.Now()}}
|
||||
|
||||
err := c.Update(colQuerier, change)
|
||||
|
||||
@@ -198,3 +200,31 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string)
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
func GetUniqueSensorDates(sensor_location string) ([]Data, error){
|
||||
d := []Data{}
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
session := db.Mongo.Session.Copy()
|
||||
defer session.Close()
|
||||
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return d, nil
|
||||
}
|
||||
|
||||
return d, nil
|
||||
|
||||
} else {
|
||||
return d, errors.New("Query failed")
|
||||
}
|
||||
}
|
||||
*/
|
||||
@@ -20,7 +20,8 @@ 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("/discord", controller.DiscordRedirect)
|
||||
r.GET("/vpn", controller.VPNRedirect)
|
||||
r.GET("/camera", controller.CameraRedirect)
|
||||
|
||||
Reference in New Issue
Block a user