package com.br.distanceCities.rest;

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;
import javax.ws.rs.core.Response;

import com.br.distanceCities.db.CrudDAO;
import com.br.distanceCities.db.impl.CityDAO;
import com.br.distanceCities.exception.ApplicationException;
import com.br.distanceCities.exception.DatabaseException;
import com.br.distanceCities.model.City;

@Path("/cities")
public class CityRESTService {

	@GET
	@Produces(MediaType.APPLICATION_JSON)
	public Response listAllCities() throws ApplicationException {
		try {
			CrudDAO<City> dao = new CityDAO();
			Iterable<City> retorno = dao.listAll();
			return Response.ok(retorno).build();
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
	}

	@GET
	@Path("/{id:[0-9][0-9]*}")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response lookupMemberById(@PathParam("id") Integer id) throws ApplicationException {
		try {
			CrudDAO<City> dao = new CityDAO();
			City retorno = dao.findFirst(new City(id));
			return Response.ok(retorno).build();
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
	}

	@POST
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces({ MediaType.APPLICATION_JSON })
	public Response insertCity(City city) throws ApplicationException {
		try {
			CrudDAO<City> dao = new CityDAO();
			City retorno = dao.insert(city);
			return Response.ok(retorno).build();
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
	}

	@PUT
	@Path("/{id:[0-9][0-9]*}")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces({ MediaType.APPLICATION_JSON })
	public Response updateCity(@PathParam("id") Integer id, City city) throws ApplicationException {
		try {
			CrudDAO<City> dao = new CityDAO();
			city.setId(id);
			dao.update(city);

			City retorno = dao.findFirst(new City(id));
			return Response.ok(retorno).build();
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
	}

	@DELETE
	@Path("/{id:[0-9][0-9]*}")
	public Response deleteCity(@PathParam("id") Integer id) throws ApplicationException {
		try {
			CrudDAO<City> dao = new CityDAO();
			dao.delete(new City(id));
			return Response.ok().build();
		} catch (DatabaseException e) {
			throw new ApplicationException(e);
		}
	}

}

@@ -2175,16 +2175,59 @@
 -9%5D*%7D%22)%0A
+%09@Produces(%7B MediaType.APPLICATION_JSON %7D)%0A
 %09public 
@@ -2402,16 +2402,18 @@
 onse.ok(
+id
 ).build(

Ajuste do retorno quando deleta