Skip to content
Snippets Groups Projects
Commit c0a4082d authored by Tanguy Dessoly's avatar Tanguy Dessoly
Browse files

Tout sauf partiel et form

parent 8d3e00c1
Branches master
No related tags found
No related merge requests found
Pipeline #15497 canceled
......@@ -2,6 +2,7 @@ package fr.ulille.iut.todo.dao;
import java.util.List;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.BindBean;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
......@@ -21,8 +22,19 @@ public interface TacheDAO {
@SqlUpdate("insert into taches (id, nom, description) values (:id, :nom, :description)")
int insert(@BindBean Tache tache);
@SqlUpdate("update taches set nom = :nom, description = :description WHERE id = :id_origin")
int update(@Bind("id_origin") String id_origine, @BindBean Tache tache);
@SqlUpdate("delete from taches where id=?")
@RegisterBeanMapper(Tache.class)
int delete(String id);
@SqlQuery("select * from taches")
@RegisterBeanMapper(Tache.class)
List<Tache> getAll();
@SqlQuery("select * from taches where id=?")
@RegisterBeanMapper(Tache.class)
Tache getById(String id);
}
......@@ -11,16 +11,19 @@ import fr.ulille.iut.todo.service.TodoService;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.NotFoundException;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.EntityTag;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.NoContentException;
import jakarta.ws.rs.core.Request;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.ResponseBuilder;
......@@ -46,6 +49,32 @@ public class TodoRessource {
return todoService.getAll();
}
@GET
@Path("{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Tache getById(@PathParam("id") String id) {
LOGGER.info("getById(String id)");
Tache tache = todoService.getTache(UUID.fromString(id));
if(tache == null) {
throw new NotFoundException();
}
return tache;
}
@GET
@Path("{id}/description")
@Produces({ MediaType.TEXT_PLAIN })
public String getDescriptionById(@PathParam("id") String id) {
LOGGER.info("getDesciptionById(String id)");
Tache tache = todoService.getTache(UUID.fromString(id));
if(tache == null) {
throw new NotFoundException();
}
return tache.getDescription();
}
@POST
public Response createTache(CreationTacheDTO tacheDto) {
LOGGER.info("createTache()");
......@@ -64,4 +93,20 @@ public class TodoRessource {
}
return builder.build();
}
@DELETE
@Path("{id}")
public void deleteTache(@PathParam("id") String id) {
LOGGER.info("deleteTache(String id)");
int res = todoService.deleteTache(UUID.fromString(id));
if(res < 1) throw new NotFoundException();
}
@PUT
@Path("{id}")
public int putTache(@PathParam("id") String id, CreationTacheDTO tache) {
LOGGER.info("putTache(String id)");
return todoService.updateTache(id, Tache.fromCreationTacheDTO(tache));
}
}
......@@ -87,4 +87,5 @@ public class Tache {
return false;
return true;
}
}
......@@ -15,7 +15,7 @@ public class TodoService {
}
public Tache getTache(UUID id) {
return null;
return taches.getById(id.toString());
}
public List<Tache> getAll() {
......@@ -26,11 +26,11 @@ public class TodoService {
taches.insert(newTache);
}
public int deleteTache(String id) {
return 0 ;
public int deleteTache(UUID id) {
return taches.delete(id.toString());
}
public void updateTache(Tache tache) {
return;
public int updateTache(String id, Tache tache) {
return taches.update(id, tache);
}
}
package fr.ulille.iut.todo.ressource;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
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,33 +98,95 @@ public class TodoRessourceTest extends JerseyTest {
@Test
public void get_with_wrong_id_should_return_404() {
Response res = target("taches").path(UUID.randomUUID().toString()).request().get();
assertEquals(404, res.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);
String returned = target("taches").path(tache.getId().toString() + "/description").request().get(String.class);
assertEquals(tache.getDescription(), returned);
}
@Test
public void get_for_description_with_wrong_id_should_return_404() {
Response res = target("taches").path(UUID.randomUUID().toString() + "/description").request().get();
assertEquals(404, res.getStatus());
}
@Test
public void delete_should_remove_task_and_return_204() {
Tache tache = new Tache();
tache.setNom("test");
tache.setDescription("description");
dao.insert(tache);
Response res = target("taches").path(tache.getId().toString()).request().delete();
assertEquals(204, res.getStatus());
}
@Test
public void delete_with_wrong_id_should_return_404() {
Response res = target("taches").path(UUID.randomUUID().toString()).request().delete();
assertEquals(404, res.getStatus());
}
@Test
public void put_should_replace_existing_task_values_return_200_and_task() {
UUID id = UUID.randomUUID();
Tache tacheBefore = new Tache();
tacheBefore.setNom("testBefore");
tacheBefore.setDescription("descriptionBefore");
tacheBefore.setId(id);
dao.insert(tacheBefore);
Tache tacheAfter = new Tache();
tacheAfter.setNom("testAfter");
tacheAfter.setDescription("descriptionAfter");
tacheAfter.setId(id);
Response res = target("taches").path(tacheBefore.getId().toString()).request().put(Entity.json(tacheAfter));
assertNotEquals(tacheBefore, tacheAfter);
assertEquals(200, res.getStatus());
}
@Test
public void partial_put_should_fail_with_400() {
UUID id = UUID.randomUUID();
Tache tacheBefore = new Tache();
tacheBefore.setNom("testBefore");
tacheBefore.setDescription("descriptionBefore");
tacheBefore.setId(id);
dao.insert(tacheBefore);
Tache tacheAfter = new Tache();
tacheAfter.setNom("testAfter");
tacheAfter.setDescription("descriptionAfter");
tacheAfter.setId(id);
Response res = target("taches").path(tacheBefore.getId().toString()).request().put(Entity.json(tacheAfter));
assertNotEquals(tacheBefore, tacheAfter);
assertEquals(404, res.getStatus());
}
@Test
public void post_with_form_data_should_return_201_location_and_task() {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment