using System.Collections.Generic;
using System.Data.SqlClient;
using Baza_Reklam.Classes.Model;

namespace Baza_Reklam.Classes.Repositories
{
    public class NavigationSubColumnRepository : Repository<NavigationSubColumn>
    {
        public NavigationSubColumnRepository(string connectionString)
            : base(connectionString)
        {
        }

        public List<NavigationSubColumn> FindAllActiveByNavigationColumn(NavigationColumn navigationColumn)
        {
            string query = "SELECT Id, Name FROM Rozdzial WHERE  Active=1 AND DzialId={0}";
            query = string.Format(query, navigationColumn.Id);

            List<NavigationSubColumn> navigationSubColumns = new List<NavigationSubColumn>();

            try
            {
                _connection.Open();
                _command = new SqlCommand(query, _connection);
                _reader = _command.ExecuteReader();
                if (_reader != null)
                {
                    while (_reader.Read())
                    {
                        NavigationSubColumn navigationSubColumn = new NavigationSubColumn(_reader.GetInt32(0), _reader.GetString(1));
                        navigationSubColumn.NavigationColumn = navigationColumn;

                        navigationSubColumns.Add(navigationSubColumn);
                    }
                }
            }
            finally
            {
                CleanUp();

            }
            return navigationSubColumns;
        }
    }
}
