using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CG67.FicheCollege.Domaine;
using CG67.FicheCollege.Interface;

namespace CG67.FicheCollege.Entrepot
{
    public class EntrepotRestaurationExterne : EntrepotBase, IEntrepotRestaurationExterne
    {
        public IList<RestaurationExterne> GetByCodeRNEAndAnnee(string codeRNE, int annee)
        {
            IList<RestaurationExterne> resultat = new List<RestaurationExterne>();
            using (SqlConnection connexion = new SqlConnection(this.ChaineDeConnexion))
            {
                try
                {
                    connexion.Open();
                    using (SqlCommand command = connexion.CreateCommand())
                    {
                        command.CommandText = "SELECT Etablissement.NomCollege, RestaurationExterne.NbEleves, RestaurationExterne.TypeAide FROM RestaurationExterne INNER JOIN Etablissement ON (Etablissement.CodeRNE = RestaurationExterne.CodeRNEaidant) WHERE RestaurationExterne.CodeRNEAide = @RNE AND RestaurationExterne.Annee = @Annee";
                        command.Parameters.AddWithValue("@RNE", codeRNE);
                        command.Parameters.AddWithValue("@Annee", annee);
                        using (SqlDataReader dr = command.ExecuteReader())
                        {
                            while (dr.Read())
                            {
                                resultat.Add(new RestaurationExterne(dr["NomCollege"].ToString(), Convert.ToInt16(dr["NbEleves"].ToString()), dr["TypeAide"].ToString()));
                            }
                        }
                    }
                }
                catch (Exception erreurInterne)
                {
                    throw new Exception(" " + erreurInterne);
                }
                finally
                {
                    if (connexion.State == ConnectionState.Open)
                        connexion.Close();
                }
            }
            return resultat;
        }
    }
}
