using Data; using Mailjet.Client; using Mailjet.Client.Resources; using Microsoft.AspNetCore.Mvc; using Model; using Services.Implemnetation; using Newtonsoft.Json.Linq; using Services.Interaces; using Services.EmailSend; using Microsoft.EntityFrameworkCore; namespace Web.Controllers { public class SubscriptionController : Controller { private readonly SurveyContext _context; private readonly IEmailServices _mailSerivces; private readonly IConfiguration _configuration; public SubscriptionController(SurveyContext context,IEmailServices mailSerivces,IConfiguration configuration) { _context = context; _mailSerivces = mailSerivces; _configuration = configuration; } // GET: /Subscription/Subscribe public IActionResult Subscribe() { return View(); } // POST: /Subscription/Subscribe [HttpPost] [ValidateAntiForgeryToken] public async Task Subscribe(Subscription subscription) { if (ModelState.IsValid) { try { string email = subscription.Email; string senderName = email.Substring(0, email.IndexOf('@')).ToUpper(); // Check if the email already exists var existingSubscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (existingSubscription != null) { TempData["error"] = "Du er allerede abonneret."; return RedirectToAction("", "home"); } string subject = "Bekræft dit abonnement"; string confirmationPath = _configuration["Email:ConfirmEmailPath"]; // Retrieve the confirmation path from appsettings string confirmationUrl = $"{Request.Scheme}://{Request.Host}/{confirmationPath}?email={email}"; // Construct the confirmation URL string unsubscribeUrl = $"{Request.Scheme}://{Request.Host}/unsubscribe?email={email}"; // Add unsubscribe URL string body = $@"

Hej {senderName},

Tak for dit abonnement!

For at bekræfte dit abonnement, skal du klikke på knappen herunder:

Bekræft abonnement

Hvis knappen ikke virker, kan du kopiere dette link ind i din browser:

{confirmationUrl}


Med venlig hilsen,
Nærværskonsulenterne ApS


Nærværskonsulenterne ApS
Brødemosevej 24A, 3300 Frederiksværk
kontakt@nvkn.dk

"; var newEmail = new EmailToSend(email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(newEmail); var subscriber = new Subscription { Name = senderName, Email = subscription.Email, IsSubscribed = false }; _context.Subscriptions.Add(subscriber); await _context.SaveChangesAsync(); TempData["success"] = "Abonnement oprettet. Bekræft venligst din email."; return RedirectToAction("", "home"); } catch (Exception) { TempData["error"] = "Kunne ikke oprette abonnement."; return RedirectToAction("", "home"); } } return RedirectToAction("", "home"); } [HttpGet] public async Task Confirmation(string email) { try { // Find the subscription with the provided email var subscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (subscription != null) { if (subscription.IsSubscribed) { // If IsSubscribed is already true, inform the user that the email is already confirmed ViewBag.Message = "Din email er allerede bekræftet. Tak!"; } else { // Update the IsSubscribed property to true subscription.IsSubscribed = true; _context.Subscriptions.Update(subscription); var sentEmails = _context.SentNewsletterEamils.Where(e => e.RecipientEmail == email); // Set IsUnsubscribed flag to true for email events foreach (var emailEvent in sentEmails) { emailEvent.IsUnsubscribed = false; _context.Entry(emailEvent).State = EntityState.Modified; } await _context.SaveChangesAsync(); // Send a "thank you" email to the user string subject = "Tak for bekræftelse af dit abonnement"; string unsubscribeUrl = $"{Request.Scheme}://{Request.Host}/unsubscribe?email={email}"; string body = $@"

Hej {subscription.Name},

Tak for at bekræfte dit abonnement. Du er nu tilmeldt vores nyhedsbrev.

Du vil modtage vores seneste nyheder og opdateringer på denne email-adresse.


Med venlig hilsen,
Nærværskonsulenterne ApS


Nærværskonsulenterne ApS
Brødemosevej 24A, 3300 Frederiksværk
kontakt@nvkn.dk

"; var thankYouEmail = new EmailToSend(subscription.Email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(thankYouEmail); // Inform the user that the email has been confirmed ViewBag.Message = "Tak for at bekræfte din email. Du er nu tilmeldt!"; } return View(subscription); // You can return a view to show a confirmation message } else { ViewBag.Message = "Du er blevet afmeldt vores nyhedsbrev. Tak!"; return View(subscription); } } catch (Exception ex) { // Log or handle the exception as needed return View("Error"); // You can return a view to show an error message } } [HttpGet] public async Task UnsubscribeConfirmation(string email) { try { // Find the subscription with the provided email var subscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); if (subscription != null) { if (subscription.IsSubscribed) { // Update the IsSubscribed property to false subscription.IsSubscribed = false; _context.Subscriptions.Remove(subscription); await _context.SaveChangesAsync(); // Remove the email from SentNewsletterEmail var sentEmails = _context.SentNewsletterEamils.Where(e => e.RecipientEmail == email); // Set IsUnsubscribed flag to true for email events foreach (var emailEvent in sentEmails) { emailEvent.IsUnsubscribed = true; _context.Entry(emailEvent).State = EntityState.Modified; } await _context.SaveChangesAsync(); // Inform the user that the email has been unsubscribed ViewBag.Message = "Du er nu afmeldt vores nyhedsbrev. Vi er kede af at se dig gå."; // Send a simple confirmation email to the user string subject = "Bekræftelse på afmelding"; // Simple plain text email body string body = $@"Hej,

Du er nu afmeldt vores nyhedsbrev.

Med venlig hilsen,
Nærværskonsulenterne ApS


Nærværskonsulenterne ApS
Brødemosevej 24A, 3300 Frederiksværk
kontakt@nvkn.dk"; var confirmationEmail = new EmailToSend(subscription.Email, subject, body); await _mailSerivces.SendConfirmationEmailAsync(confirmationEmail); return View(subscription); } else { // If IsSubscribed is already false, inform the user that the email is already unsubscribed ViewBag.Message = "Din email er allerede afmeldt. Tak!"; return View(subscription); } } else { // Inform the user that the unsubscription process couldn't be completed ViewBag.Message = "Du er blevet afmeldt vores nyhedsbrev. Tilmeld dig først."; return View(subscription); } } catch (Exception ex) { // Log or handle the exception as needed return View("Error"); } } //[HttpGet] //public async Task UnsubscribeConfirmation(string email) //{ // try // { // // Find the subscription with the provided email // var subscription = await _context.Subscriptions.FirstOrDefaultAsync(s => s.Email == email); // if (subscription != null) // { // if (subscription.IsSubscribed) // { // // Update the IsSubscribed property to false // subscription.IsSubscribed = false; // _context.Subscriptions.Remove(subscription); // await _context.SaveChangesAsync(); // // Inform the user that the email has been unsubscribed // ViewBag.Message = "You have successfully unsubscribed from our newsletter. We're sorry to see you go"; // // Optionally, send an email confirmation to the user // string subject = "Unsubscription Confirmation"; // string body = $@" // // // Unsubscribe Confirmation // // // //
//

Unsubscribe Confirmation

//

You have successfully unsubscribed from our newsletter. We're sorry to see you go.

//
//
Søren Eggert Lundsteen Olsen
//
SeoSoft ApS
//
//
Hovedgaden 3
Jordrup
Kolding 6064
Denmark
//
// // "; // var thankYouEmail = new EmailToSend(subscription.Email, subject, body); // await _mailSerivces.SendConfirmationEmailAsync(thankYouEmail); // return View(subscription); // You can return a view to show a confirmation message // } // else // { // // If IsSubscribed is already false, inform the user that the email is already unsubscribed // ViewBag.Message = "Your email is already unsubscribed. Thank you!"; // return View(subscription); // You can return a view to show a message // } // } // else // { // // Inform the user that the unsubscription process couldn't be completed // ViewBag.Message = "You have been unsubscribed from our newsletter. subscribe first."; // return View(subscription); // You can return a view to show an error message // } // } // catch (Exception ex) // { // // Log or handle the exception as needed // return View("Error"); // You can return a view to show an error message // } //} } }