package foodtruck.server.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.common.base.Strings;
import com.google.inject.Inject;

import foodtruck.dao.ApplicationDAO;
import foodtruck.model.DailySchedule;
import foodtruck.truckstops.FoodTruckStopService;
import foodtruck.util.Clock;

/**
 * @author aviolette
 * @since 1/27/13
 */
@Path("/daily_schedule") @Produces(MediaType.APPLICATION_JSON)
public class DailyScheduleResource {
  private final FoodTruckStopService truckService;
  private final Clock clock;
  private final ApplicationDAO appDAO;

  @Inject
  public DailyScheduleResource(FoodTruckStopService foodTruckService, Clock clock, ApplicationDAO appDAO) {
    this.truckService = foodTruckService;
    this.clock = clock;
    this.appDAO = appDAO;
  }

  @GET @Produces("application/json")
  public DailySchedule findForDay(@QueryParam("appKey") final String appKey) {
    requireAppKey(appKey);
    return truckService.findStopsForDay(clock.currentDay());
  }

  private void requireAppKey(String appKey) {
    if (Strings.isNullOrEmpty(appKey) || appDAO.findById(appKey) == null) {
      throw new WebApplicationException(Response.Status.UNAUTHORIZED);
    }
  }
}

@@ -363,16 +363,52 @@
 ionDAO;%0A
+import foodtruck.model.Application;%0A
 import f
@@ -1262,16 +1262,17 @@
     if (
+!
 Strings.
@@ -1296,49 +1296,135 @@
 Key)
- %7C%7C appDAO.findById(appKey) == null) %7B%0A  
+) %7B%0A      Application app = appDAO.findById(appKey);%0A      if (app != null && app.isEnabled()) %7B%0A        return;%0A      %7D%0A    %7D%0A
     
@@ -1488,20 +1488,14 @@
 RIZED);%0A
-    %7D%0A
   %7D%0A%7D%0A

Check if app key has been enabled before allowing user to see resource