import java.sql.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Map;

import static spark.Spark.*;

import pro.tmedia.CardController;
import pro.tmedia.CardService;
import pro.tmedia.JsonUtil;
import spark.template.freemarker.FreeMarkerEngine;
import spark.ModelAndView;
import static spark.Spark.get;

import com.heroku.sdk.jdbc.DatabaseUrl;

public class Main {

  public static void main(String[] args) {

    port(Integer.valueOf(System.getenv("PORT")));
    staticFileLocation("/public");

    new CardController(new CardService());

    /*get("/is-server-available-status", (request, response) -> {
            Map<String, Object> attributes = new HashMap<>();
            attributes.put("message", "Server is available!");

            return new ModelAndView(attributes, "index.ftl");
        }, new FreeMarkerEngine());*/

    get("/is-server-available-status", (req, res) -> "Server is available!", JsonUtil.json());


    get("/depot", (req, res) -> {
      Connection connection = null;
      Map<String, Object> attributes = new HashMap<>();
      try {
        connection = DatabaseUrl.extract().getConnection();

        Statement stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
        stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
        ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");

        ArrayList<String> output = new ArrayList<String>();
        while (rs.next()) {
          output.add( "Read from DB: " + rs.getTimestamp("tick"));
        }

        attributes.put("results", output);
        return new ModelAndView(attributes, "depot.ftl");
      } catch (Exception e) {
        attributes.put("message", "There was an error: " + e);
        return new ModelAndView(attributes, "error.ftl");
      } finally {
        if (connection != null) try{connection.close();} catch(SQLException e){}
      }
    }, new FreeMarkerEngine());

    /* Testing DB with recording of "ticks" */
    get("/", (req, res) -> {
      Connection connection = null;
      Map<String, Object> attributes = new HashMap<>();
      try {
        connection = DatabaseUrl.extract().getConnection();

        Statement stmt = connection.createStatement();
        stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ticks (tick timestamp)");
        stmt.executeUpdate("INSERT INTO ticks VALUES (now())");
        ResultSet rs = stmt.executeQuery("SELECT tick FROM ticks");

        ArrayList<String> output = new ArrayList<String>();
        while (rs.next()) {
          output.add( "Read from DB: " + rs.getTimestamp("tick"));
        }

        attributes.put("results", output);
        return new ModelAndView(attributes, "db.ftl");
      } catch (Exception e) {
        attributes.put("message", "There was an error: " + e);
        return new ModelAndView(attributes, "error.ftl");
      } finally {
        if (connection != null) try{connection.close();} catch(SQLException e){}
      }
    }, new FreeMarkerEngine());



    get("/общественная-приемная", (request, response) -> {
      Map<String, Object> attributes = new HashMap<>();
      attributes.put("message", "Hello World!");

      return new ModelAndView(attributes, "mp-public-reception.ftl");
    }, new FreeMarkerEngine());

  }

}

@@ -1985,12 +1985,10 @@
  /* 
-Test
+Us
 ing 
@@ -2009,20 +2009,22 @@
 ing of %22
-tick
+mantra
 s%22 */%0A  
@@ -2317,39 +2317,56 @@
 E IF NOT EXISTS 
-ticks (
+mantra (wisdom varchar, 
 tick timestamp)%22
@@ -2368,32 +2368,34 @@
 amp)%22);%0A        
+//
 stmt.executeUpda
@@ -2479,39 +2479,42 @@
 ery(%22SELECT 
-tick FROM ticks
+wisdom FROM mantra
 %22);%0A%0A       
@@ -2558,32 +2558,126 @@
 List%3CString%3E();%0A
+        output.add(%22%3Cbutton%3E%D0%97%D0%B0%D0%BF%D0%B8%D1%81%D0%B0%D1%82%D1%8C%3C/button%3E%3Cbutton%3E%D0%A1%D0%BE%D1%85%D1%80%D0%B0%D0%BD%D0%B8%D1%82%D1%8C%3C/button%3E%3Cdiv%3E...%3C/div%3E%3Cbr/%3E%22);%0A
         while (r
@@ -2690,32 +2690,34 @@
 ()) %7B%0A          
+//
 output.add( %22Rea
@@ -2749,32 +2749,216 @@
 stamp(%22tick%22));%0A
+          //output.add( %22Read from DB: %22 + rs.getString(%22wisdom%22));%0A          output.add( %22%3Cdiv class=%5C%22wisdom%5C%22 style=%5C%22background: #CCCCCC;%5C%22%3E%22 + rs.getString(%22wisdom%22) + %22%3C/div%3E%22);%0A
         %7D%0A%0A     
@@ -3342,16 +3342,484 @@
 e());%0A%0A%0A
+    get(%22/mantra/create%22, (req, res) -%3E %7B%0A      Connection connection = null;%0A      Map%3CString, Object%3E attributes = new HashMap%3C%3E();%0A      try %7B%0A        connection = DatabaseUrl.extract().getConnection();%0A%0A        Statement stmt = connection.createStatement();%0A        stmt.executeUpdate(%22CREATE TABLE IF NOT EXISTS mantra (wisdom varchar, tick timestamp)%22);%0A        stmt.executeUpdate(%22INSERT INTO mantra VALUES (%5C%22%22 + req.params(%22wisdom%22) + %22%5C%22,now())%22);%0A    %7D);%0A%0A%0A
 %0A    get

Сохранить и записать, работа с базой, отображение и создание заметок