1
0
mirror of https://github.com/sismics/docs.git synced 2026-01-04 20:39:26 +00:00

#65: Vocabulary resource

This commit is contained in:
jendib
2016-02-14 21:00:21 +01:00
parent d3a74ed361
commit 98497f2a37
9 changed files with 546 additions and 283 deletions

View File

@@ -86,9 +86,6 @@ public class CommentResource extends BaseResource {
throw new ForbiddenClientException();
}
// Validate input data
ValidationUtil.validateRequired(id, "id");
// Get the comment
CommentDao commentDao = new CommentDao();
Comment comment = commentDao.getActiveById(id);

View File

@@ -1,11 +1,19 @@
package com.sismics.docs.rest.resource;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObjectBuilder;
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;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.sismics.docs.core.dao.jpa.VocabularyDao;
import com.sismics.docs.core.model.jpa.Vocabulary;
@@ -19,6 +27,30 @@ import com.sismics.rest.util.ValidationUtil;
*/
@Path("/vocabulary")
public class VocabularyResource extends BaseResource {
@GET
@Path("{name: [a-z0-9\\-]+}")
public Response get(@PathParam("name") String name) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Assemble results
VocabularyDao vocabularyDao = new VocabularyDao();
List<Vocabulary> vocabularyList = vocabularyDao.getByName(name);
JsonArrayBuilder entries = Json.createArrayBuilder();
for (Vocabulary vocabulary : vocabularyList) {
entries.add(Json.createObjectBuilder()
.add("id", vocabulary.getId())
.add("value", vocabulary.getValue())
.add("order", vocabulary.getOrder()));
}
// Always return OK
JsonObjectBuilder response = Json.createObjectBuilder()
.add("entries", entries);
return Response.ok().entity(response.build()).build();
}
/**
* Add a vocabulary.
*
@@ -37,10 +69,11 @@ public class VocabularyResource extends BaseResource {
// Validate input data
name = ValidationUtil.validateLength(name, "name", 1, 50, false);
ValidationUtil.validateRegex(name, "name", "[a-z0-9\\-]+");
value = ValidationUtil.validateLength(value, "value", 1, 100, false);
Integer order = ValidationUtil.validateInteger(orderStr, "order");
// Create the comment
// Create the vocabulary
VocabularyDao vocabularyDao = new VocabularyDao();
Vocabulary vocabulary = new Vocabulary();
vocabulary.setName(name);
@@ -48,12 +81,96 @@ public class VocabularyResource extends BaseResource {
vocabulary.setOrder(order);
vocabularyDao.create(vocabulary);
// Returns the comment
// Returns the vocabulary
JsonObjectBuilder response = Json.createObjectBuilder()
.add("id", vocabulary.getId())
.add("name", vocabulary.getName())
.add("value", vocabulary.getValue())
.add("order", Integer.toString(vocabulary.getOrder()));
.add("order", vocabulary.getOrder());
return Response.ok().entity(response.build()).build();
}
/**
* Update a vocabulary.
*
* @param name Name
* @param value Value
* @param order Order
* @return Response
*/
@POST
@Path("{id: [a-z0-9\\-]+}")
public Response update(@PathParam("id") String id,
@FormParam("name") String name,
@FormParam("value") String value,
@FormParam("order") String orderStr) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Validate input data
name = ValidationUtil.validateLength(name, "name", 1, 50, true);
ValidationUtil.validateRegex(name, "name", "[a-z0-9\\-]+");
value = ValidationUtil.validateLength(value, "value", 1, 100, true);
Integer order = null;
if (orderStr != null) {
order = ValidationUtil.validateInteger(orderStr, "order");
}
// Get the vocabulary entry
VocabularyDao vocabularyDao = new VocabularyDao();
Vocabulary vocabulary = vocabularyDao.getById(id);
if (vocabulary == null) {
return Response.status(Status.NOT_FOUND).build();
}
// Update the vocabulary
if (name != null) {
vocabulary.setName(name);
}
if (value != null) {
vocabulary.setValue(value);
}
if (order != null) {
vocabulary.setOrder(order);
}
vocabularyDao.update(vocabulary);
// Returns the vocabulary
JsonObjectBuilder response = Json.createObjectBuilder()
.add("id", vocabulary.getId())
.add("name", vocabulary.getName())
.add("value", vocabulary.getValue())
.add("order", vocabulary.getOrder());
return Response.ok().entity(response.build()).build();
}
/**
* Delete a vocabulary.
*
* @param id ID
* @return Response
*/
@DELETE
@Path("{id: [a-z0-9\\-]+}")
public Response delete(@PathParam("id") String id) {
if (!authenticate()) {
throw new ForbiddenClientException();
}
// Get the vocabulary
VocabularyDao vocabularyDao = new VocabularyDao();
Vocabulary vocabulary = vocabularyDao.getById(id);
if (vocabulary == null) {
return Response.status(Status.NOT_FOUND).build();
}
// Delete the vocabulary
vocabularyDao.delete(id);
// Always return OK
JsonObjectBuilder response = Json.createObjectBuilder()
.add("status", "ok");
return Response.ok().entity(response.build()).build();
}
}