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

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

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

        public override List<AdLocation> FindAll()
        {
            const string query = "SELECT Id, Text FROM dbo.ReklamaLokalizacja ORDER BY Id";
            List<AdLocation> adPlacements = new List<AdLocation>();

            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())
                        adPlacements.Add(new AdLocation(reader.GetInt32(0), reader.GetString(1)));
                }
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                    reader.Dispose();
                }
                if (cmd != null) cmd.Dispose();
                if (conn != null)
                {
                    conn.Close();
                    conn.Dispose();
                }

            }
            return adPlacements;
        }
    }
}