2025-08-11 20:19:49 +01:00

1068 lines
43 KiB
C#

using CatherineLynwood.Models;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Data.SqlClient;
using SixLabors.ImageSharp.Web.Commands.Converters;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
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<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> Addhoneypot(DateTime dateTime, string ip, string country, string userAgent, string referer)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveHoneypot";
cmd.Parameters.AddWithValue("@DateTime", dateTime);
cmd.Parameters.AddWithValue("@IP", ip);
cmd.Parameters.AddWithValue("@Country", country);
cmd.Parameters.AddWithValue("@UserAgent", userAgent);
cmd.Parameters.AddWithValue("@Referer", referer);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
success = false;
}
}
}
return success;
}
public async Task<int> SaveFlagClick(string country)
{
int likes = 0;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveTrailerLike";
cmd.Parameters.AddWithValue("@Country", country);
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
likes = GetDataInt(rdr, "Likes");
}
}
}
catch (Exception ex)
{
}
}
}
return likes;
}
public async Task<FlagSupportViewModel> GetTrailerLikes()
{
FlagSupportViewModel flagSupportViewModel = new FlagSupportViewModel();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetTrailerLikes";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
string country = GetDataString(rdr, "Country");
int likes = GetDataInt(rdr, "Likes");
flagSupportViewModel.FlagCounts.Add(country, likes);
}
}
}
catch (Exception ex)
{
}
}
}
return flagSupportViewModel;
}
public async Task<bool> AddMarketingAsync(Marketing marketing)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveMarketingOptions";
cmd.Parameters.AddWithValue("@Name", marketing.Name);
cmd.Parameters.AddWithValue("@Email", marketing.Email);
cmd.Parameters.AddWithValue("@OptExtras", marketing.optExtras);
cmd.Parameters.AddWithValue("@OptFutureBooks", marketing.optFutureBooks);
cmd.Parameters.AddWithValue("@OptNews", marketing.optNews);
cmd.Parameters.AddWithValue("@Age", marketing.Age);
cmd.Parameters.AddWithValue("@Sex", marketing.Sex);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
success = false;
}
}
}
return success;
}
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;
}
public async Task<bool> EnterGiveawayAsync(Marketing marketing)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveGiveawayEntry";
cmd.Parameters.AddWithValue("@Name", marketing.Name);
cmd.Parameters.AddWithValue("@Email", marketing.Email);
cmd.Parameters.AddWithValue("@OptExtras", false);
cmd.Parameters.AddWithValue("@OptFutureBooks", true);
cmd.Parameters.AddWithValue("@OptNews", true);
cmd.Parameters.AddWithValue("@Age", marketing.Age);
cmd.Parameters.AddWithValue("@Sex", marketing.Sex);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
success = false;
}
}
}
return success;
}
public List<AccessCode> GetAccessCodes()
{
List<AccessCode> accessCodes = new List<AccessCode>();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAccessCodes";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
accessCodes.Add(new AccessCode
{
PageNumber = GetDataInt(rdr, "PageNumber"),
WordIndex = GetDataInt(rdr, "WordIndex"),
ExpectedWord = GetDataString(rdr, "ExpectedWord"),
AccessLevel = GetDataInt(rdr, "AccessLevel"),
BookNumber = GetDataInt(rdr, "BookNumber")
});
}
}
}
catch (Exception ex)
{
}
}
}
return accessCodes;
}
public async Task<List<BlogSummaryResponse>> GetAllBlogsAsync()
{
List<BlogSummaryResponse> list = new List<BlogSummaryResponse>();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllBlogs";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
list.Add(new BlogSummaryResponse
{
AiSummary = GetDataString(rdr, "AiSummary"),
BlogUrl = GetDataString(rdr, "BlogUrl"),
Draft = GetDataBool(rdr, "Draft"),
IndexText = GetDataString(rdr, "IndexText"),
PublishDate = GetDataDate(rdr, "PublishDate"),
SubTitle = GetDataString(rdr, "SubTitle"),
Title = GetDataString(rdr, "Title"),
});
}
}
}
catch (Exception ex)
{
}
}
}
return list;
}
public async Task<ARCReaderList> GetAllARCReadersAsync()
{
ARCReaderList arcReaderList = new ARCReaderList();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllARCReaders";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
arcReaderList.Applications.Add(new ARCReaderApplication
{
HasKindleAccess = GetDataString(rdr, "HasKindleAccess"),
ContentFit = GetDataString(rdr, "ContentFit"),
Email = GetDataString(rdr, "Email"),
ExtraNotes = GetDataString(rdr, "ExtraNotes"),
FullName = GetDataString(rdr, "FullName"),
KindleEmail = GetDataString(rdr, "KindleEmail"),
Platforms = GetDataString(rdr, "Platforms"),
PlatformsOther = GetDataString(rdr, "PlatformsOther"),
PreviewChapters = GetDataString(rdr, "PreviewChapters"),
ReviewCommitment = GetDataString(rdr, "ReviewCommitment"),
ReviewLink = GetDataString(rdr, "ReviewLink"),
SubmittedAt = GetDataDate(rdr, "SubmittedAt"),
});
}
}
}
catch (Exception ex)
{
}
}
}
return arcReaderList;
}
public async Task<BlogAdminIndex> GetBlogAdminIndexAsync()
{
BlogAdminIndex blogAdminIndex = new BlogAdminIndex();
blogAdminIndex.BlogItems = new List<BlogAdminIndexItem>();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetBlogIndex";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
blogAdminIndex.BlogItems.Add(new BlogAdminIndexItem
{
BlogID = GetDataInt(rdr, "BlogID"),
BlogUrl = GetDataString(rdr, "BlogUrl"),
Draft = GetDataBool(rdr, "Draft"),
IndexText = GetDataString(rdr, "IndexText"),
PublishDate = GetDataDate(rdr, "PublishDate"),
SubTitle = GetDataString(rdr, "SubTitle"),
Title = GetDataString(rdr, "Title"),
});
}
}
}
catch (Exception ex)
{
}
}
}
return blogAdminIndex;
}
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<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.AiSummary = GetDataString(rdr, "AiSummary");
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.Draft = GetDataBool(rdr, "Draft");
blog.ImageAlt = GetDataString(rdr, "ImageAlt");
blog.ImageDescription = GetDataString(rdr, "ImageDescription");
blog.ImageFirst = GetDataBool(rdr, "ImageFirst");
blog.ImagePrompt = GetDataString(rdr, "ImagePrompt");
blog.ImageUrl = GetDataString(rdr, "ImageUrl");
blog.Indexed = GetDataBool(rdr, "Indexed");
blog.IndexText = GetDataString(rdr, "IndexText");
blog.Likes = GetDataInt(rdr, "Likes");
blog.PostedBy = GetDataString(rdr, "PostedBy");
blog.PostedImage = GetDataString(rdr, "PostedImage");
blog.PublishDate = GetDataDate(rdr, "PublishDate");
blog.SubTitle = GetDataString(rdr, "SubTitle");
blog.Template = GetDataString(rdr, "Template");
blog.Title = GetDataString(rdr, "Title");
blog.VideoUrl = GetDataString(rdr, "VideoUrl");
blog.ResponderID = GetDataInt(rdr, "ResponderID");
}
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<BlogIndex> GetBlogsAsync()
{
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";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
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")
});
}
await rdr.NextResultAsync();
while (await rdr.ReadAsync())
{
blogIndex.NextBlog.PublishDate = GetDataDate(rdr, "PublishDate");
blogIndex.NextBlog.Title = GetDataString(rdr, "Title");
blogIndex.NextBlog.SubTitle = GetDataString(rdr, "SubTitle");
blogIndex.NextBlog.IndexText = GetDataString(rdr, "IndexText");
blogIndex.NextBlog.ImageAlt = GetDataString(rdr, "ImageAlt");
blogIndex.NextBlog.ImageUrl = GetDataString(rdr, "ImageUrl");
}
}
}
catch (Exception ex)
{
}
}
}
return blogIndex;
}
public async Task<List<BlogPost>> GetDueBlogPostsAsync()
{
List<BlogPost> blogPosts = new List<BlogPost>();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetUnIndexedBlogs";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
blogPosts.Add(new BlogPost
{
BlogID = GetDataInt(rdr, "BlogID"),
BlogUrl = GetDataString(rdr, "BlogUrl")
});
}
}
}
catch (Exception ex)
{
}
}
}
return blogPosts;
}
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 = GetDataString(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<List<SelectListItem>> GetResponderList()
{
List<SelectListItem> selectListItems = new List<SelectListItem>();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetResponderList";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
selectListItems.Add(new SelectListItem
{
Value = GetDataInt(rdr, "ResponderID").ToString(),
Text = GetDataString(rdr, "Name"),
});
}
}
}
catch (Exception ex)
{
}
}
}
return selectListItems;
}
public async Task<Reviews> GetReviewsAsync()
{
Reviews reviews = new Reviews();
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetReviews";
using (SqlDataReader rdr = await cmd.ExecuteReaderAsync())
{
while (await rdr.ReadAsync())
{
reviews.Items.Add(new Review
{
AuthorName = GetDataString(rdr, "AuthorName"),
DatePublished = GetDataDate(rdr, "DatePublished"),
RatingValue = GetDataDouble(rdr, "RatingValue"),
ReviewBody = GetDataString(rdr, "ReviewBody"),
SiteName = GetDataString(rdr, "SiteName"),
URL = GetDataString(rdr, "URL")
});
}
}
}
catch (Exception ex)
{
}
}
}
return reviews;
}
public async Task<bool> MarkAsNotifiedAsync(int blogID)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "MarkBlogAsIndexed";
cmd.Parameters.AddWithValue("@BlogID", blogID);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
success = false;
}
}
}
return success;
}
public async Task<bool> SaveBlogToDatabase(BlogPostRequest blog)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveBlog";
cmd.Parameters.AddWithValue("@AiSummary", blog.AiSummary);
cmd.Parameters.AddWithValue("@BlogUrl", blog.BlogUrl);
cmd.Parameters.AddWithValue("@ContentBottom", blog.ContentBottom);
cmd.Parameters.AddWithValue("@ContentTop", blog.ContentTop);
cmd.Parameters.AddWithValue("@ImageAlt", blog.ImageAlt);
cmd.Parameters.AddWithValue("@ImageDescription", blog.ImageDescription);
cmd.Parameters.AddWithValue("@ImageFirst", blog.ImagePosition.ToLower() == "left");
cmd.Parameters.AddWithValue("@ImagePrompt", blog.ImagePrompt);
cmd.Parameters.AddWithValue("@IndexText", blog.IndexText);
cmd.Parameters.AddWithValue("@PublishDate", blog.PublishDate);
cmd.Parameters.AddWithValue("@ResponderID", blog.ResponderID);
cmd.Parameters.AddWithValue("@SubTitle", blog.SubTitle);
cmd.Parameters.AddWithValue("@Title", blog.Title);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
}
}
}
return success;
}
public async Task<bool> SaveContact(Contact contact, bool subscribe = false)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveContact";
cmd.Parameters.AddWithValue("@Name", contact.Name);
cmd.Parameters.AddWithValue("@EmailAddress", contact.EmailAddress);
cmd.Parameters.AddWithValue("@Subscribe", subscribe);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
success = false;
}
}
}
return success;
}
public async Task<bool> SaveARCReaderApplication(ArcReaderApplicationModel arcReaderApplication)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
await conn.OpenAsync();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "SaveARCReader";
cmd.Parameters.AddWithValue("@FullName", arcReaderApplication.FullName);
cmd.Parameters.AddWithValue("@Email", arcReaderApplication.Email);
cmd.Parameters.AddWithValue("@KindleEmail", string.IsNullOrWhiteSpace(arcReaderApplication.KindleEmail) ? (object)DBNull.Value : arcReaderApplication.KindleEmail);
cmd.Parameters.AddWithValue("@HasKindleAccess", arcReaderApplication.HasKindleAccess);
cmd.Parameters.AddWithValue("@Platforms", arcReaderApplication.Platforms != null ? string.Join(",", arcReaderApplication.Platforms) : (object)DBNull.Value);
cmd.Parameters.AddWithValue("@PreviewChapters", arcReaderApplication.PreviewChapters != null ? string.Join(",", arcReaderApplication.PreviewChapters) : (object)DBNull.Value);
cmd.Parameters.AddWithValue("@PlatformsOther", string.IsNullOrWhiteSpace(arcReaderApplication.PlatformsOther) ? (object)DBNull.Value : arcReaderApplication.PlatformsOther);
cmd.Parameters.AddWithValue("@ReviewLink", string.IsNullOrWhiteSpace(arcReaderApplication.ReviewLink) ? (object)DBNull.Value : arcReaderApplication.ReviewLink);
cmd.Parameters.AddWithValue("@ContentFit", arcReaderApplication.ContentFit ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@ReviewCommitment", arcReaderApplication.ReviewCommitment ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@ExtraNotes", string.IsNullOrWhiteSpace(arcReaderApplication.ExtraNotes) ? (object)DBNull.Value : arcReaderApplication.ExtraNotes);
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
// Optionally log or rethrow
success = false;
}
}
}
return success;
}
public async Task<bool> UpdateBlogAsync(Blog blog)
{
bool success = true;
using (SqlConnection conn = new SqlConnection(_connectionString))
using (SqlCommand cmd = new SqlCommand("UpdateBlog", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
// Required ID for WHERE clause
cmd.Parameters.AddWithValue("@blogID", blog.BlogID);
// Basic text fields
cmd.Parameters.AddWithValue("@blogUrl", blog.BlogUrl ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@publishDate", blog.PublishDate);
cmd.Parameters.AddWithValue("@responderID", blog.ResponderID);
cmd.Parameters.AddWithValue("@title", blog.Title ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@subTitle", (object?)blog.SubTitle ?? DBNull.Value);
cmd.Parameters.AddWithValue("@indexText", blog.IndexText ?? (object)DBNull.Value);
// Audio
cmd.Parameters.AddWithValue("@audioTranscriptUrl", (object?)blog.AudioTranscriptUrl ?? DBNull.Value);
cmd.Parameters.AddWithValue("@audioTeaserUrl", (object?)blog.AudioTeaserUrl ?? DBNull.Value);
cmd.Parameters.AddWithValue("@audioTeaserText", (object?)blog.AudioTeaserText ?? DBNull.Value);
// Numbers / bools
cmd.Parameters.AddWithValue("@likes", blog.Likes);
cmd.Parameters.AddWithValue("@imageFirst", blog.ImageFirst);
cmd.Parameters.AddWithValue("@indexed", blog.Indexed);
cmd.Parameters.AddWithValue("@draft", blog.Draft);
// Content
cmd.Parameters.AddWithValue("@contentTop", blog.ContentTop ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@contentBottom", (object?)blog.ContentBottom ?? DBNull.Value);
// Media
cmd.Parameters.AddWithValue("@videoUrl", (object?)blog.VideoUrl ?? DBNull.Value);
cmd.Parameters.AddWithValue("@imageUrl", (object?)blog.ImageUrl ?? DBNull.Value);
cmd.Parameters.AddWithValue("@imageAlt", (object?)blog.ImageAlt ?? DBNull.Value);
cmd.Parameters.AddWithValue("@imageDescription", (object?)blog.ImageDescription ?? DBNull.Value);
// Template / AI
cmd.Parameters.AddWithValue("@template", blog.Template ?? (object)DBNull.Value);
cmd.Parameters.AddWithValue("@aiSummary", (object?)blog.AiSummary ?? DBNull.Value);
cmd.Parameters.AddWithValue("@imagePrompt", (object?)blog.ImagePrompt ?? DBNull.Value);
try
{
await conn.OpenAsync();
await cmd.ExecuteNonQueryAsync();
}
catch (Exception ex)
{
success = false;
// Optionally log ex
}
}
return success;
}
#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 Double GetDataDouble(SqlDataReader rdr, string field)
{
int colIndex = rdr.GetOrdinal(field);
return rdr.IsDBNull(colIndex) ? 0 : rdr.GetDouble(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
}
}