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 {
public List
//Dodanie wpisu
@POST
@Path("/entry")
@Produces(MediaType.APPLICATION_JSON)
public void addEntry(EntryPOJO json) throws Exception{
entryList.add(new EntryPOJO(json.getId(),json.getDate(),json.getSubject(),json.getContent()));
}
//wyswietlanie wszystkich wpisow
@GET
@Path("/entry")
@Produces(MediaType.APPLICATION_JSON)
public List
return entryList;
}
//wyswietlanie wpisu o danym id
@GET
@Path("/entry/{id}")
@Produces(MediaType.APPLICATION_JSON)
public EntryPOJO showEntryById(@PathParam("id") String id) throws Exception{
if(entryList.isEmpty()){
return null;
}
else{
for(EntryPOJO entry: entryList){
if(entry.getId().equals(id)){
return entry;
}
}
}
return null;
}
//edycja wpisu o danym id
@PUT
@Path("/entry/{id}")
@Consumes(MediaType.APPLICATION_JSON)
public void editEntryById(@PathParam("id") String id, EntryPOJO json) throws Exception{
for (EntryPOJO entry: entryList){
if(entry.getId().equals(id)){
entry.setId(json.getId());
entry.setDate(json.getDate());
entry.setSubject(json.getSubject());
entry.setContent(json.getContent());
}
}
}
//usuwanie wpisu po id
@DELETE
@Path("/entry/{id}")
public void deleteEntryById(@PathParam("id") String id) throws Exception{
int index = -1;
for(int i=0; i
index = i;
}
}
entryList.remove(index);
}
//usuwa wszystkie wpisy
@DELETE
@Path("/entry")
public void deleteEntry() throws Exception{
entryList.clear();
}
//dodanie komentarza
@PUT
@Path("/entry/{id}/comment")
@Produces(MediaType.APPLICATION_JSON)
public void addComment(@PathParam("id") String id, CommentPOJO json) throws Exception{
int index = -1;
for(int i=0; i
index = i;
}
}
entryList.get(index).getCommentPOJOList().add(new CommentPOJO(json.getId(), json.getDate(), json.getUser(), json.getSubject(), json.getContent()));
}
//wyswietlenie komentarzy dla wpisu id
@GET
@Path("/entry/{id}/comment")
@Produces(MediaType.APPLICATION_JSON)
public List
int index = -1;
for(int i=0; i
index = i;
}
}
return entryList.get(index).getCommentPOJOList();
}
//usuniecie komentarza dla danego wpisu
@DELETE
public void deleteCommentForPostById(@PathParam("id") String entryId,
int index = -1;
for(int i=0; i
entryList.get(i).getCommentPOJOList().remove(j);
break;
}
}
}
}
}