import model.Status;
import model.TodoDao;
import spark.*;
import spark.template.velocity.*;
import java.util.*;
import static spark.Spark.*;


/*
        //Connect to DB
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/uBay", "root", "1234");

        //Execute Query
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT first_name, last_name FROM account");

        //Get Query Results
        while (rs.next()) {
            //Retrieve by column name
            String FN = rs.getString("first_name");
            String LN = rs.getString("last_name");

            //Display values
            System.out.println(", First: " + FN);
            System.out.println(", Last: " + LN);
            System.out.println();
 */
















public class Ubay {

    public static void main(String [] args) {

        //Connect to DB
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/uBay", "root", "1234");

        exception(Exception.class, (e, req, res) -> e.printStackTrace()); // print all exceptions
        staticFiles.location("/public");
        port(9999);

        // Render main UI
        get("/", (req, res) -> renderGUI(req));
        get("/login", (req, res) -> renderLoginTemplate(req));
        put("/login2", (req, res) -> parseLogin(req)); }


    private static String parseLogin(Request req) {

        String email = req.queryParams("email");
        String password = req.queryParams("password");

        System.out.println(email);
        System.out.print(password);

    return renderTemplate("velocity/home.vm", new HashMap()); }

    private static String renderGUI(Request req) {
        String statusStr = req.queryParams("status");
        Map<String, Object> model = new HashMap<>();
        if ("true".equals(req.queryParams("ic-request"))) {
            return renderTemplate("velocity/todoList.vm", model);
        }
        return renderTemplate("velocity/index.vm", model);
    }

    private static String renderLoginTemplate(Request req) {
        Map<String, Object> model = new HashMap<>();
        return renderTemplate("velocity/login.vm", model);
    }

    private static String renderTemplate(String template, Map model) {
        return new VelocityTemplateEngine().render(new ModelAndView(model, template));
    }

}
@@ -1,20 +1,44 @@
+//import com.mysql.*;%0A//
 import model.Status;
@@ -38,16 +38,18 @@
 Status;%0A
+//
 import m
@@ -111,16 +111,36 @@
 ocity.*;
+%0A%0Aimport java.sql.*;
 %0Aimport 
@@ -921,24 +921,38 @@
 %5B%5D args) %7B%0A%0A
+        try %7B%0A
         //Co
@@ -1062,32 +1062,33 @@
 oot%22, %221234%22);%0A%0A
+%0A
         exceptio
@@ -1419,19 +1419,101 @@
 ogin(req
-));
+, con));%0A%0A        %7D catch (SQLException exc) %7B%0A        System.out.println(%22Error%22); %7D
  %7D%0A%0A%0A   
@@ -1553,24 +1553,40 @@
 (Request req
+, Connection con
 ) %7B%0A%0A       
@@ -1678,24 +1678,32 @@
 password%22);%0A
+        
 %0A        Sys
@@ -1703,43 +1703,337 @@
     
-System.out.println(email);%0A
+try %7B%0A            Statement stmt = con.createStatement();%0A            ResultSet rs = stmt.executeQuery(%22SELECT first_name, card FROM account WHERE first_name = '%22+email+%22'%22);%0A            rs.next();%0A            String cnum = rs.getString(%22card%22);%0A            System.out.println(cnum);%0A        %7D catch (SQLException exc)%7B%0A 
         
 Syst
@@ -2020,32 +2020,35 @@
  exc)%7B%0A         
+   
 System.out.print
@@ -2051,19 +2051,33 @@
 rint
-(password);
+ln(%22Name not found%22); %7D%0A%0A
 %0A%0A  

Set uo connection to DB and check if login email/name is valid