Facebook
From Big Anoa, 6 Years ago, written in Java 5.
Embed
Download Paste or View Raw
Hits: 314
  1. package lab6.rest.service;
  2.  
  3. import lab6.rest.pojo.CommentPOJO;
  4. import lab6.rest.pojo.EntryPOJO;
  5.  
  6. import javax.inject.Singleton;
  7. import javax.ws.rs.*;
  8. import javax.ws.rs.core.MediaType;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. @Singleton
  13. @Path("/blog")
  14. public class Blog {
  15.  
  16.     public List<EntryPOJO> entryList = new ArrayList<>();
  17.  
  18.     //Dodanie wpisu
  19.     @POST
  20.     @Path("/entry")
  21.     @Produces(MediaType.APPLICATION_JSON)
  22.     public void addEntry(EntryPOJO json) throws Exception{
  23.         entryList.add(new EntryPOJO(json.getId(),json.getDate(),json.getSubject(),json.getContent()));
  24.     }
  25.  
  26.     //wyswietlanie wszystkich wpisow
  27.     @GET
  28.     @Path("/entry")
  29.     @Produces(MediaType.APPLICATION_JSON)
  30.     public List<EntryPOJO> showEntry() throws Exception{
  31.         return entryList;
  32.     }
  33.  
  34.     //wyswietlanie wpisu o danym id
  35.     @GET
  36.     @Path("/entry/{id}")
  37.     @Produces(MediaType.APPLICATION_JSON)
  38.     public EntryPOJO showEntryById(@PathParam("id") String id) throws Exception{
  39.  
  40.         if(entryList.isEmpty()){
  41.             return null;
  42.         }
  43.         else{
  44.             for(EntryPOJO entry: entryList){
  45.                 if(entry.getId().equals(id)){
  46.                     return entry;
  47.                 }
  48.             }
  49.         }
  50.         return null;
  51.     }
  52.  
  53.     //edycja wpisu o danym id
  54.     @PUT
  55.     @Path("/entry/{id}")
  56.     @Consumes(MediaType.APPLICATION_JSON)
  57.     public void editEntryById(@PathParam("id") String id, EntryPOJO json) throws Exception{
  58.  
  59.         for (EntryPOJO entry: entryList){
  60.             if(entry.getId().equals(id)){
  61.                 entry.setId(json.getId());
  62.                 entry.setDate(json.getDate());
  63.                 entry.setSubject(json.getSubject());
  64.                 entry.setContent(json.getContent());
  65.             }
  66.         }
  67.     }
  68.  
  69.     //usuwanie wpisu po id
  70.     @DELETE
  71.     @Path("/entry/{id}")
  72.     public void deleteEntryById(@PathParam("id") String id) throws Exception{
  73.  
  74.  
  75.         int index = -1;
  76.         for(int i=0; i<entryList.size(); i++) {
  77.             if(entryList.get(i).getId().equals(id)) {
  78.                 index = i;
  79.             }
  80.         }
  81.         entryList.remove(index);
  82.     }
  83.  
  84.     //usuwa wszystkie wpisy
  85.     @DELETE
  86.     @Path("/entry")
  87.     public void deleteEntry() throws Exception{
  88.         entryList.clear();
  89.     }
  90.  
  91.     //dodanie komentarza
  92.     @PUT
  93.     @Path("/entry/{id}/comment")
  94.     @Produces(MediaType.APPLICATION_JSON)
  95.     public void addComment(@PathParam("id") String id, CommentPOJO json) throws Exception{
  96.  
  97.         int index = -1;
  98.         for(int i=0; i<entryList.size(); i++) {
  99.             if(entryList.get(i).getId().equals(id)) {
  100.                 index = i;
  101.             }
  102.         }
  103.         entryList.get(index).getCommentPOJOList().add(new CommentPOJO(json.getId(), json.getDate(), json.getUser(), json.getSubject(), json.getContent()));
  104.     }
  105.  
  106.     //wyswietlenie komentarzy dla wpisu id
  107.     @GET
  108.     @Path("/entry/{id}/comment")
  109.     @Produces(MediaType.APPLICATION_JSON)
  110.     public List<CommentPOJO> showComment(@PathParam("id") String id) throws Exception{
  111.  
  112.         int index = -1;
  113.         for(int i=0; i<entryList.size(); i++) {
  114.             if(entryList.get(i).getId().equals(id)) {
  115.                 index = i;
  116.             }
  117.         }
  118.  
  119.         return entryList.get(index).getCommentPOJOList();
  120.     }
  121.  
  122.     //usuniecie komentarza dla danego wpisu
  123.     @DELETE
  124.     @Path("/entry/{id}/comment/{id}")
  125.     public void  deleteCommentForPostById(@PathParam("id") String entryId, @PathParam("id") String commentId) throws Exception{
  126.  
  127.  
  128.         int index = -1;
  129.         for(int i=0; i<entryList.size(); i++) {
  130.                 for (int j = 0; j<entryList.get(i).getCommentPOJOList().size(); j++){
  131.                     if(entryList.get(i).getCommentPOJOList().get(j).getId().equals(commentId)){
  132.                         entryList.get(i).getCommentPOJOList().remove(j);
  133.                         break;
  134.                     }
  135.                 }
  136.  
  137.             }
  138.         }
  139. }
  140.  

Replies to Untitled rss

Title Name Language When
Re: Untitled Scanty Pheasant java5 6 Years ago.