public ActionResult Edit(int id, Task updatedTask)
        {
            var existingTask = s_tasks.Single(t => t.TaskId == id);
            existingTask.Title = updatedTask.Title;
            existingTask.Description = updatedTask.Description;
            existingTask.Completed = updatedTask.Completed;

            return RedirectToAction("GetAll");
        }
        public void AddTask(Task task)
        {
            using (var connection = CreateConnection()) {
                var command = connection.CreateCommand();
                command.CommandText = "insert into task(title, description) values (@title, @description)";
                command.Parameters.AddWithValue("@title", task.Title);
                command.Parameters.AddWithValue("@description", task.Description);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        public void AddTask(Task task)
        {
            using (var connection = DatabaseConnection.CreateConnection()) {
                var command = connection.CreateCommand();
                command.CommandText = "insert into task(title, description, createdby) values (@title, @description, @userid)";
                command.Parameters.AddWithValue("@title", task.Title);
                command.Parameters.AddWithValue("@description", task.Description);
                command.Parameters.AddWithValue("@userid", m_currentUser.UserId);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        public void UpdateTask(int id, Task task)
        {
            using (var connection = CreateConnection()) {
                var command = connection.CreateCommand();
                command.CommandText = "update task set title = @title, description = @descr, completed = @cmpl where taskid = @taskid";
                command.Parameters.AddWithValue("@taskid", id);
                command.Parameters.AddWithValue("@title", task.Title);
                command.Parameters.AddWithValue("@descr", task.Description);
                command.Parameters.AddWithValue("@cmpl", task.Completed);

                connection.Open();
                command.ExecuteNonQuery();
            }
        }
        public int AddTask(Task task)
        {
            using (var connection = DatabaseConnection.CreateConnection()) {
                var command = connection.CreateCommand();
                command.CommandText = "add_task";
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@title", task.Title);
                command.Parameters.AddWithValue("@description", task.Description);
                command.Parameters.AddWithValue("@created_by", m_currentUser.UserId);
                command.Parameters.AddWithValue("@task_id", -1).Direction = ParameterDirection.Output;

                connection.Open();
                command.ExecuteNonQuery();

                return (int) command.Parameters["@task_id"].Value;
            }
        }
        public IEnumerable<Task> FindUncompletedTasks()
        {
            using (var connection = CreateConnection()) {
                var command = connection.CreateCommand();
                command.CommandText = "select * from task where completed = 0";

                connection.Open();
                var tasks = new List<Task>();
                using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) {
                    while (reader.Read()) {
                        var task = new Task();
                        task.TaskId = reader.GetInt32(0);
                        task.Title = reader.GetString(1);
                        task.Description = reader.GetString(2);
                        task.Completed = reader.GetBoolean(3);
                        tasks.Add(task);
                    }
                }

                return tasks;
            }
        }
        public Task FindTask(int id)
        {
            using (var connection = CreateConnection()) {
                var command = connection.CreateCommand();
                command.CommandText = "select * from task where taskid = @taskid";
                command.Parameters.AddWithValue("@taskid", id);

                connection.Open();
                using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection)) {
                    if (reader.Read()) {
                        // TODO: Extract method for reading task and use in all task readering methods.
                        var task = new Task();
                        task.TaskId = reader.GetInt32(0);
                        task.Title = reader.GetString(1);
                        task.Description = reader.GetString(2);
                        task.Completed = reader.GetBoolean(3);
                        return task;
                    }
                    return null;
                }
            }
        }
 public ActionResult Edit(int id, Task updatedTask)
 {
     m_tasks.UpdateTask(id, updatedTask);
     return RedirectToAction("GetAll");
 }
 public ActionResult Create(Task task)
 {
     m_tasks.AddTask(task);
     return RedirectToAction("GetAll");
 }
 // POST api/task
 public int Post(Task task)
 {
     var id = m_task.AddTask(task);
     return id;
 }
 private static Task ReadTaskFromRecord(IDataRecord record)
 {
     var task = new Task();
     task.TaskId = record.GetInt32(0);
     task.Title = record.GetString(1);
     task.Description = record.GetString(2);
     task.Completed = record.GetBoolean(3);
     return task;
 }
 public void UpdateTask(int id, Task task)
 {
     throw new NotImplementedException();
 }
 public ActionResult Create(Task task)
 {
     task.TaskId = ++s_counter;
     s_tasks.Add(task);
     return RedirectToAction("GetAll");
 }