Skip to content
This repository was archived by the owner on Jul 24, 2023. It is now read-only.
Draft
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
16 changes: 12 additions & 4 deletions src/main/java/com/google/sps/servlets/MapServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
public class MapServlet extends HttpServlet {

private final String TRIP_KEY_PARAM = "tripKey";
private final String DATE_PARAM = "date";

/**
* Checks for invalid cases (no user or tripKey).
Expand Down Expand Up @@ -73,8 +74,9 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
} else {
Key tripKey = KeyFactory.stringToKey(stringTripKey);

String date = request.getParameter(DATE_PARAM);
// Gets the locations from datastore and writes them to .../get-map
String result = doGetMap(response, datastore, userEntity, tripKey);
String result = doGetMap(response, datastore, userEntity, tripKey, date);
response.getWriter().println(result);
}
}
Expand All @@ -83,7 +85,7 @@ public void doGet(HttpServletRequest request, HttpServletResponse response)
* Gets the locations and returns them as a JSON string.
*/
public String doGetMap(HttpServletResponse response,
DatastoreService datastore, Entity userEntity, Key tripEntityKey) throws IOException {
DatastoreService datastore, Entity userEntity, Key tripEntityKey, String date) throws IOException {

// Get trip Entity based on trip key.
// Trip entity is needed to ensure that tripKey corresponds to a Trip under the current user.
Expand All @@ -100,9 +102,15 @@ public String doGetMap(HttpServletResponse response,
return "No trip found";
}

// Get TripDay associated with the Trip.
// TODO(eshika): change to select the desired tripDay for multiday trips.
if (date == "" || date == null) {
date = (String) tripEntity.getProperty(Trip.START_DATE);
}

// Get TripDay associated with the Trip and inputted date.
Filter tripDayFilter =
new FilterPredicate(TripDay.DATE, FilterOperator.EQUAL, date);
Query tripDayQuery = new Query(TripDay.QUERY_STRING, tripEntity.getKey());
tripDayQuery.setFilter(tripDayFilter);
PreparedQuery tripDayResults = datastore.prepare(tripDayQuery);
Entity tripDayEntity = tripDayResults.asSingleEntity();

Expand Down
5 changes: 5 additions & 0 deletions src/main/webapp/maps.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<button type="button" class="btn btn-primary btn-xlarge">Maps</button>
</a>
</div>
<h2>My Trip Map</h2>
<div id ="dateInput">
<label for="inputMapDate">Display Map for:</label>
<input type="date" class="form-control" name="inputMapDate"
id="inputMapDate">
<div id="routeMap"></div>
<div id="rightPanel"></div>
</div>
Expand Down
18 changes: 15 additions & 3 deletions src/main/webapp/mapsScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,28 @@ window.initMap = function() {
directionsRenderer.setMap(map);
directionsRenderer.setPanel(document.getElementById('rightPanel'));

document.getElementById('inputMapDate').onchange = () => {
const date = document.getElementById('inputMapDate').value
if (date !== '') {
displayRouteOnMap(date);
}
};

// Get locations from MapServlet and display directions on map.
displayRouteOnMap();
displayRouteOnMap('');
}

/*
* Gets locations from MapServlet with the tripKey parameter.
* Calls showDirections to show the directions with those locations.
*/
function displayRouteOnMap() {
fetch('/get-map' + getTripKeyQuery()).then(response => response.json()).then((locations) => {
function displayRouteOnMap(date) {
const urlParams = new URLSearchParams(window.location.search);
const tripKey = urlParams.get('tripKey');
const tripKeyQuery = (tripKey != null && tripKey != '') ? '?tripKey=' + tripKey : '';
const dateQuery = (date != null && date != '') ? '&date=' + date : '';

fetch('/get-map' + tripKeyQuery + dateQuery).then(response => response.json()).then((locations) => {
showDirections(locations);
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/main/webapp/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@
text-align: center;
}

#inputMapDate {
width: 200px;
margin: auto;
margin-bottom: .75rem;
}

#dateInput {
width: 100%;

#mapsTitle {
margin-bottom: 10px;
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/google/sps/MapServletTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public void testDoGetMap() throws Exception {
datastore.put(yosemiteEntity);

// run do Get
String result = mapServlet.doGetMap(response, datastore, userEntity, tripEntity.getKey());
String result = mapServlet.doGetMap(response, datastore, userEntity, tripEntity.getKey(), "");

// even though DomeEntity is added first, its order property is 1 so it should appear
// after YosemiteEntity in the JSON.
Expand Down