mirror of
https://github.com/sismics/docs.git
synced 2025-12-13 17:56:20 +00:00
#23: Tag tree (server)
This commit is contained in:
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=1
|
||||
db.version=2
|
||||
@@ -22,6 +22,7 @@ import com.sismics.docs.core.dao.jpa.dto.TagStatDto;
|
||||
import com.sismics.docs.core.model.jpa.Tag;
|
||||
import com.sismics.rest.exception.ClientException;
|
||||
import com.sismics.rest.exception.ForbiddenClientException;
|
||||
import com.sismics.rest.util.JsonUtil;
|
||||
import com.sismics.rest.util.ValidationUtil;
|
||||
|
||||
/**
|
||||
@@ -50,7 +51,8 @@ public class TagResource extends BaseResource {
|
||||
items.add(Json.createObjectBuilder()
|
||||
.add("id", tag.getId())
|
||||
.add("name", tag.getName())
|
||||
.add("color", tag.getColor()));
|
||||
.add("color", tag.getColor())
|
||||
.add("parent", JsonUtil.nullable(tag.getParentId())));
|
||||
}
|
||||
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
@@ -96,7 +98,8 @@ public class TagResource extends BaseResource {
|
||||
@PUT
|
||||
public Response add(
|
||||
@FormParam("name") String name,
|
||||
@FormParam("color") String color) {
|
||||
@FormParam("color") String color,
|
||||
@FormParam("parent") String parentId) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
@@ -117,11 +120,20 @@ public class TagResource extends BaseResource {
|
||||
throw new ClientException("AlreadyExistingTag", MessageFormat.format("Tag already exists: {0}", name));
|
||||
}
|
||||
|
||||
// Check the parent
|
||||
if (parentId != null) {
|
||||
Tag parentTag = tagDao.getByTagId(principal.getId(), parentId);
|
||||
if (parentTag == null) {
|
||||
throw new ClientException("ParentNotFound", MessageFormat.format("Parent not found: {0}", parentId));
|
||||
}
|
||||
}
|
||||
|
||||
// Create the tag
|
||||
tag = new Tag();
|
||||
tag.setName(name);
|
||||
tag.setColor(color);
|
||||
tag.setUserId(principal.getId());
|
||||
tag.setParentId(parentId);
|
||||
String id = tagDao.create(tag);
|
||||
|
||||
JsonObjectBuilder response = Json.createObjectBuilder()
|
||||
@@ -140,7 +152,8 @@ public class TagResource extends BaseResource {
|
||||
public Response update(
|
||||
@PathParam("id") String id,
|
||||
@FormParam("name") String name,
|
||||
@FormParam("color") String color) {
|
||||
@FormParam("color") String color,
|
||||
@FormParam("parent") String parentId) {
|
||||
if (!authenticate()) {
|
||||
throw new ForbiddenClientException();
|
||||
}
|
||||
@@ -161,6 +174,14 @@ public class TagResource extends BaseResource {
|
||||
throw new ClientException("TagNotFound", MessageFormat.format("Tag not found: {0}", id));
|
||||
}
|
||||
|
||||
// Check the parent
|
||||
if (parentId != null) {
|
||||
Tag parentTag = tagDao.getByTagId(principal.getId(), parentId);
|
||||
if (parentTag == null) {
|
||||
throw new ClientException("ParentNotFound", MessageFormat.format("Parent not found: {0}", parentId));
|
||||
}
|
||||
}
|
||||
|
||||
// Check for name duplicate
|
||||
Tag tagDuplicate = tagDao.getByName(principal.getId(), name);
|
||||
if (tagDuplicate != null && !tagDuplicate.getId().equals(id)) {
|
||||
@@ -174,6 +195,8 @@ public class TagResource extends BaseResource {
|
||||
if (!StringUtils.isEmpty(color)) {
|
||||
tag.setColor(color);
|
||||
}
|
||||
// Parent tag is always updated to have the possibility to delete it
|
||||
tag.setParentId(parentId);
|
||||
|
||||
tagDao.update(tag);
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=1
|
||||
db.version=2
|
||||
@@ -1,3 +1,3 @@
|
||||
api.current_version=${project.version}
|
||||
api.min_version=1.0
|
||||
db.version=1
|
||||
db.version=2
|
||||
@@ -2,6 +2,7 @@ package com.sismics.docs.rest;
|
||||
|
||||
import javax.json.JsonArray;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonValue;
|
||||
import javax.ws.rs.client.Entity;
|
||||
import javax.ws.rs.core.Form;
|
||||
import javax.ws.rs.core.Response;
|
||||
@@ -44,7 +45,8 @@ public class TestTagResource extends BaseJerseyTest {
|
||||
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, tag1Token)
|
||||
.put(Entity.form(new Form()
|
||||
.param("name", "Tag4")
|
||||
.param("color", "#00ff00")), JsonObject.class);
|
||||
.param("color", "#00ff00")
|
||||
.param("parent", tag3Id)), JsonObject.class);
|
||||
String tag4Id = json.getString("id");
|
||||
Assert.assertNotNull(tag4Id);
|
||||
|
||||
@@ -129,6 +131,7 @@ public class TestTagResource extends BaseJerseyTest {
|
||||
Assert.assertTrue(tags.size() > 0);
|
||||
Assert.assertEquals("Tag4", tags.getJsonObject(1).getString("name"));
|
||||
Assert.assertEquals("#00ff00", tags.getJsonObject(1).getString("color"));
|
||||
Assert.assertEquals(tag3Id, tags.getJsonObject(1).getString("parent"));
|
||||
|
||||
// Update a tag
|
||||
json = target().path("/tag/" + tag4Id).request()
|
||||
@@ -146,6 +149,7 @@ public class TestTagResource extends BaseJerseyTest {
|
||||
Assert.assertTrue(tags.size() > 0);
|
||||
Assert.assertEquals("UpdatedName", tags.getJsonObject(1).getString("name"));
|
||||
Assert.assertEquals("#0000ff", tags.getJsonObject(1).getString("color"));
|
||||
Assert.assertEquals(JsonValue.NULL, tags.getJsonObject(1).get("parent"));
|
||||
|
||||
// Deletes a tag
|
||||
target().path("/tag/" + tag4Id).request()
|
||||
|
||||
Reference in New Issue
Block a user