406 lines
16 KiB
C#
406 lines
16 KiB
C#
using System.Data;
|
|
|
|
using CatherineLynwood.Models;
|
|
|
|
using Microsoft.Data.SqlClient;
|
|
|
|
namespace CatherineLynwood.Services
|
|
{
|
|
public class DataAccess
|
|
{
|
|
#region Private Fields
|
|
|
|
private readonly string _connectionString;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public DataAccess(string connectionString)
|
|
{
|
|
_connectionString = connectionString;
|
|
}
|
|
|
|
public async Task<Questions> GetQuestionsAsync()
|
|
{
|
|
Questions questions = new Questions();
|
|
|
|
using (SqlConnection conn = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
try
|
|
{
|
|
await conn.OpenAsync();
|
|
cmd.Connection = conn;
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "GetQuestions";
|
|
|
|
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
|
|
{
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
questions.AskedQuestions.Add(new Question
|
|
{
|
|
Age = GetDataInt(rdr, "Age"),
|
|
EmailAddress = GetDataString(rdr, "EmailAddress"),
|
|
Name = GetDataString(rdr, "Name"),
|
|
Text = GetDataString(rdr, "Question"),
|
|
QuestionDate = GetDataDate(rdr, "QuestionDate"),
|
|
Sex = GetDataString(rdr, "Sex")
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return questions;
|
|
}
|
|
|
|
public async Task<BlogIndex> GetBlogsAsync(string categoryIDs)
|
|
{
|
|
BlogIndex blogIndex = new BlogIndex();
|
|
|
|
using (SqlConnection conn = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
try
|
|
{
|
|
await conn.OpenAsync();
|
|
cmd.Connection = conn;
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "GetBlog";
|
|
cmd.Parameters.AddWithValue("@CategoryIDs", categoryIDs);
|
|
|
|
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
|
|
{
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
blogIndex.BlogCategories.Add(new BlogCategory
|
|
{
|
|
CategoryID = GetDataInt(rdr, "CategoryID"),
|
|
Category = GetDataString(rdr, "Category"),
|
|
Selected = GetDataBool(rdr, "Selected")
|
|
});
|
|
}
|
|
|
|
await rdr.NextResultAsync();
|
|
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
blogIndex.Blogs.Add(new Blog
|
|
{
|
|
BlogID = GetDataInt(rdr, "BlogID"),
|
|
BlogUrl = GetDataString(rdr, "BlogUrl"),
|
|
Title = GetDataString(rdr, "Title"),
|
|
SubTitle = GetDataString(rdr, "SubTitle"),
|
|
PublishDate = GetDataDate(rdr, "PublishDate"),
|
|
Likes = GetDataInt(rdr, "Likes"),
|
|
IndexText = GetDataString(rdr, "IndexText"),
|
|
ImageUrl = GetDataString(rdr, "ImageUrl"),
|
|
ImageAlt = GetDataString(rdr, "ImageAlt"),
|
|
ImageDescription = GetDataString(rdr, "ImageDescription")
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return blogIndex;
|
|
}
|
|
|
|
public async Task<Blog> GetBlogItemAsync(string blogUrl)
|
|
{
|
|
Blog blog = new Blog();
|
|
|
|
using (SqlConnection conn = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
try
|
|
{
|
|
await conn.OpenAsync();
|
|
cmd.Connection = conn;
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "GetBlogItem";
|
|
cmd.Parameters.AddWithValue("@BlogUrl", blogUrl);
|
|
|
|
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
|
|
{
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
blog.AudioTeaserText = GetDataString(rdr, "AudioTeaserText");
|
|
blog.AudioTeaserUrl = GetDataString(rdr, "AudioTeaserUrl");
|
|
blog.AudioTranscriptUrl = GetDataString(rdr, "AudioTranscriptUrl");
|
|
blog.BlogID = GetDataInt(rdr, "BlogID");
|
|
blog.BlogUrl = GetDataString(rdr, "BlogUrl");
|
|
blog.ContentBottom = GetDataString(rdr, "ContentBottom");
|
|
blog.ContentTop = GetDataString(rdr, "ContentTop");
|
|
blog.ImageAlt = GetDataString(rdr, "ImageAlt");
|
|
blog.ImageDescription = GetDataString(rdr, "ImageDescription");
|
|
blog.ImageFirst = GetDataBool(rdr, "ImageFirst");
|
|
blog.ImageUrl = GetDataString(rdr, "ImageUrl");
|
|
blog.IndexText = GetDataString(rdr, "IndexText");
|
|
blog.Likes = GetDataInt(rdr, "Likes");
|
|
blog.PublishDate = GetDataDate(rdr, "PublishDate");
|
|
blog.SubTitle = GetDataString(rdr, "SubTitle");
|
|
blog.Template = GetDataString(rdr, "Template");
|
|
blog.Title = GetDataString(rdr, "Title");
|
|
}
|
|
|
|
await rdr.NextResultAsync();
|
|
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
blog.BlogImages.Add(new BlogImage
|
|
{
|
|
ImageID = GetDataInt(rdr, "ImageID"),
|
|
BlogID = GetDataInt(rdr, "BlogID"),
|
|
ImageUrl = GetDataString(rdr, "ImageUrl"),
|
|
ImageCaption = GetDataString(rdr, "ImageCaption"),
|
|
ImageText = GetDataString(rdr, "ImageText")
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return blog;
|
|
}
|
|
|
|
public async Task<BlogComments> GetBlogCommentsAsync(int blogID)
|
|
{
|
|
BlogComments blogComments = new BlogComments();
|
|
|
|
using (SqlConnection conn = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
try
|
|
{
|
|
await conn.OpenAsync();
|
|
cmd.Connection = conn;
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "GetBlogComments";
|
|
cmd.Parameters.AddWithValue("@BlogID", blogID);
|
|
|
|
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
|
|
{
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
blogComments.BlogID = GetDataInt(rdr, "BlogID");
|
|
blogComments.BlogUrl = GetDataString(rdr, "BlogUrl");
|
|
}
|
|
|
|
await rdr.NextResultAsync();
|
|
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
blogComments.ExistingComments.Add(new BlogCommentExisting
|
|
{
|
|
BlogID = GetDataInt(rdr, "BlogID"),
|
|
Comment = GetDataString(rdr, "Comment"),
|
|
CommentDate = GetDataDate(rdr, "CommentDate"),
|
|
EmailAddress = GetDataString(rdr, "EmailAddress"),
|
|
Name = GetDataString(rdr, "Name"),
|
|
Reply = GetDataString(rdr, "Reply"),
|
|
ResponderName = GetDataString(rdr, "ResponderName"),
|
|
ImageUrl = GetDataString(rdr, "ImageUrl")
|
|
});
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return blogComments;
|
|
}
|
|
|
|
public async Task<bool> AddBlogCommentAsync(BlogComment blogComment)
|
|
{
|
|
bool visible = false;
|
|
|
|
using (SqlConnection conn = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
try
|
|
{
|
|
await conn.OpenAsync();
|
|
cmd.Connection = conn;
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "SaveBlogComment";
|
|
cmd.Parameters.AddWithValue("@BlogID", blogComment.BlogID);
|
|
cmd.Parameters.AddWithValue("@Comment", blogComment.Comment);
|
|
cmd.Parameters.AddWithValue("@EmailAddress", blogComment.EmailAddress);
|
|
cmd.Parameters.AddWithValue("@Name", blogComment.Name);
|
|
cmd.Parameters.AddWithValue("@Sex", blogComment.Sex);
|
|
cmd.Parameters.AddWithValue("@Age", blogComment.Age);
|
|
|
|
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
|
|
{
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
visible = GetDataBool(rdr, "Visible");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return visible;
|
|
}
|
|
|
|
public async Task<bool> AddQuestionAsync(Question question)
|
|
{
|
|
bool visible = false;
|
|
|
|
using (SqlConnection conn = new SqlConnection(_connectionString))
|
|
{
|
|
using (SqlCommand cmd = new SqlCommand())
|
|
{
|
|
try
|
|
{
|
|
await conn.OpenAsync();
|
|
cmd.Connection = conn;
|
|
cmd.CommandType = CommandType.StoredProcedure;
|
|
cmd.CommandText = "SaveQuestion";
|
|
cmd.Parameters.AddWithValue("@Question", question.Text);
|
|
cmd.Parameters.AddWithValue("@EmailAddress", question.EmailAddress);
|
|
cmd.Parameters.AddWithValue("@Name", question.Name);
|
|
cmd.Parameters.AddWithValue("@Sex", question.Sex);
|
|
cmd.Parameters.AddWithValue("@Age", question.Age);
|
|
|
|
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
|
|
{
|
|
while (await rdr.ReadAsync())
|
|
{
|
|
visible = GetDataBool(rdr, "Visible");
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
return visible;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Protected Methods
|
|
|
|
protected bool GetDataBool(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? false : rdr.GetBoolean(colIndex);
|
|
}
|
|
|
|
protected byte[] GetDataBytes(SqlDataReader rdr, string field)
|
|
{
|
|
int colindex = rdr.GetOrdinal(field);
|
|
|
|
if (rdr.IsDBNull(colindex))
|
|
{
|
|
MemoryStream rs = new MemoryStream();
|
|
return rs.ToArray();
|
|
}
|
|
|
|
int bufferSize = 100; // Size of the BLOB buffer.
|
|
byte[] outbyte = new byte[bufferSize];
|
|
long startIndex = 0;
|
|
|
|
MemoryStream ms = new MemoryStream();
|
|
BinaryWriter bw = new BinaryWriter(ms);
|
|
long retval = rdr.GetBytes(colindex, startIndex, outbyte, 0, bufferSize);
|
|
|
|
while (retval == bufferSize)
|
|
{
|
|
bw.Write(outbyte);
|
|
bw.Flush();
|
|
|
|
// Reposition the start index to the end of the last buffer and fill the buffer.
|
|
startIndex += bufferSize;
|
|
retval = rdr.GetBytes(colindex, startIndex, outbyte, 0, bufferSize);
|
|
}
|
|
|
|
bw.Write(outbyte, 0, (int)retval);
|
|
bw.Flush();
|
|
bw.Close();
|
|
|
|
return ms.ToArray();
|
|
}
|
|
|
|
protected DateTime GetDataDate(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? new DateTime() : rdr.GetDateTime(colIndex);
|
|
}
|
|
|
|
protected Decimal GetDataDecimal(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? 0 : rdr.GetDecimal(colIndex);
|
|
}
|
|
|
|
protected Int32 GetDataInt(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? 0 : rdr.GetInt32(colIndex);
|
|
}
|
|
|
|
protected long GetDataLong(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? 0 : rdr.GetInt64(colIndex);
|
|
}
|
|
|
|
protected string GetDataString(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? string.Empty : rdr.GetString(colIndex);
|
|
}
|
|
|
|
protected TimeSpan GetDataTimeSpan(SqlDataReader rdr, string field)
|
|
{
|
|
int colIndex = rdr.GetOrdinal(field);
|
|
|
|
return rdr.IsDBNull(colIndex) ? new TimeSpan() : rdr.GetTimeSpan(colIndex);
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
}
|
|
} |