package com.passbook.resources;

import com.google.common.base.Optional;
import com.passbook.api.Pass;
import com.passbook.core.Device;
import com.passbook.db.DeviceDAO;
import com.passbook.helper.Authenticator;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("/v1/passes")
@Produces(MediaType.APPLICATION_JSON)
public class PassbookPassesResource {

    private final DeviceDAO deviceDAO;

    public PassbookPassesResource(final DeviceDAO deviceDAO) {
        this.deviceDAO = deviceDAO;
    }

    @GET
    @Path("/{passTypeIdentifier}/{serialNumber}")
    public Response getLatest(@Context HttpHeaders headers,
                              @PathParam("passTypeIdentifier") String passTypeIdentifier,
                              @PathParam("serialNumber") String serialNumber) {
        Optional<Device> device = deviceDAO.findByPassTypeIdentifierAndSerialNumber(passTypeIdentifier, serialNumber);
        if (!device.isPresent()) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }

        if (!Authenticator.isAuthorized(headers, device.get())) {
            return Response.status(Response.Status.UNAUTHORIZED).build();
        }

        return Response.ok(new Pass(device.get().getUpdatedAt(), serialNumber))
                .lastModified(new DateTime(device.get().getUpdatedAt(), DateTimeZone.UTC).toDate())
                .build();
    }

}

@@ -30,48 +30,8 @@
 s;%0A%0A
-import com.google.common.base.Optional;%0A
 impo
@@ -1024,24 +1024,14 @@
     
-Optional%3C
 Device
-%3E
  dev
@@ -1137,27 +1137,22 @@
 if (
-!
 device
-.isPresent()
+ != null
 ) %7B%0A
@@ -1288,22 +1288,16 @@
 , device
-.get()
 )) %7B%0A   
@@ -1413,38 +1413,32 @@
 new Pass(device.
-get().
 getUpdatedAt(), 
@@ -1506,14 +1506,8 @@
 ice.
-get().
 getU

change return type