Created
June 19, 2015 11:23
-
-
Save Kikimora/5e72d1a6fa03f95d30ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Path("/person") | |
@Produces(MediaType.APPLICATION_JSON) | |
public class PersonResource { | |
final private static Logger LOGGER = LoggerFactory.getLogger(PersonResource.class); | |
ArrayList<PersonData> data = new ArrayList<>(); | |
public static class PersonDAO extends OClassDAO { | |
@Override | |
public String getClassName() { | |
return "Person"; | |
} | |
} | |
private Projection personUpdateData = new Projection() | |
.add("name") | |
.add("birthday") | |
.add("died") | |
.add("avatarURL") | |
.child("movies", movies -> movies.add("title")) | |
.add("roles"); | |
private Projection personData = personUpdateData.copy() | |
.add("id") | |
.child("movies", movies -> movies.add("id")); | |
private KeyValueCoding mapKVC = personUpdateData.makeKeyValueCoding(KVC.get(Map.class)); | |
@GET | |
public DataTablesResponse index( | |
@QueryParam("draw") int id, | |
@QueryParam("start") int start, | |
@QueryParam("length") int length) { | |
LOGGER.trace("index"); | |
try (PersonDAO dao = new PersonDAO()) { | |
Object [] data = dao.all(personData).map(record -> { | |
if (record.get("roles") == null) { | |
record.put("roles", new Object[]{}); | |
} | |
if (record.get("movies") == null) { | |
record.put("movies", new Object[]{}); | |
} | |
return record; | |
}).toArray(); | |
return new DataTablesResponse(id, data); | |
} | |
} | |
@POST | |
@Consumes(MediaType.APPLICATION_JSON) | |
public void create(Map<String, Object> person) { | |
LOGGER.trace("create"); | |
try (PersonDAO dao = new PersonDAO()) { | |
dao.save(new Record(data, mapKVC)); | |
} | |
} | |
@PUT | |
@Path("/{id}") | |
@Consumes(MediaType.APPLICATION_JSON) | |
public void update(@PathParam("id") EntityId id, Map<String, Object> data) { | |
LOGGER.trace("update: {}", id); | |
try (PersonDAO dao = new PersonDAO()) { | |
data.put("id", id); | |
dao.save(new Record(data, mapKVC)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment