/**
 * Created by Siclait on 18/7/16.
 */
import Entity.User;
import JSONTools.ResponseError;
import org.h2.engine.Database;

import static JSONTools.JSONUtil.json;
import static spark.Spark.after;
import static spark.Spark.get;

public class JSONServiceController {

    public JSONServiceController() {

        // GETS
        // Fetch All Urls
        get("/json/allurls", (req, res) -> DatabaseManager.FetchAllURL(), json());

        // Fetch Specific Short Url
        get("/json/original/:short",  (req, res) -> {
            String shortURL = req.params(":short");

            String url = DatabaseManager.FetchOriginalURL(shortURL);

            if(url != null)
                return url;

            res.status(400);
            return new ResponseError("No url with id %s found", shortURL);

        }, json());

        // Fetch All Users
        get("/json/allusers", (req, res) -> DatabaseManager.FetchAllUsers(), json());

        // Fetch Specific User
        get("/json/user/:username", (req, res) -> {
            String username = req.params(":username");

            User user = DatabaseManager.FetchUser(username);

            if(user != null)
                return user;

            res.status(400);
            return new ResponseError("No user with id %s found", username);

        }, json());

        get("/json/:user/urls", (req, res) -> {
            String username = req.params(":user");

            return DatabaseManager.FetchAllURLForUser(username);
        }, json());

        // POSTS
        

        after("/json/*", (req, res) -> res.type("application/json"));
    }
}


@@ -35,16 +35,35 @@
 16.%0A */%0A
+import Entity.URL;%0A
 import E
@@ -70,24 +70,24 @@
 ntity.User;%0A
-
 import JSONT
@@ -832,32 +832,377 @@
    %7D, json());%0A%0A
+        get(%22/json/url/:short%22, (req, res) -%3E %7B%0A%0A            String shortURL = req.params(%22:short%22);%0A%0A            URL url = DatabaseManager.FetchURL(shortURL);%0A%0A            if(url != null)%0A                return url;%0A%0A            res.status(400);%0A            return new ResponseError(%22No url with id %25s found%22, shortURL);%0A%0A        %7D,  json());%0A%0A
         // Fetch
@@ -1889,24 +1889,16 @@
 / POSTS%0A
-        
 %0A%0A      

Add Fetch Url Json function