/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.github.daniel.shuy.ws.rs.jpa.crud;

import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Extend this class to create a CRUD RepositoryCRUD Class.
 * @param <E> The Entity Class type
 * @param <R> The Repository Class type
 */
public abstract class ResourceCRUD<E extends EntityCRUD, R extends RepositoryCRUD<E>> {
    private final R repository;
    
    public ResourceCRUD(R repository) {
        this.repository = repository;
    }
    
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void create(E content) {
        repository.add(content);
    }
    
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<E> readAll() {
        return repository.getAll();
    }
    
    @GET
    @Path("{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public E read(@PathParam("id") String id) {
        long idLong;
        try {
            idLong = Long.parseLong(id);
        }
        catch (NumberFormatException e) {
            return null;
        }
        
        return repository.get(idLong);
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public void update(E content) {
        repository.update(content);
    }
    
    @DELETE
    @Consumes(MediaType.APPLICATION_JSON)
    public void delete(E content) {
        repository.remove(content);
    }
}

@@ -249,16 +249,52 @@
 l.List;%0A
+import java.util.function.Function;%0A
 import j
@@ -532,16 +532,50 @@
 diaType;
+%0Aimport javax.ws.rs.core.Response;
 %0A%0A/**%0A *
@@ -1267,17 +1267,24 @@
  public 
-E
+Response
  read(@P
@@ -1323,208 +1323,115 @@
     
-long idLong;%0A        try %7B%0A            idLong = Long.parseLong(id);%0A        %7D%0A        catch (NumberFormatException e) %7B%0A            return null;%0A        %7D%0A        %0A        return repository.get(idLong
+return doWithID(id, (idLong) -%3E %7B%0A            return Response.ok(repository.get(idLong)).build();%0A        %7D
 );%0A 
@@ -1692,20 +1692,393 @@
 e(content);%0A
+    private Response doWithID(String id, Function%3CLong, Response%3E function) %7B%0A        long idLong;%0A        try %7B%0A            idLong = Long.parseLong(id);%0A        %7D%0A        catch (NumberFormatException e) %7B%0A            return Response.status(Response.Status.BAD_REQUEST).entity(%22id must be a Number%22).build();%0A        %7D%0A        %0A        return function.apply(idLong);%0A    %7D%0A
     %7D%0A%7D%0A

Change read/get to return a HTTP Error 400 if specified id is non-numeric