func (uc UberController) AddTrip(w http.ResponseWriter, r *http.Request) { trip := models.Trip{} tripResp := models.TripResponse{} location := models.Location{} loc := make(map[string]models.Location) decoder := json.NewDecoder(r.Body) err := decoder.Decode(&trip) if err != nil { fmt.Errorf("Error in decoding the Input JSON: %v", err) } tripResp.StartingFromLocationID = trip.StartingFromLocationID tripResp.ID = bson.NewObjectId() tripResp.Status = "planning" //Initial Location oid := bson.ObjectIdHex(trip.StartingFromLocationID) err = uc.session.DB("test_mongo_db").C("test").FindId(oid).One(&location) if err != nil { fmt.Printf("got an error finding a doc %v\n") } loc[trip.StartingFromLocationID] = location //fmt.Print(json.NewEncoder(w).Encode(loc[trip.StartingFromLocationID])) for _, each := range trip.LocationIds { oid = bson.ObjectIdHex(each) err = uc.session.DB("test_mongo_db").C("test").FindId(oid).One(&location) if err != nil { fmt.Printf("got an error finding a doc %v\n") } loc[each] = location //fmt.Print(json.NewEncoder(w).Encode(loc[each])) } /*for _, each := range loc { fmt.Print(json.NewEncoder(w).Encode(each)) }*/ var price int var duration int var distance float64 startId := trip.StartingFromLocationID startLat := loc[startId].Coordinate.Lat startLng := loc[startId].Coordinate.Lng nextId := startId originLat := loc[startId].Coordinate.Lat originLng := loc[startId].Coordinate.Lng minPrice := 99999 minDuration := 0 minDistance := 0.0 totalCost := 0 totalDuration := 0 totalDistance := 0.0 pos := 0 for len(loc) > 1 { for index, each := range loc { if index != startId { price, duration, distance = GetEstimates(startLat, startLng, each.Coordinate.Lat, each.Coordinate.Lng) if price < minPrice { minPrice = price minDuration = duration minDistance = distance nextId = index } } } trip.LocationIds[pos] = nextId //fmt.Println("The min price is: %d with startid: %s and nextId: %s", minPrice, startId, nextId) totalCost += minPrice totalDuration += minDuration totalDistance += minDistance delete(loc, startId) startId = nextId startLat = loc[startId].Coordinate.Lat startLng = loc[startId].Coordinate.Lng minPrice = 99999 minDuration = 0 minDistance = 0.00 pos++ } price, duration, distance = GetEstimates(startLat, startLng, originLat, originLng) totalCost += price totalDuration += duration totalDistance += distance tripResp.BestRouteLocationIds = trip.LocationIds tripResp.TotalUberCosts = totalCost tripResp.TotalUberDuration = totalDuration tripResp.TotalDistance = totalDistance err = uc.session.DB("test_mongo_db").C("trip_details").Insert(tripResp) if err != nil { fmt.Printf("Can't insert document: %v\n", err) } err = uc.session.DB("test_mongo_db").C("trip_details").FindId(tripResp.ID).One(&tripResp) if err != nil { fmt.Printf("got an error finding a doc %v\n") } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(200) json.NewEncoder(w).Encode(tripResp) }
func (uc UberController) RequestTrip(w http.ResponseWriter, r *http.Request) { params := r.URL.Query() uid := params.Get(":trip_id") oid := bson.ObjectIdHex(uid) tripResp := models.TripResponse{} err := uc.session.DB("test_mongo_db").C("trip_details").FindId(oid).One(&tripResp) if err != nil { fmt.Printf("got an error finding a doc %v\n") } tripReq := models.TripRequest{} tripReq.ID = tripResp.ID tripReq.Status = tripResp.Status tripReq.StartingFromLocationID = tripResp.StartingFromLocationID tripReq.BestRouteLocationIds = tripResp.BestRouteLocationIds tripReq.TotalUberCosts = tripResp.TotalUberCosts tripReq.TotalUberDuration = tripResp.TotalUberDuration tripReq.TotalDistance = tripResp.TotalDistance if len(tripResp.BestRouteLocationIds) > 0 { tripReq.NextDestinationLocationID = tripResp.BestRouteLocationIds[0] } if len(tripReq.BestRouteLocationIds) == 0 { tripReq.Status = "trip over" tripReq.NextDestinationLocationID = "" tripReq.StartingFromLocationID = "" tripReq.UberWaitTimeEta = 0 } else { if tripReq.Status == "planning" { tripReq.Status = "requesting" tripReq.NextDestinationLocationID = tripReq.BestRouteLocationIds[0] } else if tripResp.Status == "requesting" { if len(tripReq.BestRouteLocationIds) > 1 { value := tripReq.BestRouteLocationIds[1:len(tripReq.BestRouteLocationIds)] tripReq.BestRouteLocationIds = value tripReq.NextDestinationLocationID = tripReq.BestRouteLocationIds[0] } else { tripReq.BestRouteLocationIds = nil tripReq.NextDestinationLocationID = tripReq.StartingFromLocationID } } oid := bson.ObjectIdHex(tripReq.StartingFromLocationID) loc := models.Location{} err := uc.session.DB("test_mongo_db").C("test").FindId(oid).One(&loc) if err != nil { fmt.Printf("got an error finding a doc %v\n") } fmt.Println(loc) startLat := loc.Coordinate.Lat startLng := loc.Coordinate.Lng tripReq.UberWaitTimeEta = GetEstimatedTime(startLat, startLng) if len(tripReq.BestRouteLocationIds) == 0 { tripReq.StartingFromLocationID = tripReq.NextDestinationLocationID } } tripResp.ID = tripReq.ID tripResp.Status = tripReq.Status tripResp.StartingFromLocationID = tripReq.StartingFromLocationID tripResp.BestRouteLocationIds = tripReq.BestRouteLocationIds tripResp.TotalUberCosts = tripReq.TotalUberCosts tripResp.TotalUberDuration = tripReq.TotalUberDuration tripResp.TotalDistance = tripReq.TotalDistance err = uc.session.DB("test_mongo_db").C("trip_details").UpdateId(oid, tripResp) if err != nil { fmt.Printf("got an error updating a doc %v\n") } w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(200) json.NewEncoder(w).Encode(tripReq) }