diff --git a/src/main/java/fr/ulille/iut/todo/Main.java b/src/main/java/fr/ulille/iut/todo/Main.java index 36baee069cd18c915c7a187e8fcc34b062a39a25..71876d97f022b2e356d7e4b080367cbc364f48ef 100644 --- a/src/main/java/fr/ulille/iut/todo/Main.java +++ b/src/main/java/fr/ulille/iut/todo/Main.java @@ -17,7 +17,7 @@ import org.glassfish.jersey.server.ResourceConfig; */ public class Main { // Base URI the Grizzly HTTP server will listen on - public static final String BASE_URI = "http://localhost:8080/api/v1/"; + public static final String BASE_URI = "http://localhost:8081/api/v1/"; private static final LogManager logManager = LogManager.getLogManager(); static { diff --git a/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java b/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java index d2c6b42603e33d0f54d29d20e6263cbbccb5944d..d8682b0afc83ea6a55390cc10c166b03b4034d20 100644 --- a/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java +++ b/src/main/java/fr/ulille/iut/todo/dao/TacheDAO.java @@ -24,5 +24,8 @@ public interface TacheDAO { @SqlQuery("select * from taches") @RegisterBeanMapper(Tache.class) List<Tache> getAll(); -} + + Tache getById(String id); + Tache getByDescription(String description); +} \ No newline at end of file diff --git a/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java b/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java index d05086abcce229c9237ff7d8c69d18fa06d280c9..2841554dcdc2b02c6e3b3a0e8d4c7477c6086dce 100644 --- a/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java +++ b/src/main/java/fr/ulille/iut/todo/ressource/TodoRessource.java @@ -10,6 +10,7 @@ import fr.ulille.iut.todo.service.Tache; import fr.ulille.iut.todo.service.TodoService; import jakarta.ws.rs.Consumes; import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.FormParam; import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; import jakarta.ws.rs.PUT; @@ -64,4 +65,43 @@ public class TodoRessource { } return builder.build(); } + @GET + @Path("{id}") + public Tache getById(@PathParam("id") UUID id) { + Tache task = new TodoService().getTache(id); + if(task == null) { + throw new WebApplicationException(404); + } + return task; + } + @GET + @Path("{description}/description") + public Tache getByDescription(@PathParam("description") String description) { + Tache task = new TodoService().getTache(description); + if(task == null) { + throw new WebApplicationException(404); + } + return task; + } + @POST + @Consumes(MediaType.APPLICATION_FORM_URLENCODED) + public Response createTacheFromForm(@FormParam("nom") String nom, @FormParam("description") String description) { + LOGGER.info("createTacheFromForm()"); + + Tache tache = new Tache(); + tache.setNom(nom); + tache.setDescription(description); + todoService.addTache(tache); + URI location = uri.getAbsolutePathBuilder().path(tache.getId().toString()).build(); + + EntityTag etag = new EntityTag(Integer.toString(tache.hashCode())); + ResponseBuilder response = request.evaluatePreconditions(etag); + + if (response == null) { + response = Response.created(location); + response.tag(etag); + response.entity(tache); + } + return response.build(); + } } diff --git a/src/main/java/fr/ulille/iut/todo/service/TodoService.java b/src/main/java/fr/ulille/iut/todo/service/TodoService.java index 654e7a274a37e36b9701d060299d8ae698ab5138..2ebb7dce8850198b282fda78b6d96d0ae5f8787d 100644 --- a/src/main/java/fr/ulille/iut/todo/service/TodoService.java +++ b/src/main/java/fr/ulille/iut/todo/service/TodoService.java @@ -15,9 +15,13 @@ public class TodoService { } public Tache getTache(UUID id) { - return null; + return taches.getById(id.toString()); } + public Tache getTache(String description) { + return taches.getByDescription(description); + } + public List<Tache> getAll() { return taches.getAll(); } diff --git a/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java b/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java index 847c5ed70736499c198d6e60890bd709ab317ec8..4266c3f2a0e13467465bcd8b2b1817fa2c00698a 100644 --- a/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java +++ b/src/test/java/fr/ulille/iut/todo/ressource/TodoRessourceTest.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertTrue; import java.util.ArrayList; import java.util.List; +import java.util.UUID; import java.util.logging.Level; import java.util.logging.Logger; @@ -96,18 +97,31 @@ public class TodoRessourceTest extends JerseyTest { @Test public void get_with_wrong_id_should_return_404() { + Response response = target("taches").path(UUID.randomUUID().toString()).request().get(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); } @Test public void get_for_description_should_work_with_existing_task() { + Tache tache = new Tache(); + tache.setNom("test"); + tache.setDescription("description"); + dao.insert(tache); + + Tache returned = target("taches").path(tache.getDescription().toString()).request().get(Tache.class); + + assertEquals(tache, returned); } @Test public void get_for_description_with_wrong_id_should_return_404() { + Response response = target("taches/description").path(UUID.randomUUID().toString()).request().get(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); } - @Test + /* @Test public void delete_should_remove_task_and_return_204() { + } @Test @@ -120,9 +134,14 @@ public class TodoRessourceTest extends JerseyTest { @Test public void partial_put_should_fail_with_400() { - } + } */ @Test public void post_with_form_data_should_return_201_location_and_task() { + Form form=new Form(); + form.param("nom", "task 1"); + form.param("description", "value"); + Response response= target("taches").request().post(Entity.form(form)); + assertEquals(response.getStatus(),201); } }