package com.sangdol.experiment.portableDb;

import com.codahale.metrics.annotation.Timed;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * @author hugh
 */
@Path("/user")
@Produces(MediaType.APPLICATION_JSON)
public class ViewResource {
    private ViewService viewService;

    public ViewResource(ViewService viewService) {
        this.viewService = viewService;
    }

    @GET
    @Path("{id}")
    @Timed
    public List<View> viewList(@PathParam("id") int userId) {
        return viewService.getLatest10Visitors(userId);
    }

    @POST
    @Path("{id}")
    @Timed
    public int createView(@PathParam("id") int hostId, @QueryParam("visitor_id") int visitorId) {
        if (visitorId == 0)
            return 0;   // TODO throw 404

        return viewService.createView(hostId, visitorId);
    }

    @POST
    @Path("clear")
    @Timed
    public String clear() {
        viewService.clear();
        return "Success";
    }

    @GET
    @Path("view-count")
    @Timed
    public List<Integer> viewCount() {
        return viewService.getAllViewCounts();
    }
}

@@ -500,18 +500,50 @@
 int 
-user
+host
 Id) %7B%0A
+        checkValidity(hostId);%0A%0A
     
@@ -585,20 +585,20 @@
 isitors(
-user
+host
 Id);%0A   
@@ -750,126 +750,218 @@
     
-if (visitorId == 0)%0A            return 0;   // TODO throw 404%0A%0A        return viewService.createView(hostId, visitorId
+checkValidity(visitorId);%0A%0A        return viewService.createView(hostId, visitorId);%0A    %7D%0A%0A    private void checkValidity(int userId) %7B%0A        if (userId %3C 1)%0A            throw new WebApplicationException(404
 );%0A 
@@ -1126,16 +1126,17 @@
 ew-count
+s
 %22)%0A    @

Throw 404 exception when accessing with invalid userId