﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataBaseLayer
{
    public class GridBookModel
    {
        public int BookId { get; set; }
        public string BookName { get; set; }
        public IEnumerable<GridAuthorModel> Authors
        {
            get 
            {
                return GridAuthorDataAccess.getAuthors(this.BookId);
            }
        }
    }

    public class GridBookDataAccess 
    {
        public static IEnumerable<GridBookModel> getBooks() 
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand(StoredProceduresConst.spBooks, conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;
                using (var books = cmd.ExecuteReader())
                {
                    if (books != null)
                    {
                        GridBookModel b;
                        List<GridBookModel> bookList = new List<GridBookModel>();

                        while (books.Read())
                        {
                            b = new GridBookModel();
                            b.BookId = (int)books["BookId"];
                            b.BookName = (string)books["BookName"];
                            bookList.Add(b);
                        }
                        return bookList;
                    }
                }
                return null;
            }
        }        
    }
}
