Large diffs are not rendered by default.

@@ -3,6 +3,7 @@
import java.util.List;
import java.util.Map;

import dao.Sql2oCategoryDao;
import dao.Sql2oTaskDao;
import models.Category;
import models.Task;
@@ -18,6 +19,9 @@ public static void main(String[] args) {
staticFileLocation("/public");
String connectionString = "jdbc:h2:~/todolist.db;INIT=RUNSCRIPT from 'classpath:db/create.sql'";
Sql2o sql2o = new Sql2o(connectionString,"","");

//Instantiate Data Access Objects for task and category
Sql2oCategoryDao categoryDao = new Sql2oCategoryDao(sql2o);
Sql2oTaskDao taskDao = new Sql2oTaskDao(sql2o);

//get: delete all tasks
@@ -33,24 +37,38 @@ public static void main(String[] args) {
//get: show new task form
get("/tasks/new", (req, res) -> {
Map<String, Object> model = new HashMap<>();
// List<Category> allCategories = taskDao.getAllCategories();

// model.put("availableCategories", allCategories);

if (categoryDao.getAllCategories().size() > 0) {
List<Category> allCategories = categoryDao.getAllCategories();
model.put("availableCategories", allCategories);
}
return new ModelAndView(model, "task-form.hbs");
}, new HandlebarsTemplateEngine());

//task: process new task form
post("/tasks/new", (request, response) -> { //URL to make new task on POST route
Map<String, Object> model = new HashMap<>();
String description = request.queryParams("description");
int thisCategory = Integer.parseInt(request.queryParams("pickles") );
String newCategory = request.queryParams("pickles");
int thisCategory = Integer.parseInt(request.queryParams("thatCategory"));
if (newCategory != null) {
categoryDao.add(new Category(newCategory));
}
Task newTask = new Task(description, thisCategory);
taskDao.add(newTask);
model.put("task", newTask);
return new ModelAndView(model, "success.hbs");
}, new HandlebarsTemplateEngine());

//If categories exist, display them
post("/newCategory", (request, response) -> {
Map<String, Object> model = new HashMap<>();
String newFocus = request.queryParams("pickles");
categoryDao.add(new Category(newFocus));
List<Category> allCategories = categoryDao.getAllCategories();
model.put("availableCategories", allCategories);
return new ModelAndView(model, "task-form.hbs");
}, new HandlebarsTemplateEngine());

//get: show all tasks
get("/", (req, res) -> {
Map<String, Object> model = new HashMap<>();
@@ -10,4 +10,5 @@ CREATE TABLE IF NOT EXISTS tasks (
CREATE TABLE IF NOT EXISTS categories (
id int PRIMARY KEY auto_increment,
focus VARCHAR
);
);

@@ -5,31 +5,37 @@
<form action="/tasks/{{editTask.id}}/update" method="post">
<label for="description">Edit this task's description</label>
<input id="description" name="description" type="text" value="{{editTask.description}}">

</form>
{{else}}
<h1>Add a new Task!</h1>

<form action="/tasks/new" method="post">
<label for="categoryId">New category: </label>
<input id="categoryId" name="pickles" type="text">
{{#if availableCategories}}
<h1>Add a new Task!</h1>
<form action="/tasks/new" method="post">
<div class="form-group">
<label for="whichCategory">Category: </label>
<select name="thatCategory" id="whichCategory">
{{#each availableCategories}}
<option value="{{id}}">{{focus}}</option>
{{/each}}
</select>
</div>
<div class="form-group">
<label for="description">Task Description: </label>
<input id="description" name="description" type="text">
</div>
{{else}}
<form action="/newCategory" method="post">
<label for="categoryId">Add a category: </label>
<input id="categoryId" name="pickles" type="text">
</form>

<div class="form-group">
<label for="whichCategory">Category: </label>
<select name="whatCategory" id="whichCategory">

{{#each availableCategories}}
<option value="{{id}}">{{focus}}</option>
{{/each}}
{{/if}}


</select>
</div>
<div class="form-group">
<label for="description">Task Description: </label>
<input id="description" name="description" type="text">
</div>
{{/if}}
<button type="submit" class="btn btn-default">Go!</button>
</form>
{{/partial}}
{{/partial}}

{{> layout.hbs}}