using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

using Baza_Reklam.Classes.Model;

namespace Baza_Reklam.Classes.Repositories
{
    public class SectionRepository : Repository<Section>
    {
        public List<Section> FindAllActiveByTitle(Title title)
        {
            string query = "SELECT Id, Nazwa, Active FROM Grzbiety WHERE tytulid={0} AND Active=1 ORDER BY Id";
            query = string.Format(query, title.Id);

            List<Section> sections = new List<Section>();

            SqlConnection conn = null;
            SqlCommand cmd = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection(ConnString.getConnString().Value);
                conn.Open();
                cmd = new SqlCommand(query, conn);
                reader = cmd.ExecuteReader();
                if (reader != null)
                {
                    while (reader.Read())
                    {
                        sections.Add(new Section(reader.GetInt32(0), title, reader.GetString(1)));
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return sections;
        }
    }
}