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

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

        public override List<SubscriptionType> FindAll()
        {
            const string query = "SELECT Id, TitleId, Name, PricePln, PriceEur, AmtMounth FROM SubscriptionType";

            List<SubscriptionType> subscriptionTypes = new List<SubscriptionType>();

            try
            {
                _connection.Open();
                _command = new SqlCommand(query, _connection);

                _reader = _command.ExecuteReader();
                if (_reader != null)
                {
                    while (_reader.Read())
                    {  
                        subscriptionTypes.Add(new SubscriptionType(_reader.GetInt32(0), 
                                                                   _reader.GetInt32(1),
                                                                   _reader.GetString(2),
                                                                   _reader.GetDecimal(3),
                                                                   _reader.GetDecimal(4),
                                                                   _reader.GetInt32(5)));
                    }
                }
            }
            finally
            {
                CleanUp();
            }

            return subscriptionTypes;
        }
    }
}