package io.github.javageeks.mase.web;

import io.github.javageeks.mase.model.Todo;
import io.github.javageeks.mase.service.TodoService;

import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Todo rest web service.
 */
@Controller
public class TodoController {

	@Autowired
	private TodoService todoService;

	@RequestMapping(value = "/todos", method = RequestMethod.GET)
	public @ResponseBody
	List<Todo> getAllTodo() {
		return todoService.findAll();
	}

	@RequestMapping(value = "/todos/{todoId}", method = RequestMethod.GET)
	public @ResponseBody
	Todo getTodo(@PathVariable long todoId) {
		return todoService.findById(todoId);
	}

	@RequestMapping(value = "/todos/{todoId}", method = RequestMethod.POST)
	public @ResponseBody
	String createTodo(Todo todo) {
		todoService.create(todo);
		return todo.toString();
	}

	@RequestMapping(value = "/todos/{todoId}", method = RequestMethod.PUT)
	public @ResponseBody
	String editTodo(Todo todo) {
		// update with mongo template save method
		todoService.create(todo);
		return todo.toString();
	}

	@RequestMapping(value = "/todos/{todoId}", method = RequestMethod.DELETE)
	public @ResponseBody
	String deleteTodo(@PathVariable long todoId) {
		Todo todo = todoService.findById(todoId);
		todoService.delete(todo);
		return Response.ok().toString();
	}

}

@@ -158,43 +158,8 @@
 t;%0A%0A
-import javax.ws.rs.core.Response;%0A%0A
 impo
@@ -318,206 +318,9 @@
 ion.
-PathVariable;%0Aimport org.springframework.web.bind.annotation.RequestMapping;%0Aimport org.springframework.web.bind.annotation.RequestMethod;%0Aimport org.springframework.web.bind.annotation.ResponseBody
+*
 ;%0A%0A/
@@ -550,24 +550,72 @@
 AllTodo() %7B%0A
+        // return all todos for user in session%0A
 %09%09return tod
@@ -752,34 +752,36 @@
 o(@PathVariable 
-lo
+Stri
 ng todoId) %7B%0A%09%09r
@@ -848,33 +848,24 @@
 ue = %22/todos
-/%7BtodoId%7D
 %22, method = 
@@ -907,22 +907,20 @@
 seBody%0A%09
-String
+Todo
  createT
@@ -927,72 +927,78 @@
 odo(
-Todo todo) %7B%0A%09%09todoService.create(todo);%0A%09%09return todo.toString(
+@RequestBody Todo todo) %7B%0A        return todoService.saveOrUpdate(todo
 );%0A%09
@@ -1028,33 +1028,24 @@
 ue = %22/todos
-/%7BtodoId%7D
 %22, method = 
@@ -1086,22 +1086,20 @@
 seBody%0A%09
-String
+Todo
  editTod
@@ -1100,16 +1100,29 @@
 ditTodo(
+@RequestBody 
 Todo tod
@@ -1132,52 +1132,15 @@
 %7B%0A%09%09
-// update with mongo template save method%0A%09%09
+return 
 todo
@@ -1147,19 +1147,25 @@
 Service.
-cre
+saveOrUpd
 ate(todo
@@ -1171,34 +1171,8 @@
 o);%0A
-%09%09return todo.toString();%0A
 %09%7D%0A%0A
@@ -1269,22 +1269,20 @@
 seBody%0A%09
-String
+void
  deleteT
@@ -1303,10 +1303,12 @@
 ble 
-lo
+Stri
 ng t
@@ -1392,43 +1392,8 @@
 o);%0A
-%09%09return Response.ok().toString();%0A
 %09%7D%0A%0A

fix tod controller to accept POST requests