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

namespace Baza_Reklam.Classes.Repositories
{
    public class NavigationSubColumnRepository : Repository<NavigationSubColumn>
    {
        private readonly string _connectionString;

        public NavigationSubColumnRepository(string connectionString)
        {
            _connectionString = 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>();

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(_connectionString);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        NavigationSubColumn navigationSubColumn = new NavigationSubColumn(reader.GetInt32(0), reader.GetString(1));
                        navigationSubColumn.NavigationColumn = navigationColumn;

                        navigationSubColumns.Add(navigationSubColumn);
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return navigationSubColumns;
        }
    }
}
