func GetTrip(w http.ResponseWriter, r *http.Request, p httprouter.Params) { tId := p.ByName("trip_id") checkID, _ := strconv.Atoi(tId) var tripData structure.DataStorage findTarget := false for key, value := range hmap { if key == checkID { tripData = value findTarget = true } } if findTarget == false { w.WriteHeader(404) return } tripResponse := structure.TripResponse{} tripResponse.Id = tripData.Id tripResponse.Status = tripData.Status tripResponse.Starting_from_location_id = tripData.Starting_from_location_id tripResponse.Best_route_location_id = tripData.Best_route_location_id tripResponse.Total_uber_costs = tripData.Total_uber_costs tripResponse.Total_distance = tripData.Total_distance tripResponse.Total_uber_duration = tripData.Total_uber_duration trip, _ := json.Marshal(tripResponse) w.WriteHeader(200) fmt.Fprintf(w, "%s", trip) }
func CreateTrip(rw http.ResponseWriter, req *http.Request, p httprouter.Params) { var tripRequest structure.TripRequest json.NewDecoder(req.Body).Decode(&tripRequest) tripData := structure.DataStorage{} tripResponse := structure.TripResponse{} tripResponse.Id = getID() tripResponse.Status = "planning" tripResponse.Starting_from_location_id = tripRequest.Starting_from_location_id tripResponse.Best_route_location_id = tripRequest.Location_ids getBestRoute(&tripResponse, &tripData, tripRequest.Starting_from_location_id, tripRequest.Location_ids) hmap[tripResponse.Id] = tripData trip, _ := json.Marshal(tripResponse) rw.Header().Set("Content-Type", "application/json") rw.WriteHeader(201) fmt.Fprintf(rw, "%s", trip) }