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

using Baza_Reklam.Classes.Model;

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

        public TitleRepository(string connectionString)
        {
            _connectionString = connectionString;
        }

        public List<Title> FindAllActive()
        {
            const string query = "select Id, Symb, SYMB2, Max_Rabat, [Tyt Full Name] from [lista tytułów] where active=1 ORDER BY Id";

            List<Title> titles = new List<Title>();

            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())
                    {
                        titles.Add(new Title(reader.GetInt32(0), reader.GetString(1), reader.GetString(2).Trim(), reader.GetDouble(3)));
                    }
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return titles;
        }
    }
}