using MyAppSpApi.DataRepository.Helper;
using System.Data;
using System.Data.SqlClient;
 
namespace MyAppSpApi.StoredProcedure
{
    /// <summary>
    /// Use when running the dbo.CustOrderHist stored procedure
    /// </summary>
    public class CustOrderHist
    {
        private const CommandType _commandType = CommandType.StoredProcedure;
        private readonly string _connectionString;
 
        /// <summary>
        /// constructor
        /// </summary>
        public CustOrderHist()
        {
        }
 
        /// <summary>
        /// constructor
        /// </summary>
        public CustOrderHist (string connectionString)
        {
            _connectionString = connectionString;
        }
 
        /// <summary>
        /// Gets or Sets ProductName
        /// </summary>
        public string ProductName { getset; } 
 
        /// <summary>
        /// Gets or Sets Total
        /// </summary>
        public int? Total { getset; } 
 
        /// <summary>
        /// Runs the stored procedure dbo.CustOrderHist
        /// </summary>
        public async Task<List<CustOrderHist>> Run(string CustomerID) 
        {
            List<SqlParametersqlParamList = new();
            List<CustOrderHistobjCustOrderHistList = null;
 
            // add stored procedure parameters
            DatabaseFunctions.AddSqlParameter(sqlParamList"@CustomerID"CustomerID);
 
            // get the data
            DataTable dt = await DatabaseFunctions.GetDataTableAsync(_connectionString, "[dbo].[CustOrderHist]"sqlParamList, _commandType);
 
            // add items to the list
            if (dt != null && dt.Rows.Count > 0)
            {
                objCustOrderHistList = new List<CustOrderHist>();
 
                foreach (DataRow dr in dt.Rows)
                {
                    // instantiate the class
                    CustOrderHist objCustOrderHist = new();
 
                    // assign values
                    if (dr["ProductName"] != System.DBNull.Value)
                        objCustOrderHist.ProductName = (string)dr["ProductName"];
                    else
                        objCustOrderHist.ProductName = null;
 
                    if (dr["Total"] != System.DBNull.Value)
                        objCustOrderHist.Total = (int)dr["Total"];
                    else
                        objCustOrderHist.Total = null;
 
                    // add item to the list
                    objCustOrderHistList.Add(objCustOrderHist);
                }
            }
 
            return objCustOrderHistList;
        }
    }
}