Facebook
From Small Leech, 6 Years ago, written in Java 5.
Embed
Download Paste or View Raw
Hits: 259
  1. package lab6.rest.service;
  2.  
  3. import lab6.rest.pojo.CommentPOJO;
  4. import lab6.rest.pojo.EntryPOJO;
  5. import javax.inject.Singleton;
  6. import javax.ws.rs.*;
  7. import javax.ws.rs.core.MediaType;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. @Singleton
  12. @Path("/blog")
  13. public class Blog {
  14.  
  15.    private List<EntryPOJO> entries = new ArrayList<>();
  16.  
  17.     @POST
  18.     @Path("/entry")
  19.     @Produces(MediaType.APPLICATION_JSON)
  20.     public void addEntry(EntryPOJO json) throws Exception{
  21.        entries.add(new EntryPOJO(json.getId(), json.getDate(), json.getSubject(), json.getContent()));
  22.     }
  23.  
  24.     @GET
  25.     @Path("/entry")
  26.     @Produces(MediaType.APPLICATION_JSON)
  27.     public List<EntryPOJO> showAllEntries() throws Exception{
  28.         return entries;
  29.     }
  30.  
  31.     @GET
  32.     @Path("/entry/{id}")
  33.     @Produces(MediaType.APPLICATION_JSON)
  34.     public EntryPOJO searchEntryById(@PathParam("id") int id) throws Exception{
  35.  
  36.         int index = -1;
  37.         for(int i=0; i<entries.size(); i++) {
  38.             if(entries.get(i).getId()==id) {
  39.                 index = i;
  40.             }
  41.         }
  42.         return entries.get(index);
  43.     }
  44.  
  45.     @PUT
  46.     @Path("/entry/{id}")
  47.     @Consumes(MediaType.APPLICATION_JSON)
  48.     public void changeEntryById(@PathParam("id") int id, EntryPOJO json) throws Exception{
  49.  
  50.         int index = -1;
  51.  
  52.         for(int i=0; i<entries.size(); i++) {
  53.             if(entries.get(i).getId()==id) {
  54.                 index = i;
  55.             }
  56.         }
  57.  
  58.         entries.get(index).setId(json.getId());
  59.         entries.get(index).setDate(json.getDate());
  60.         entries.get(index).setSubject(json.getSubject());
  61.         entries.get(index).setContent(json.getContent());
  62.         entries.get(index).setComments(json.getComments());
  63.  
  64.     }
  65.  
  66.     @DELETE
  67.     @Path("/entry/")
  68.     public void deleteAllEntries() throws Exception{
  69.         entries.clear();
  70.     }
  71.  
  72.     @DELETE
  73.     @Path("/entry/{id}")
  74.     public void deleteEntryById(@PathParam("id") int id) throws Exception{
  75.  
  76.         int index = -1;
  77.         for(int i=0; i<entries.size(); i++) {
  78.             if(entries.get(i).getId()==id) {
  79.                 index = i;
  80.             }
  81.         }
  82.         entries.remove(index);
  83.     }
  84.  
  85.     @PUT
  86.     @Path("/entry/{id}/comment")
  87.     @Consumes(MediaType.APPLICATION_JSON)
  88.     public void addComment(@PathParam("id") int id, CommentPOJO json) throws Exception{
  89.  
  90.         int index = -1;
  91.         for(int i=0; i<entries.size(); i++) {
  92.             if(entries.get(i).getId()==id) {
  93.                 index = i;
  94.             }
  95.         }
  96.         entries.get(index).getComments().add(new CommentPOJO(json.getId(), json.getDate(), json.getUser(), json.getSubject(), json.getContent()));
  97.     }
  98.  
  99.     @GET
  100.     @Path("/entry/{id}/comment")
  101.     @Produces(MediaType.APPLICATION_JSON)
  102.     public List<CommentPOJO> showComment(@PathParam("id") int id) throws Exception{
  103.  
  104.         int index = -1;
  105.         for(int i=0; i<entries.size(); i++) {
  106.             if(entries.get(i).getId()==id) {
  107.                 index = i;
  108.             }
  109.         }
  110.  
  111.         return entries.get(index).getComments();
  112.     }
  113.  
  114.     @DELETE
  115.     @Path("/entry/{id}/comment/{id}")
  116.     public void deleteComment(@PathParam("id") int id,@PathParam("id") int id2 ) throws Exception{
  117.  
  118.         int indexEntry = -1;
  119.         int indexComment = -1;
  120.         for(int i=0; i<entries.size(); i++) {
  121.             for(int j=0; j<entries.get(i).getComments().size(); j++) {
  122.                 if(entries.get(i).getComments().get(j).getId()==id2) {
  123.                     indexEntry = i;
  124.                     indexComment = j;
  125.                 }
  126.             }
  127.         }
  128.         if(indexComment!=-1&&indexEntry!=-1) {
  129.             entries.get(indexEntry).getComments().remove(indexComment);
  130.         }
  131.  
  132.  
  133.     }
  134.  
  135. }