package lab6.rest.service;
import lab6.rest.pojo.CommentPOJO;
import lab6.rest.pojo.EntryPOJO;
import javax.inject.Singleton;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;
@Singleton
@Path("/blog")
public class Blog {
@POST
@Path("/entry")
@Produces(MediaType.APPLICATION_JSON)
public void addEntry
(EntryPOJO json
) throws Exception{
entries.add(new EntryPOJO(json.getId(), json.getDate(), json.getSubject(), json.getContent()));
}
@GET
@Path("/entry")
@Produces(MediaType.APPLICATION_JSON)
return entries;
}
@GET
@Path("/entry/{id}")
@Produces(MediaType.APPLICATION_JSON)
public EntryPOJO searchEntryById
(@PathParam
("id") int id
) throws Exception{
int index = -1;
for(int i=0; i<entries.size(); i++) {
if(entries.get(i).getId()==id) {
index = i;
}
}
return entries.get(index);
}
@PUT
@Path("/entry/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public void changeEntryById
(@PathParam
("id") int id, EntryPOJO json
) throws Exception{
int index = -1;
for(int i=0; i<entries.size(); i++) {
if(entries.get(i).getId()==id) {
index = i;
}
}
entries.get(index).setId(json.getId());
entries.get(index).setDate(json.getDate());
entries.get(index).setSubject(json.getSubject());
entries.get(index).setContent(json.getContent());
entries.get(index).setComments(json.getComments());
}
@DELETE
@Path("/entry/")
public void deleteAllEntries
() throws Exception{
entries.clear();
}
@DELETE
@Path("/entry/{id}")
public void deleteEntryById
(@PathParam
("id") int id
) throws Exception{
int index = -1;
for(int i=0; i<entries.size(); i++) {
if(entries.get(i).getId()==id) {
index = i;
}
}
entries.remove(index);
}
@PUT
@Path("/entry/{id}/comment")
@Consumes(MediaType.APPLICATION_JSON)
public void addComment
(@PathParam
("id") int id, CommentPOJO json
) throws Exception{
int index = -1;
for(int i=0; i<entries.size(); i++) {
if(entries.get(i).getId()==id) {
index = i;
}
}
entries.get(index).getComments().add(new CommentPOJO(json.getId(), json.getDate(), json.getUser(), json.getSubject(), json.getContent()));
}
@GET
@Path("/entry/{id}/comment")
@Produces(MediaType.APPLICATION_JSON)
public List<CommentPOJO
> showComment
(@PathParam
("id") int id
) throws Exception{
int index = -1;
for(int i=0; i<entries.size(); i++) {
if(entries.get(i).getId()==id) {
index = i;
}
}
return entries.get(index).getComments();
}
@DELETE
@Path("/entry/{id}/comment/{id}")
public void deleteComment
(@PathParam
("id") int id,@PathParam
("id") int id2
) throws Exception{
int indexEntry = -1;
int indexComment = -1;
for(int i=0; i<entries.size(); i++) {
for(int j=0; j<entries.get(i).getComments().size(); j++) {
if(entries.get(i).getComments().get(j).getId()==id2) {
indexEntry = i;
indexComment = j;
}
}
}
if(indexComment!=-1&&indexEntry!=-1) {
entries.get(indexEntry).getComments().remove(indexComment);
}
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}