package com.google.sps.servlets;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceConfig;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.common.flogger.FluentLogger;
import com.google.gson.Gson;
import com.google.sps.data.Trip;
import com.google.sps.data.UserInfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/api/gettrips")
public class GetTripsServlet extends HttpServlet {
  private final Gson gson = new Gson();
  private final UserService userService = UserServiceFactory.getUserService();
  private final DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
  private static final FluentLogger logger = FluentLogger.forEnclosingClass();

  @Override
  public void init() {
    System.setProperty(
        DatastoreServiceConfig.DATASTORE_EMPTY_LIST_SUPPORT, Boolean.TRUE.toString());
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    UserInfo userInfo = UserInfo.findOrCreateUser(userService, datastore);
    if (userInfo == null) {
      response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    }
    List<String> userTripIds = userInfo.getTripIds();
    ArrayList<Trip> userTrips = new ArrayList<Trip>();
    for (String tripId : userTripIds) {
      try {
        Entity tripEntity = datastore.get(KeyFactory.stringToKey(tripId));
        Trip trip = Trip.fromEntity(tripEntity);
        userTrips.add(trip);
      } catch (EntityNotFoundException exception) {
        logger.atInfo().withCause(exception).log("Trip Entity not found: %s", tripId);
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        return;
      }
    }
    String json = gson.toJson(userTrips);
    response.setContentType("application/json");
    response.getWriter().println(json);
  }
}

@@ -1866,95 +1866,59 @@
     
-try %7B%0A        Entity tripEntity = datastore.get(KeyFactory.stringToKey(
+Trip trip = Trip.getTrip(datastore, 
 tripId)
-)
 ;%0A      
   Tr
@@ -1917,307 +1917,139 @@
     
- 
- Trip trip = Trip.fromEntity(tripEntity);%0A        userTrips.add(trip);%0A      %7D catch (EntityNotFoundException exception) %7B%0A        logger.atInfo().withCause(exception).log(%22Trip Entity not found: %25s%22, tripId);%0A        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);%0A        return
+if (trip == null) %7B%0A        response.setStatus(HttpServletResponse.SC_NOT_FOUND);%0A      %7D %0A      else%7B %0A        userTrips.add(trip)
 ;%0A  

use getTrips in GetTripsServlet