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 {
//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)
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}")
int index = -1;
for(int i=0; i<entryList.size(); i++) {
if(entryList.get(i).getId().equals(id)) {
index = i;
}
}
entryList.remove(index);
}
//usuwa wszystkie wpisy
@DELETE
@Path("/entry")
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<entryList.size(); i++) {
if(entryList.get(i).getId().equals(id)) {
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)
int index = -1;
for(int i=0; i<entryList.size(); i++) {
if(entryList.get(i).getId().equals(id)) {
index = i;
}
}
return entryList.get(index).getCommentPOJOList();
}
//usuniecie komentarza dla danego wpisu
@DELETE
@Path("/entry/{id}/comment/{id2}")
public void deleteCommentForPostById
(@PathParam
("id") String entryId, @PathParam
("id2") String commentId
) throws Exception{
int index = -1;
for(int i=0; i<entryList.size(); i++) {
for (int j = 0; j<entryList.get(i).getCommentPOJOList().size(); j++){
if(entryList.get(i).getCommentPOJOList().get(j).getId().equals(commentId)){
entryList.get(i).getCommentPOJOList().remove(j);
break;
}
}
}
}
}
Replies to Re: Untitled
Title |
Name |
Language |
UNIX |
When |
Re: Re: Untitled |
Sharp Anoa |
java5 |
1495660559 |
6 Years ago. |
{"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"}