package com.xebia.xoverflow.server;

import com.xebia.xoverflow.server.exception.BadRequestException;
import com.xebia.xoverflow.server.model.Post;
import com.xebia.xoverflow.server.service.DaggerModule;
import com.xebia.xoverflow.server.service.PostRepositoryService;
import dagger.ObjectGraph;
import org.codehaus.jackson.map.ObjectMapper;
import spark.Request;

import javax.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static spark.Spark.*;

public class XoverflowServer {


    private final PostRepositoryService repositoryService;

    public static void main(String[] args) {
        ObjectGraph graph = ObjectGraph.create(DaggerModule.class);

        XoverflowServer xoverflowServer = graph.get(XoverflowServer.class);

        xoverflowServer.runServer();
    }


    private final ObjectMapper objectMapper;

    @Inject
    public XoverflowServer(ObjectMapper mapper, PostRepositoryService repositoryService ) {
        this.objectMapper = mapper;
        this.repositoryService = repositoryService;
    }


    public void runServer() {
        staticFileLocation("/");

        // Get All posts
        get("/posts", (request, response) -> {

            List<Post> posts = repositoryService.listLast10Posts();

            return postToJson(posts);
        });

        // Create a post
        put("/post", (request, response) -> {
            Post post = parsePostFromRequest(request);

            post = repositoryService.create(post);

            post = new Post();
            post.setSubject("Qui a gagné le Hackathon ?");
            post.setBody("Je voudrais connaitre l'identité du gagnant du Hackathon :)");
            post.setDate(new Date());
            post.setUserName("rbung");
            post.setAnswers(new ArrayList<>());
            post.setId("azertyuytrez");
            return post;
          //  return postToJson(post);
        });

        get("/hello", (request, response) -> "Hello World!");

    }


    private Post parsePostFromRequest(Request request) {
        Post post;
        try {
            post = objectMapper.readValue(request.body(), Post.class);
        } catch (IOException e) {
            throw new BadRequestException(e);
        }
        return post;
    }

    private String postToJson(Object objectToSerializeInJson) {
        try {
            return objectMapper.writeValueAsString(objectToSerializeInJson);
        } catch (IOException e) {
            throw new BadRequestException(e);
        }
    }

}

@@ -1296,24 +1296,60 @@
 10Posts();%0A%0A
+             posts = stubdedList();%0A
             
@@ -1375,32 +1375,33 @@
 osts);%0A        %7D
+ 
 );%0A%0A        // C
@@ -2050,32 +2050,568 @@
 rld!%22);%0A%0A    %7D%0A%0A
+    private List%3CPost%3E stubdedList() %7B%0A        List%3CPost%3E res = new ArrayList%3C%3E();%0A%0A        for (int i = 0; i %3C 10; i++) %7B%0A            Post post = new Post();%0A            post.setSubject(%22Qui a gagn%C3%A9 le Hackathon ?%22);%0A            post.setBody(%22Je voudrais connaitre l'identit%C3%A9 du gagnant du Hackathon :)%22);%0A            post.setDate(new Date());%0A            post.setUserName(%22rbung%22);%0A            post.setAnswers(new ArrayList%3C%3E());%0A            post.setId(%22azertyuytrez%22);%0A            res.add(post);%0A        %7D%0A        return res;%0A    %7D%0A%0A
 %0A    private Pos

Add stubed post list