239 lines
7.2 KiB
C#
239 lines
7.2 KiB
C#
using CatherineLynwood.Helpers;
|
|
using CatherineLynwood.Models;
|
|
using CatherineLynwood.Services;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using SendGrid.Helpers.Mail;
|
|
|
|
namespace CatherineLynwood.Controllers
|
|
{
|
|
[Route("the-alpha-flame")]
|
|
public class TheAlphaFlameController : Controller
|
|
{
|
|
#region Private Fields
|
|
|
|
private DataAccess _dataAccess;
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public TheAlphaFlameController(DataAccess dataAccess)
|
|
{
|
|
_dataAccess = dataAccess;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Methods
|
|
|
|
[Route("characters/beth-fletcher")]
|
|
public IActionResult Beth()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
|
|
[Route("blog")]
|
|
public async Task<IActionResult> Blog(BlogFilter blogFilter)
|
|
{
|
|
// Convert the Categories list to a comma-separated string for querying
|
|
string categoryIDs = blogFilter.Categories != null ? string.Join(",", blogFilter.Categories) : string.Empty;
|
|
|
|
// Retrieve the blogs filtered by categories
|
|
BlogIndex blogIndex = await _dataAccess.GetBlogsAsync(categoryIDs);
|
|
blogIndex.BlogFilter = blogFilter;
|
|
blogIndex.BlogFilter.TotalPages = (int)Math.Ceiling((double)blogIndex.Blogs.Count / blogFilter.ResultsPerPage);
|
|
blogIndex.IsMobile = IsMobile(Request);
|
|
|
|
if (blogFilter.TotalPages != blogFilter.PreviousTotalPages)
|
|
{
|
|
blogFilter.PageNumber = 1;
|
|
}
|
|
|
|
// Determine sorting direction: 1 = newest first, 2 = oldest first
|
|
if (blogFilter.SortDirection == 1)
|
|
{
|
|
// Sort by newest first
|
|
blogIndex.Blogs = blogIndex.Blogs.OrderByDescending(b => b.PublishDate).ToList();
|
|
}
|
|
else if (blogFilter.SortDirection == 2)
|
|
{
|
|
// Sort by oldest first
|
|
blogIndex.Blogs = blogIndex.Blogs.OrderBy(b => b.PublishDate).ToList();
|
|
}
|
|
|
|
// Paginate the results based on ResultsPerPage
|
|
int resultsPerPage = blogFilter.ResultsPerPage > 0 ? blogFilter.ResultsPerPage : 10; // Default to 10 if not specified
|
|
|
|
// Calculate the items for the current page
|
|
blogIndex.Blogs = blogIndex.Blogs
|
|
.Skip((blogFilter.PageNumber - 1) * resultsPerPage)
|
|
.Take(resultsPerPage)
|
|
.ToList();
|
|
|
|
// Show advanced options if any category filters are applied
|
|
blogIndex.ShowAdvanced = !string.IsNullOrEmpty(categoryIDs);
|
|
|
|
return View(blogIndex);
|
|
}
|
|
|
|
[Route("blog/{slug}")]
|
|
public async Task<IActionResult> BlogItem(string slug, bool showThanks)
|
|
{
|
|
Blog blog = await _dataAccess.GetBlogItemAsync(slug);
|
|
|
|
if (blog.Title == null)
|
|
{
|
|
return RedirectPermanent("/the-alpha-flame/blog");
|
|
}
|
|
|
|
blog.ShowThanks = showThanks;
|
|
|
|
if (blog.Template == "slideshow")
|
|
{
|
|
return View("SlideShowTemplate", blog);
|
|
}
|
|
|
|
return View("DefaultTemplate", blog);
|
|
}
|
|
|
|
|
|
|
|
[Route("characters")]
|
|
public IActionResult Characters()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("comment")]
|
|
public async Task<IActionResult> Comment(BlogComment blogComment, string blogUrl)
|
|
{
|
|
bool showThanks = false;
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
bool visible = await _dataAccess.AddBlogCommentAsync(blogComment);
|
|
|
|
if (!visible)
|
|
{
|
|
var to = new EmailAddress(blogComment.EmailAddress, blogComment.Name);
|
|
var subject = "Thank you from Catherine Lynwood Web Site";
|
|
var plainTextContent = $"Dear {blogComment.Name},/r/nThank you for taking the time to comment on my blog post. ";
|
|
var htmlContent = $"Dear {blogComment.Name},<br>" +
|
|
$"<p>Thank you for taking the time to comment on my recent blog post.</p>" +
|
|
$"<p>This email is to let you know that all comments are reviewed before appearing on the blog page on which they were made. " +
|
|
$"This is unfortuantely necessary to avoid spam and possible offensive remarks. " +
|
|
$"I will endevour to review your comment as soon as possible. " +
|
|
$"Please check back soon.</p>" +
|
|
$"<p>Catherine Lynwood<br>" +
|
|
$"Author: The Alpha Flame<br>" +
|
|
@$"Web: <a href=""https://www.catherinelynwood.com"">www.catherinelynwood.com</a></p>";
|
|
await SendEmail.Execute(to, subject, plainTextContent, htmlContent);
|
|
}
|
|
|
|
showThanks = true;
|
|
}
|
|
|
|
PreFillContact blogPreFill = new PreFillContact
|
|
{
|
|
Name = blogComment.Name,
|
|
EmailAddress = blogComment.EmailAddress,
|
|
Age = blogComment.Age,
|
|
Sex = blogComment.Sex
|
|
};
|
|
|
|
string json = JsonConvert.SerializeObject(blogPreFill);
|
|
|
|
CookieOptions cookieOptions = new CookieOptions
|
|
{
|
|
Expires = DateTime.Now.AddYears(1)
|
|
};
|
|
|
|
Response.Cookies.Append("BlogPreFill", json, cookieOptions);
|
|
|
|
return RedirectToAction("BlogItem", new { slug = blogUrl, showThanks = showThanks });
|
|
}
|
|
|
|
[Route("")]
|
|
public IActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/maggie-grant")]
|
|
public IActionResult Maggie()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/rebecca-jones")]
|
|
public IActionResult Rebecca()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("reckoning")]
|
|
public IActionResult Reckoning()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("redemption")]
|
|
public IActionResult Redemption()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/rick")]
|
|
public IActionResult Rick()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/rob-jackson")]
|
|
public IActionResult Rob()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/rosie-macdonald")]
|
|
public IActionResult Rosie()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/sophie-jones")]
|
|
public IActionResult Sophie()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
[Route("characters/zoe-davies")]
|
|
public IActionResult Zoe()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion Public Methods
|
|
|
|
#region Private Methods
|
|
|
|
private bool IsMobile(HttpRequest request)
|
|
{
|
|
string userAgent = request.Headers["User-Agent"].ToString().ToLower();
|
|
return userAgent.Contains("mobi") ||
|
|
userAgent.Contains("android") ||
|
|
userAgent.Contains("iphone") ||
|
|
userAgent.Contains("ipad");
|
|
}
|
|
|
|
#endregion Private Methods
|
|
}
|
|
} |