127 lines
3.9 KiB
C#
127 lines
3.9 KiB
C#
using CatherineLynwood.Models;
|
||
using CatherineLynwood.Services;
|
||
|
||
using Microsoft.AspNetCore.Mvc;
|
||
|
||
namespace CatherineLynwood.Controllers
|
||
{
|
||
[Route("access")]
|
||
public class AccessController : Controller
|
||
{
|
||
#region Private Fields
|
||
|
||
private readonly IAccessCodeService _accessCodeService;
|
||
private readonly ILogger<HomeController> _logger;
|
||
private DataAccess _dataAccess;
|
||
|
||
#endregion Private Fields
|
||
|
||
#region Public Constructors
|
||
|
||
public AccessController(ILogger<HomeController> logger, DataAccess dataAccess, IAccessCodeService accessCodeService)
|
||
{
|
||
_logger = logger;
|
||
_dataAccess = dataAccess;
|
||
_accessCodeService = accessCodeService;
|
||
}
|
||
|
||
#endregion Public Constructors
|
||
|
||
#region Public Methods
|
||
|
||
[HttpGet("mailinglist")]
|
||
public IActionResult MailingList()
|
||
{
|
||
return View();
|
||
}
|
||
|
||
[HttpPost("mailinglist")]
|
||
public async Task<IActionResult> MailingList(Marketing marketing)
|
||
{
|
||
bool success = true;
|
||
|
||
if (!string.IsNullOrEmpty(marketing.Email))
|
||
{
|
||
success = await _dataAccess.AddMarketingAsync(marketing);
|
||
}
|
||
|
||
if (success)
|
||
{
|
||
// Set cookie so we don’t ask again
|
||
Response.Cookies.Append("MailingListPrompted", "true", new CookieOptions
|
||
{
|
||
Expires = DateTimeOffset.UtcNow.AddYears(5),
|
||
IsEssential = true
|
||
});
|
||
|
||
// Redirect back to original destination
|
||
var returnUrl = TempData["ReturnUrl"] as string ?? "/extras";
|
||
return Redirect(returnUrl);
|
||
}
|
||
else
|
||
{
|
||
return RedirectToAction("Prompt");
|
||
}
|
||
}
|
||
|
||
[HttpPost("decline")]
|
||
public IActionResult Decline()
|
||
{
|
||
// Set cookie so we don’t ask again
|
||
Response.Cookies.Append("MailingListPrompted", "true", new CookieOptions
|
||
{
|
||
Expires = DateTimeOffset.UtcNow.AddYears(5),
|
||
IsEssential = true
|
||
});
|
||
|
||
// Redirect back to original destination
|
||
var returnUrl = TempData["ReturnUrl"] as string ?? "/extras";
|
||
return Redirect(returnUrl);
|
||
}
|
||
|
||
[HttpGet("prompt")]
|
||
public async Task<IActionResult> Prompt(string returnUrl = "/extras")
|
||
{
|
||
var (pageNumber, wordIndex) = await _accessCodeService.GetCurrentChallengeAsync();
|
||
|
||
ViewBag.PageNumber = pageNumber;
|
||
ViewBag.WordIndex = wordIndex;
|
||
ViewBag.ReturnUrl = returnUrl;
|
||
|
||
return View();
|
||
}
|
||
|
||
[HttpPost("prompt")]
|
||
public async Task<IActionResult> Prompt(string userWord, string returnUrl = "/extras")
|
||
{
|
||
var (accessLevel, highestBook) = await _accessCodeService.ValidateWordAsync(userWord);
|
||
|
||
if (accessLevel > 0 && highestBook > 0)
|
||
{
|
||
HttpContext.Session.SetInt32("BookAccessLevel", accessLevel);
|
||
HttpContext.Session.SetInt32("BookAccessMax", highestBook);
|
||
|
||
var promptedCookie = Request.Cookies["MailingListPrompted"];
|
||
if (string.IsNullOrEmpty(promptedCookie))
|
||
{
|
||
TempData["ReturnUrl"] = returnUrl;
|
||
return RedirectToAction("MailingList");
|
||
}
|
||
|
||
return Redirect(returnUrl);
|
||
}
|
||
|
||
var (pageNumber, wordIndex) = await _accessCodeService.GetCurrentChallengeAsync();
|
||
ViewBag.PageNumber = pageNumber;
|
||
ViewBag.WordIndex = wordIndex;
|
||
ViewBag.ReturnUrl = returnUrl;
|
||
ViewBag.Error = "Invalid word. Please try again.";
|
||
|
||
return View();
|
||
}
|
||
|
||
#endregion Public Methods
|
||
}
|
||
}
|
||
|