diff --git a/src/main/java/com/google/sps/servlets/TripServlet.java b/src/main/java/com/google/sps/servlets/TripServlet.java index e9d0692..61cdb0d 100644 --- a/src/main/java/com/google/sps/servlets/TripServlet.java +++ b/src/main/java/com/google/sps/servlets/TripServlet.java @@ -81,6 +81,7 @@ public class TripServlet extends HttpServlet { private String tripName; private String tripDestination; private String tripDayOfTravel; + private String tripEndDate; private String destinationName; private String photoSrc; @@ -101,6 +102,7 @@ public class TripServlet extends HttpServlet { private static final String INPUT_TRIP_NAME = "inputTripName"; private static final String INPUT_DESTINATION = "inputDestination"; private static final String INPUT_DAY_OF_TRAVEL = "inputDayOfTravel"; + private static final String INPUT_END_DATE = "inputEndDate"; private static final String INPUT_POI_LIST = "poiList"; // Datastore and API context @@ -132,6 +134,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) this.tripName = request.getParameter(INPUT_TRIP_NAME); this.tripDestination = request.getParameter(INPUT_DESTINATION); this.tripDayOfTravel = request.getParameter(INPUT_DAY_OF_TRAVEL); + this.tripEndDate = request.getParameter(INPUT_END_DATE); String[] poiStrings = request.getParameterValues(INPUT_POI_LIST); // Populate the destinationName and photoSrc fields using Google Maps API. @@ -139,7 +142,7 @@ public void doPost(HttpServletRequest request, HttpServletResponse response) // Store the Trip Entity in datastore with the User Entity as an ancestor. Entity tripEntity = storeTripEntity(response, this.tripName, this.destinationName, - this.tripDayOfTravel, this.photoSrc, datastore); + this.tripDayOfTravel, this.tripEndDate, this.photoSrc, datastore); //do post for maps DirectionsApiRequest dirRequest = generateDirectionsRequest(this.tripDestination, this.tripDestination, poiStrings, this.context); @@ -301,7 +304,8 @@ private void populateDestinationAndPhoto(GeoApiContext context, String tripDesti * name, but can also be the placeholder image source if no photo exists. */ public Entity storeTripEntity(HttpServletResponse response, String tripName, - String destinationName, String tripDayOfTravel, String photoSrc, DatastoreService datastore) throws IOException { + String destinationName, String tripDayOfTravel, String tripEndDate, String photoSrc, DatastoreService datastore) + throws IOException { // Get User Entity. If user not logged in, redirect to homepage. Entity userEntity = AuthServlet.getCurrentUserEntity(); if (userEntity == null) { @@ -311,7 +315,7 @@ public Entity storeTripEntity(HttpServletResponse response, String tripName, // Put Trip Entity into datastore. Entity tripEntity = Trip.buildEntity(tripName, destinationName, photoSrc, - tripDayOfTravel, tripDayOfTravel, userEntity.getKey()); + tripDayOfTravel, tripEndDate, userEntity.getKey()); datastore.put(tripEntity); return tripEntity; } diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html index e520620..1968817 100644 --- a/src/main/webapp/index.html +++ b/src/main/webapp/index.html @@ -65,10 +65,15 @@

Start a New Trip

name="inputDestination">
- +
+
+ + +
diff --git a/src/main/webapp/script.js b/src/main/webapp/script.js index f464304..1df4b9e 100644 --- a/src/main/webapp/script.js +++ b/src/main/webapp/script.js @@ -143,6 +143,12 @@ function addStartTripOnChangeListeners() { checkValidInput('inputDayOfTravel'); checkNextButton(); }; + + // Input day of travel onchange listeners to check input and and next / submit. + document.getElementById('inputEndDate').onchange = () => { + checkValidInput('inputEndDate'); + checkNextButton(); + }; } // Add onclick listeners to buttons that call relevant functions in the @@ -202,14 +208,16 @@ function checkValidInput(elementId) { // Returns true if all name, location, and date inputs are valid; otherwise, false; function isStartingInputValid() { - // Get three input fields. + // Get four input fields. const inputTripName = document.getElementById('inputTripName'); const inputDestination = document.getElementById('inputDestination'); const inputDayOfTravel = document.getElementById('inputDayOfTravel'); + const inputEndDate = document.getElementById('inputEndDate'); return inputTripName.classList.contains('is-valid') && inputDestination.classList.contains('is-valid') && - inputDayOfTravel.classList.contains('is-valid'); + inputDayOfTravel.classList.contains('is-valid') && + inputEndDate.classList.contains('is-valid'); } // Return true if user has submitted a POI; otherwise, false. @@ -279,6 +287,7 @@ function toggleStartTripInputStage() { document.getElementById('inputTripName').readOnly = true; document.getElementById('inputDestination').readOnly = true; document.getElementById('inputDayOfTravel').readOnly = true; + document.getElementById('inputEndDate').readOnly = true; // Change the text of the toggle button to 'Back'. toggleStartTripStageButton.value = 'Back'; @@ -294,6 +303,7 @@ function toggleStartTripInputStage() { document.getElementById('inputTripName').readOnly = false; document.getElementById('inputDestination').readOnly = false; document.getElementById('inputDayOfTravel').readOnly = false; + document.getElementById('inputEndDate').readOnly = false; // Change the text of the toggle button to 'Next'. toggleStartTripStageButton.value = 'Next'; diff --git a/src/test/java/com/google/sps/TripServletTest.java b/src/test/java/com/google/sps/TripServletTest.java index fec5885..d3e1ff1 100644 --- a/src/test/java/com/google/sps/TripServletTest.java +++ b/src/test/java/com/google/sps/TripServletTest.java @@ -394,6 +394,7 @@ public void testStoreTripEntityLoggedIn() throws Exception { final String tripName = "Family Vacation"; final String destinationName = "Island of Hawai'i"; final String tripDayOfTravel = "2020-07-17"; + final String tripEndDate = "2020-07-24"; final String photoSrc = "../images/placeholder_image.png"; // Mock UserService methods as logged-in user. @@ -411,7 +412,7 @@ public void testStoreTripEntityLoggedIn() throws Exception { // Run storeTripEntity(...), with the User logged in (so trip is stored). Entity tripEntityReturn = tripServlet.storeTripEntity(responseMock, - tripName, destinationName, tripDayOfTravel, photoSrc, datastore); + tripName, destinationName, tripDayOfTravel, tripEndDate, photoSrc, datastore); // Retrieve the datastore results. DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); @@ -424,7 +425,7 @@ public void testStoreTripEntityLoggedIn() throws Exception { Assert.assertEquals(tripName, listResults.get(0).getProperty(Trip.TRIP_NAME)); Assert.assertEquals(destinationName, listResults.get(0).getProperty(Trip.DESTINATION_NAME)); Assert.assertEquals(tripDayOfTravel, listResults.get(0).getProperty(Trip.START_DATE)); - Assert.assertEquals(tripDayOfTravel, listResults.get(0).getProperty(Trip.END_DATE)); + Assert.assertEquals(tripEndDate, listResults.get(0).getProperty(Trip.END_DATE)); Assert.assertEquals(photoSrc, listResults.get(0).getProperty(Trip.IMAGE_SRC)); // Confirm that the Entity in the database matches the method return. @@ -440,6 +441,7 @@ public void testStoreTripEntityLoggedInCheckUser() throws Exception { final String tripName = "Family Vacation"; final String destinationName = "Island of Hawai'i"; final String tripDayOfTravel = "2020-07-17"; + final String tripEndDate = "2020-07-22"; final String photoSrc = "../images/placeholder_image.png"; // Mock UserService methods as logged-in user. @@ -457,7 +459,7 @@ public void testStoreTripEntityLoggedInCheckUser() throws Exception { // Run storeTripEntity(...), with the User logged in (so trip is stored). tripServlet.storeTripEntity(responseMock, tripName, destinationName, - tripDayOfTravel, photoSrc, datastore); + tripDayOfTravel, tripEndDate, photoSrc, datastore); // Retrieve the datastore results. DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); @@ -481,6 +483,7 @@ public void testStoreTripEntityNotLoggedIn() throws Exception { final String tripName = "Family Vacation"; final String destinationName = "Island of Hawai'i"; final String tripDayOfTravel = "2020-07-17"; + final String tripEndDate = "2020-07-27"; final String photoSrc = "../images/placeholder_image.png"; // Mock UserService methods as logged-out user. @@ -494,7 +497,7 @@ public void testStoreTripEntityNotLoggedIn() throws Exception { // Run storeTripEntity(...), with the User not logged in (so nothing is stored). Entity tripEntityReturn = tripServlet.storeTripEntity(responseMock, - tripName, destinationName, tripDayOfTravel, photoSrc, datastore); + tripName, destinationName, tripDayOfTravel, tripEndDate, photoSrc, datastore); // Retrieve the datastore results. DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();