Skip to content

Commit

Permalink
added save tour in take tour
Browse files Browse the repository at this point in the history
  • Loading branch information
s1powers committed May 29, 2016
1 parent b1ed02e commit 4e832e6
Showing 1 changed file with 8 additions and 33 deletions.
41 changes: 8 additions & 33 deletions app/src/main/java/com/cs110/lit/adventour/DB.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import android.content.Context;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.cs110.lit.adventour.model.Checkpoint;
import com.cs110.lit.adventour.model.Tour;
Expand Down Expand Up @@ -92,38 +90,10 @@ public void onErrorResponse(VolleyError error) {
public static void saveTourTakenByUser (int tour_id, int user_id, Context c, final Callback<Integer> cb) {
RequestQueue requestQueue = Volley.newRequestQueue(c);
String reqUrl = base + "users/" + user_id + "/tours/taken";

/* Prepare the request body. */
JSONObject body;
try {
body = new JSONObject("{tour_id: " + tour_id + "}");
} catch (Exception e) {
body = null;
}

/* Prepare the request with the POST method */
JsonObjectRequest req = new JsonObjectRequest(Request.Method.POST, reqUrl, body, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Integer tourId = response.getInt("tour_id");
cb.onSuccess(tourId);
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
cb.onFailure(null);
}
});

/* Actually make the request */
requestQueue.add(req);
String tourJson = "{tour_id: " + tour_id + "}";
postTourToDBServer(cb, requestQueue, reqUrl, tourJson);
}


/**
* Will send the tour data to the database
*
Expand All @@ -135,7 +105,11 @@ public static void uploadTour (String tourJson, Context c, final Callback<Intege
RequestQueue requestQueue = Volley.newRequestQueue(c);
String reqUrl = base + "tours";

/* Prepare the request body. */
postTourToDBServer(cb, requestQueue, reqUrl, tourJson);
}

private static void postTourToDBServer(final Callback<Integer> cb, RequestQueue requestQueue, String reqUrl, String tourJson) {
/* Prepare the request body. */
JSONObject body;
try {
body = new JSONObject(tourJson);
Expand All @@ -157,6 +131,7 @@ public void onResponse(JSONObject response) {
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error with tour request");
cb.onFailure(null);
}
});
Expand Down

0 comments on commit 4e832e6

Please sign in to comment.