Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions TickAPI/TickAPI/Common/Mail/Abstractions/IMailService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using TickAPI.Common.Mail.Models;
using TickAPI.Common.Results;
using TickAPI.Customers.Models;
using TickAPI.Tickets.Models;

namespace TickAPI.Common.Mail.Abstractions;

public interface IMailService
{
public Task<Result> SendTicketAsync(MailRecipient recipient, string eventName, byte[] pdfData);

public Task<Result> SendTicketsAsync(Customer customer, List<TicketWithScanUrl> tickets);
public Task<Result> SendMailAsync(IEnumerable<MailRecipient> recipients, string subject, string content, List<MailAttachment>? attachments);
}
50 changes: 37 additions & 13 deletions TickAPI/TickAPI/Common/Mail/Services/MailService.cs
Original file line number Diff line number Diff line change
@@ -1,37 +1,61 @@
using SendGrid;
using System.Text;
using SendGrid;
using SendGrid.Helpers.Mail;
using TickAPI.Common.Mail.Abstractions;
using TickAPI.Common.Mail.Models;
using TickAPI.Common.QR.Abstractions;
using TickAPI.Common.Results;
using TickAPI.Customers.Models;
using TickAPI.Tickets.Models;

namespace TickAPI.Common.Mail.Services;

public class MailService : IMailService
{
private readonly SendGridClient _client;
private readonly EmailAddress _fromEmailAddress;
private readonly IQRCodeService _qrCodeService;

public MailService(IConfiguration configuration)
public MailService(IConfiguration configuration, IQRCodeService qrCodeService)
{
_qrCodeService = qrCodeService;
var apiKey = configuration["SendGrid:ApiKey"];
_client = new SendGridClient(apiKey);
var fromEmail = configuration["SendGrid:FromEmail"];
var fromName = configuration["SendGrid:FromName"];
_fromEmailAddress = new EmailAddress(fromEmail, fromName);
}

public async Task<Result> SendTicketAsync(MailRecipient recipient, string eventName, byte[] pdfData)
public async Task<Result> SendTicketsAsync(Customer customer, List<TicketWithScanUrl> tickets)
{
var subject = $"Ticket for {eventName}";
var htmlContent = "<strong>Download your ticket from attachments</strong>";
var base64Content = Convert.ToBase64String(pdfData);
List<MailAttachment> attachments = [
new MailAttachment("ticket.pdf", base64Content, "application/pdf")
];
var res = await SendMailAsync([recipient], subject, htmlContent, attachments);
return res;
}
var subject = "Your New Tickets";
var htmlContent = new StringBuilder();
htmlContent.AppendLine("<strong>Here are your tickets:</strong><br/><ul>");

var attachments = new List<MailAttachment>();

foreach (var tWithScanUrl in tickets)
{
var ticket = tWithScanUrl.Ticket;
var eventName = ticket.Type.Event.Name;
var eventDate = ticket.Type.Event.StartDate.ToString("yyyy-MM-dd");

htmlContent.AppendLine(
$"<li>Ticket for event <b>{eventName}</b> on {eventDate} "
);

var pdfData = _qrCodeService.GenerateQrCode(tWithScanUrl.ScanUrl);

var base64Content = Convert.ToBase64String(pdfData);
attachments.Add(new MailAttachment($"ticket_{ticket.Id}.pdf", base64Content, "application/pdf"));
}

htmlContent.AppendLine("</ul>");

var recipient = new MailRecipient(customer.Email, customer.FirstName);
return await SendMailAsync([recipient], subject, htmlContent.ToString(), attachments);
}

public async Task<Result> SendMailAsync(IEnumerable<MailRecipient> recipients, string subject, string content,
List<MailAttachment>? attachments = null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public TickApiDbContext(DbContextOptions<TickApiDbContext> options) : base(optio
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Ticket>().Property(t => t.ResellPrice).HasColumnType("decimal(18,2)");
modelBuilder.Entity<Ticket>().Property(t => t.ResellPrice).IsRequired(false);
modelBuilder.Entity<TicketType>().Property(t => t.Price).HasColumnType("decimal(18,2)");
modelBuilder.Entity<Category>().HasData(
new Category
Expand Down
Loading
Loading