Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/com/google/sps/servlets/TripServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -132,14 +134,15 @@ 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.
populateDestinationAndPhoto(this.context, this.tripDestination);

// 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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/webapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,15 @@ <h2 class="site-title">Start a New Trip</h2>
name="inputDestination">
</div>
<div class="form-group col-md-3">
<label for="inputDayOfTravel">Day of Travel</label>
<label for="inputDayOfTravel">Trip Start Date</label>
<input type="date" class="form-control" name="inputDayOfTravel"
id="inputDayOfTravel">
</div>
<div class="form-group col-md-3">
<label for="inputEndDate">Trip End Date</label>
<input type="date" class="form-control" name="inputEndDate"
id="inputEndDate">
</div>
<div class="form-group">
<input type="button" class="btn btn-success" value="Next"
id="toggle-stage-button">
Expand Down
14 changes: 12 additions & 2 deletions src/main/webapp/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down
11 changes: 7 additions & 4 deletions src/test/java/com/google/sps/TripServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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();
Expand All @@ -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.
Expand All @@ -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();
Expand Down