From 6c1354a2f62aaddd960b38553689c3c3105accd6 Mon Sep 17 00:00:00 2001 From: jendib Date: Wed, 31 Jul 2013 21:02:08 +0200 Subject: [PATCH] Edition of tag name (server) --- docs-parent/TODO | 2 +- .../docs/rest/resource/TagResource.java | 38 ++++++++++++++++++- .../sismics/docs/rest/TestTagResource.java | 20 ++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/docs-parent/TODO b/docs-parent/TODO index 655ff384..f10e0f0d 100644 --- a/docs-parent/TODO +++ b/docs-parent/TODO @@ -1,3 +1,3 @@ - Client side search on tags -- Client/server side edition of existing tag names +- Client side edition of existing tag names - Server side reordering files \ No newline at end of file diff --git a/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java b/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java index e0238fd3..32e4755c 100644 --- a/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java +++ b/docs-web/src/main/java/com/sismics/docs/rest/resource/TagResource.java @@ -7,6 +7,7 @@ import java.util.List; import javax.ws.rs.DELETE; import javax.ws.rs.FormParam; 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; @@ -74,7 +75,7 @@ public class TagResource extends BaseResource { } // Validate input data - name = ValidationUtil.validateLength(name, "title", 1, 36, false); + name = ValidationUtil.validateLength(name, "name", 1, 36, false); // Get the tag TagDao tagDao = new TagDao(); @@ -94,6 +95,41 @@ public class TagResource extends BaseResource { return Response.ok().entity(response).build(); } + /** + * Update a tag tag. + * + * @param name Name + * @return Response + * @throws JSONException + */ + @POST + @Path("{id: [a-z0-9\\-]+}") + @Produces(MediaType.APPLICATION_JSON) + public Response update( + @PathParam("id") String id, + @FormParam("name") String name) throws JSONException { + if (!authenticate()) { + throw new ForbiddenClientException(); + } + + // Validate input data + name = ValidationUtil.validateLength(name, "name", 1, 36, false); + + // Get the tag + TagDao tagDao = new TagDao(); + Tag tag = tagDao.getByUserIdAndTagId(principal.getId(), id); + if (tag == null) { + throw new ClientException("TagNotFound", MessageFormat.format("Tag not found: {0}", id)); + } + + // Update the tag + tag.setName(name); + + JSONObject response = new JSONObject(); + response.put("id", id); + return Response.ok().entity(response).build(); + } + /** * Delete a tag. * diff --git a/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java b/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java index d3dad245..c157adc0 100644 --- a/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java +++ b/docs-web/src/test/java/com/sismics/docs/rest/TestTagResource.java @@ -51,6 +51,26 @@ public class TestTagResource extends BaseJerseyTest { JSONArray tags = json.getJSONArray("tags"); Assert.assertTrue(tags.length() > 0); + // Update a document + tagResource = resource().path("/tag/" + tag3Id); + tagResource.addFilter(new CookieAuthenticationFilter(tag1Token)); + postParams = new MultivaluedMapImpl(); + postParams.add("name", "Updated name"); + response = tagResource.post(ClientResponse.class, postParams); + Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); + json = response.getEntity(JSONObject.class); + Assert.assertEquals(tag3Id, json.getString("id")); + + // Get all tags + tagResource = resource().path("/tag/list"); + tagResource.addFilter(new CookieAuthenticationFilter(tag1Token)); + response = tagResource.get(ClientResponse.class); + Assert.assertEquals(Status.OK, Status.fromStatusCode(response.getStatus())); + json = response.getEntity(JSONObject.class); + tags = json.getJSONArray("tags"); + Assert.assertTrue(tags.length() > 0); + Assert.assertEquals("Updated name", tags.getJSONObject(0).getString("name")); + // Deletes a tag tagResource = resource().path("/tag/" + tag3Id); tagResource.addFilter(new CookieAuthenticationFilter(tag1Token));