using System.Text;
using Microsoft.AspNetCore.Mvc;
using OliverBooth.Common.Data.Web;
using OliverBooth.Common.Services;
namespace OliverBooth.Controllers;
[Controller]
[Route("contact/blacklist/formatted")]
public class FormattedBlacklistController : Controller
{
private readonly IContactService _contactService;
///
/// Initializes a new instance of the class.
///
/// The .
public FormattedBlacklistController(IContactService contactService)
{
_contactService = contactService;
}
[HttpGet("{format}")]
public IActionResult OnGet([FromRoute] string format)
{
IReadOnlyCollection blacklist = _contactService.GetBlacklist();
switch (format)
{
case "json":
return Json(blacklist);
case "csv":
var builder = new StringBuilder();
builder.AppendLine("EmailAddress,Name,Reason");
foreach (IBlacklistEntry entry in blacklist)
{
builder.AppendLine($"{entry.EmailAddress},{entry.Name},{entry.Reason}");
}
return Content(builder.ToString(), "text/csv", Encoding.UTF8);
}
return NotFound();
}
}