To be able to retrieve and store the details of a todo, we need a Todo bean and a service to retrieve and store the details.
Let's create a Todo bean:
public class Todo {
private int id;
private String user;
private String desc;
private Date targetDate;
private boolean isDone;
public Todo() {}
public Todo(int id, String user, String desc,
Date targetDate, boolean isDone) {
super();
this.id = id;
this.user = user;
this.desc = desc;
this.targetDate = targetDate;
this.isDone = isDone;
}
//ALL Getters
}
We have a created a simple Todo bean with the ID, the name of the user, the description of the todo, the todo target date, and an indicator for the completion status. We have added a constructor and getters for all the fields.
Let's add TodoService now. To keep things simple, this service does not talk to the database. It maintains an in-memory array list of todos. This list is initialized using a static initializer:
@Service
public class TodoService {
private static List<Todo> todos = new ArrayList<Todo>();
private static int todoCount = 3;
static {
todos.add(new Todo(1, "Jack", "Learn Spring MVC", new Date(), false));
todos.add(new Todo(2, "Jack", "Learn Struts", new Date(), false));
todos.add(new Todo(3, "Jill", "Learn Hibernate", new Date(), false));
}
Some of the important methods are as follows:
public List<Todo> retrieveTodos(String user) {
//returns all todos for a given users
}
public Todo addTodo(String name, String desc, Date targetDate, boolean isDone) {
//logic for adding a todo
}
public Todo retrieveTodo(int id) {
//retrieve a todo
}
public Todo update(Todo todo) {
//update a todo
}
public Todo deleteById(int id) {
//delete a todo
}
}
Now that we have the service and the bean ready, we can create our first service to retrieve a list of todos for a user.