Facebook
From Scanty Pheasant, 6 Years ago, written in Java 5.
This paste is a reply to Untitled from Big Anoa - go back
Embed
Viewing differences between Untitled and Re: Untitled
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 {

    public List entryList = new ArrayList<>();

    //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 showEntry() throws Exception{
        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             if(entryList.get(i).getId().equals(id)) {
                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             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)
    public List showComment(@PathParam("id") String id) throws Exception{

        int index = -1;
        for(int i=0; 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/{id}")
    
@Path("/entry/{id}/comment/{id2}")
    
public void  deleteCommentForPostById(@PathParam("id") String entryId, @PathParam("id") @PathParam("id2") String commentId) throws Exception{


        int index = -1;
        for(int i=0; i                 for (int j = 0; j                     if(entryList.get(i).getCommentPOJOList().get(j).getId().equals(commentId)){
                        entryList.get(i).getCommentPOJOList().remove(j);
                        break;
                    }
                }

            }
        }
}

Replies to Re: Untitled rss

Title Name Language When
Re: Re: Untitled Sharp Anoa java5 6 Years ago.
captcha