package ru.qatools.school.twister.resources;

import org.glassfish.jersey.server.mvc.ErrorTemplate;
import org.glassfish.jersey.server.mvc.Template;
import ru.qatools.school.twister.models.Post;
import ru.qatools.school.twister.models.Comment;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

/**
 * eroshenkoam
 * 15/11/14
 */
@Path("post")
@Produces(MediaType.TEXT_HTML)
@ErrorTemplate(name = "/error.ftl")
public class PostResources {

    @GET
    @Path("/{id}")
    @Template(name = "/post/showPost.ftl")
    public Post showPost(@PathParam("id") int id) {
        return Post.findById(id);
    }

    @GET
    @Path("/all")
    @Template(name = "/post/showPosts.ftl")
    public List<Post> showPosts() {
        return Post.findAll();
    }

    @GET
    @Path("/new")
    @Template(name = "/post/newPost.ftl")
    public Post newPost() {
        return new Post();
    }

    @POST
    @Path("/")
    @Template(name = "/post/showPost.ftl")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Post createPost(@FormParam("title") String title,
                           @FormParam("body") String body) {
        Post post = new Post();
        post.setTitle(title);
        post.setBody(body);
        post.saveIt();
        return post;
    }

    @POST
    @Path("/{id}/addComment")
    @Template(name = "/post/showPost.ftl")
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public Post addComment(@PathParam("id") String fPostId,
                           @FormParam("commentBody") String fCommentBody) {
        
    	Comment comment = new Comment();
        comment.setPost(Integer.parseInt(fPostId));
        comment.setBody(fCommentBody);
        comment.saveIt();

        return Post.findById(fPostId);
    }
}

@@ -255,15 +255,95 @@
 vax.
-ws.rs.*
+servlet.http.HttpServletResponse;%0Aimport javax.ws.rs.*;%0Aimport javax.ws.rs.core.Context
 ;%0Aim
@@ -375,16 +375,44 @@
 iaType;%0A
+import java.io.IOException;%0A
 import j
@@ -573,16 +573,64 @@
 rces %7B%0A%0A
+    @Context%0A    HttpServletResponse response;%0A%0A
     @GET
@@ -1090,51 +1090,8 @@
 /%22)%0A
-    @Template(name = %22/post/showPost.ftl%22)%0A
     
@@ -1150,20 +1150,22 @@
  public 
-Post
+String
  createP
@@ -1256,24 +1256,43 @@
 String body)
+ throws IOException
  %7B%0A        P
@@ -1391,24 +1391,25 @@
 t.saveIt();%0A
+%0A
         retu
@@ -1410,17 +1410,72 @@
   re
-turn post
+sponse.sendRedirect(%22/post/%22 + post.getId());%0A%0A        return %22%22
 ;%0A  
@@ -1523,51 +1523,8 @@
 t%22)%0A
-    @Template(name = %22/post/showPost.ftl%22)%0A
     
@@ -1583,20 +1583,22 @@
  public 
-Post
+String
  addComm
@@ -1707,16 +1707,35 @@
 entBody)
+ throws IOException
  %7B%0A     
@@ -1908,35 +1908,67 @@
   re
-turn Post.findById(fPostId)
+sponse.sendRedirect(%22/post/%22 + fPostId);%0A%0A        return %22%22
 ;%0A  

add redirect after post requests