using Data; using Microsoft.AspNetCore.Mvc; using Microsoft.CodeAnalysis; using Microsoft.EntityFrameworkCore; using Microsoft.VisualStudio.TextTemplating; using Microsoft.VisualStudio.Web.CodeGenerators.Mvc.Templates.BlazorIdentity.Pages.Manage; using OpenAI_API; using Services.EmailSend; using Services.Implemnetation; using Services.Interaces; using Web.AIConfiguration; using Web.ViewModel.NewsLetterVM; namespace Web.Areas.Admin.Controllers { public class NewslettersController : Controller { private readonly INewsLetterRepository _repository; private readonly SurveyContext _context; private readonly IEmailServices _emailServices; private readonly IConfiguration _configuration; public NewslettersController(INewsLetterRepository repository,SurveyContext context,IEmailServices emailServices,IConfiguration configuration) { _repository = repository; _context = context; _emailServices = emailServices; _configuration = configuration; } public IActionResult Index() { var totalSubscribedUsers = _context.Subscriptions.Count(s => s.IsSubscribed); // Pass the total count to the view ViewBag.TotalSubscribedUsers = totalSubscribedUsers; var newsLetterFromdb = _repository.GetAll(); var viewmodel = new List(); foreach (var item in newsLetterFromdb) { viewmodel.Add(new NewsLetterViewModel { Id=item.Id, Name=item.Name, Email=item.Email, IsSubscribed=item.IsSubscribed }); } return View(viewmodel); } public IActionResult Create() { var totalSubscribedUsers = _context.Subscriptions.Count(s => s.IsSubscribed); // Pass the total count to the view ViewBag.TotalSubscribedUsers = totalSubscribedUsers; return View(); } [HttpPost] public async Task Create(SendNewsLetterViewModel viewModel) { if(ModelState.IsValid) { try { // Retrieve all subscribed users var subscribedUsers = await _context.Subscriptions.Where(s => s.IsSubscribed).ToListAsync(); string confirmationPath = _configuration["Email:unsubscribePath"]; // Send the newsletter email to each subscribed user foreach (var user in subscribedUsers) { string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={user.Email}"; string emailBody = $"

Hey {user.Name}


{viewModel.Body}
Søren Eggert Lundsteen Olsen
Seosoft ApS

Hovedgaden 3
Jordrup
Kolding 6064
Denmark


Unsubscribe
"; var email = new EmailToSend(user.Email, viewModel.Subject, emailBody); var isSent = await _emailServices.SendConfirmationEmailAsync(email); // Handle failure to send email if needed } TempData["success"] = "Nesletter sent successfully."; return RedirectToAction(nameof(Index)); } catch (Exception ex) { // Log or handle the exception as needed TempData["success"] = "something went wrong."; return RedirectToAction(nameof(Index)); } } return View(viewModel); } [HttpGet] public IActionResult Delete(int id) { var newsLetterFromDb = _repository.GetById(id); var viewmodel = new NewsLetterViewModel { Id=newsLetterFromDb.Id, Email=newsLetterFromDb.Email, Name=newsLetterFromDb.Name, IsSubscribed=newsLetterFromDb.IsSubscribed }; return View(viewmodel); } [HttpPost] [ActionName("Delete")] public IActionResult DeleteConfirm(int id) { _repository.Delete(id); _context.SaveChanges(); TempData["Success"] = "Subscriber deleted successfully"; return RedirectToAction(nameof(Index)); } } }