public ShoppingList Create(ShoppingList source)
        {
            User me = CurrentUser.retrieve ();
            ShoppingList actual = null;

            SqlConnection connection = ConnectionFactory.GetConnection ();
            SqlCommand command = new SqlCommand (QUERY_CREATE, connection);

            command.Parameters.AddWithValue ("@UserId", me.Id);
            command.Parameters.AddWithValue ("@Date", DateTime.Now);
            command.Parameters.AddWithValue ("@Name", source.Name);
            command.Parameters.AddWithValue ("@Notes", source.Notes == null ? "": source.Notes);
            command.Parameters.AddWithValue ("@Done", false);

            try
            {
                connection.Open ();
                int id = (int) command.ExecuteScalar ();
                actual = GetById (id);
            }
            finally
            {
                connection.Close ();
            }
            return actual;
        }
 public List<ShoppingListItem> GetByShoppingList(ShoppingList list)
 {
     return GetByShoppingList (list.Id);
 }
        private ShoppingList ReadShoppingList(SqlDataReader reader)
        {
            ShoppingList list = new ShoppingList ();

            list.Id = (int) reader["Id"];
            list.Name = reader["Name"].ToString ();
            list.Date = (DateTime) reader["Date"];
            list.Notes = reader["Notes"].ToString ();
            list.Done = false;

            return list;
        }
        public ShoppingList Update(int id, ShoppingList source)
        {
            User me = CurrentUser.retrieve ();
            ShoppingList original = GetById (id);

            SqlConnection connection = ConnectionFactory.GetConnection ();
            SqlCommand command = new SqlCommand (QUERY_UPDATE, connection);

            command.Parameters.AddWithValue ("@UserId", me.Id);
            command.Parameters.AddWithValue ("@Date", source.Date == null ? original.Date : source.Date);
            command.Parameters.AddWithValue ("@Name", source.Name == null ? original.Name : source.Name);
            command.Parameters.AddWithValue ("@Notes", source.Notes == null ? original.Notes : source.Notes);

            try
            {
                connection.Open ();
                command.ExecuteScalar ();
            }
            finally
            {
                connection.Close ();
            }

            return GetById (original.Id);
        }