Beispiel #1
0
public class Application extends Controller {

  static Form<Task> taskForm = Form.form(Task.class);

  public static Result index() {
    return redirect(routes.Application.tasks());
  }

  public static Result tasks() {
    return ok(views.html.index.render(Task.all(), taskForm));
  }

  public static Result newTask() {
    Form<Task> filledForm = taskForm.bindFromRequest();
    if (filledForm.hasErrors()) {
      return badRequest(views.html.index.render(Task.all(), filledForm));
    } else {
      Task.create(filledForm.get());
      return redirect(routes.Application.tasks());
    }
  }

  public static Result deleteTask(Long id) {
    Task.delete(id);
    return redirect(routes.Application.tasks());
  }
}
Beispiel #2
0
@With(DetectUser.class)
@Authenticated(UserAuthenticator.class)
public class TaskController extends Controller {

  static Form<Task> taskForm = Form.form(Task.class);

  public static User loadUser() {
    return User.fetch(session().get("username"));
  }

  public static Result tasks() {
    User user = loadUser();
    return ok(main.render("Tasks", null, tasks.render(user.tasks, taskForm)));
  }

  public static Result newTask() {
    User user = loadUser();
    Form<Task> filledForm = taskForm.bindFromRequest();
    if (filledForm.hasErrors()) {
      flash("error", "The task could not be saved.");
      return badRequest(main.render("Tasks", null, tasks.render(user.tasks, filledForm)));
    } else {
      Task task = filledForm.get();
      task.owner = user;
      task.save();
      return created(main.render("Tasks", null, tasks.render(user.tasks, taskForm)));
    }
  }

  public static Result deleteTask(Long id) {
    Task.delete(id);
    return redirect(routes.TaskController.tasks());
  }
}