using System;
using System.Data;
using System.Data.Common;
using SiberTek.Xenta.Data.Entities;
using SiberTek.Xenta.Data.Entities.Collections;
using SiberTek.Xenta.Data.Utils;
using SiberTek.Xenta.Enums;
namespace SiberTek.Xenta.Data.Providers
{
public class PollAnswerDataProvider : DataProviderBase, IPollAnswerDataProvider
{
#region Methods
public bool InsertPollAnswer(int pollID, string title, int count, int displayOrder, DateTime createdOn, DateTime updatedOn, out int answerID)
{
bool res = false;
answerID = 0;
using(DbCommand cmd = DataBase.GetStoredProcCommand("PollAnswers_Insert"))
{
DbCommandHelper.AddInt32(DataBase, cmd, "PollID", pollID);
DbCommandHelper.AddString(DataBase, cmd, "Title", title);
DbCommandHelper.AddInt32(DataBase, cmd, "Count", count);
DbCommandHelper.AddInt32(DataBase, cmd, "DisplayOrder", displayOrder);
DbCommandHelper.AddOutInt32(DataBase, cmd, "AnswerID");
DbCommandHelper.AddDateTime(DataBase, cmd, "CreatedOn", createdOn);
DbCommandHelper.AddDateTime(DataBase, cmd, "UpdatedOn", updatedOn);
res = (DataBase.ExecuteNonQuery(cmd) > 0);
if(res)
{
answerID = DbCommandHelper.GeInt32(DataBase, cmd, "@AnswerID");
}
}
return res;
}
public PollAnswerData GetPollAnswer(int answerID)
{
PollAnswerData data = null;
if(answerID > 0)
{
using(DbCommand cmd = DataBase.GetStoredProcCommand("PollAnswers_Select"))
{
DbCommandHelper.AddInt32(DataBase, cmd, "AnswerID", answerID);
using(IDataReader reader = DataBase.ExecuteReader(cmd))
{
if(reader.Read())
{
data = ReadData(reader);
}
}
}
}
return data;
}
public PollAnswerDataCollection GetPollAnswersByPollID(int pollID)
{
PollAnswerDataCollection dataCollection = new PollAnswerDataCollection();
if(pollID > 0)
{
using(DbCommand cmd = DataBase.GetStoredProcCommand("PollAnswers_SelectByPollID"))
{
DbCommandHelper.AddInt32(DataBase, cmd, "PollID", pollID);
using(IDataReader reader = DataBase.ExecuteReader(cmd))
{
while(reader.Read())
{
dataCollection.Add(ReadData(reader));
}
}
}
}
return dataCollection;
}
public bool UpdatePollAnswer(int answerID, int pollID, string title, int count, int displayOrder, DateTime createdOn, DateTime updatedOn)
{
bool res = false;
if(answerID > 0)
{
using(DbCommand cmd = DataBase.GetStoredProcCommand("PollAnswers_Update"))
{
DbCommandHelper.AddInt32(DataBase, cmd, "AnswerID", answerID);
DbCommandHelper.AddInt32(DataBase, cmd, "PollID", pollID);
DbCommandHelper.AddString(DataBase, cmd, "Title", title);
DbCommandHelper.AddInt32(DataBase, cmd, "Count", count);
DbCommandHelper.AddInt32(DataBase, cmd, "DisplayOrder", displayOrder);
DbCommandHelper.AddDateTime(DataBase, cmd, "CreatedOn", createdOn);
DbCommandHelper.AddDateTime(DataBase, cmd, "UpdatedOn", updatedOn);
res = (DataBase.ExecuteNonQuery(cmd) > 0);
}
}
return res;
}
public bool DeletePollAnswer(int answerID)
{
bool res = false;
if(answerID > 0)
{
using(DbCommand cmd = DataBase.GetStoredProcCommand("PollAnswers_Delete"))
{
DbCommandHelper.AddInt32(DataBase, cmd, "AnswerID", answerID);
res = (DataBase.ExecuteNonQuery(cmd) > 0);
}
}
return res;
}
#endregion
#region Utilities
private static PollAnswerData ReadData(IDataReader reader)
{
PollAnswerData data = new PollAnswerData();
data.AnswerID = DataReaderHelper.ReadInt32(reader, "AnswerID");
data.PollID = DataReaderHelper.ReadInt32(reader, "PollID");
data.Title = DataReaderHelper.ReadString(reader, "Title");
data.Count = DataReaderHelper.ReadInt32(reader, "Count");
data.DisplayOrder = DataReaderHelper.ReadInt32(reader, "DisplayOrder");
data.CreatedOn = DataReaderHelper.ReadDateTime(reader, "CreatedOn");
data.UpdatedOn = DataReaderHelper.ReadDateTime(reader, "UpdatedOn");
return data;
}
#endregion
}
}