func (sess MongodbHandler) GetTrip(rw http.ResponseWriter, req *http.Request, p httprouter.Params) { tripDetails := components.TripDetails{} id := bson.ObjectIdHex(p.ByName("trip_id")) err := sess.session.DB("test_db").C("trips").FindId(id).One(&tripDetails) if err != nil { fmt.Printf("got an error finding a doc %v\n") } if tripDetails.ID == "" { fmt.Fprintf(rw, "Invalid LocationID") } else { tripDetails.NextDestinationLocationID = "" tripDetails.UberWaitTime = "" rw.Header().Set("Content-Type", "application/json; charset=UTF-8") rw.Header().Set("Access-Control-Allow-Origin", "*") rw.WriteHeader(200) json.NewEncoder(rw).Encode(tripDetails) } }
func (sess MongodbHandler) UpdateTrip(rw http.ResponseWriter, req *http.Request, p httprouter.Params) { tripDetails := components.TripDetails{} locationDetails := components.Location{} id := bson.ObjectIdHex(p.ByName("trip_id")) err := sess.session.DB("test_db").C("trips").FindId(id).One(&tripDetails) if err != nil { fmt.Printf("got an error finding a trip %v\n") } currentLocationID := tripDetails.StartingFromLocationID if tripDetails.NextDestinationLocationID == tripDetails.StartingFromLocationID { tripDetails.Status = "trip over" tripDetails.NextDestinationLocationID = "" tripDetails.StartingFromLocationID = "" tripDetails.UberWaitTime = "" } else { if tripDetails.Status == "requesting" { if len(tripDetails.BestRouteLocationIds) > 1 { currentLocationID = tripDetails.BestRouteLocationIds[0] x := tripDetails.BestRouteLocationIds[1:len(tripDetails.BestRouteLocationIds)] tripDetails.BestRouteLocationIds = x tripDetails.NextDestinationLocationID = tripDetails.BestRouteLocationIds[0] } else { tripDetails.BestRouteLocationIds = nil currentLocationID = tripDetails.NextDestinationLocationID tripDetails.NextDestinationLocationID = tripDetails.StartingFromLocationID } } else if tripDetails.Status == "planning" { tripDetails.NextDestinationLocationID = tripDetails.BestRouteLocationIds[0] tripDetails.Status = "requesting" } url := fmt.Sprintf("http://localhost:8080/locations/%s", currentLocationID) client := http.Client{Timeout: timeout} res, err := client.Get(url) if err != nil { fmt.Errorf("Cannot read localhost LocationsAPI: %v", err) } defer res.Body.Close() decoder := json.NewDecoder(res.Body) err = decoder.Decode(&locationDetails) if err != nil { fmt.Errorf("Error in decoding the Location JSON: %v", err) } startLat := locationDetails.Coordinate.Lat startLng := locationDetails.Coordinate.Lng url = fmt.Sprintf("http://localhost:8080/locations/%s", tripDetails.NextDestinationLocationID) client = http.Client{Timeout: timeout} res, err = client.Get(url) if err != nil { fmt.Errorf("Cannot read localhost LocationsAPI: %v", err) } defer res.Body.Close() decoder = json.NewDecoder(res.Body) err = decoder.Decode(&locationDetails) if err != nil { fmt.Errorf("Error in decoding the Location JSON: %v", err) } tripDetails.UberWaitTime = components.UberRideRequest(startLat, startLng, locationDetails.Coordinate.Lat, locationDetails.Coordinate.Lng) if len(tripDetails.BestRouteLocationIds) == 0 { tripDetails.NextDestinationLocationID = tripDetails.StartingFromLocationID } /////////////////////////////////////// } //update the request in database err = sess.session.DB("test_db").C("trips").UpdateId(id, tripDetails) if err != nil { fmt.Printf("got an error updating a doc %v\n") } rw.Header().Set("Content-Type", "application/json; charset=UTF-8") rw.WriteHeader(200) json.NewEncoder(rw).Encode(tripDetails) }