Email Service creado y errores de compilacion arreglados
This commit is contained in:
@@ -8,10 +8,10 @@ namespace CleanArchitecture.Application.Exceptions
|
||||
{
|
||||
Errors = new Dictionary<string, string[]>();
|
||||
}
|
||||
public ValidationException(IEnumerable<ValidationFailure> failures): this()
|
||||
{
|
||||
Errors = failures.GroupBy(e=>e.PropertyName, e => e.ErrorMessage).
|
||||
ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
|
||||
public ValidationException(IEnumerable<ValidationFailure> failures): this()
|
||||
{
|
||||
Errors = failures.GroupBy(e=>e.PropertyName, e => e.ErrorMessage).
|
||||
ToDictionary(failureGroup => failureGroup.Key, failureGroup => failureGroup.ToArray());
|
||||
}
|
||||
public IDictionary<string, string[]> Errors { get; }
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.0" />
|
||||
<PackageReference Include="SendGrid" Version="9.29.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
using CleanArchitecture.Application.Contracts.Infrastructure;
|
||||
using CleanArchitecture.Application.Models;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using SendGrid;
|
||||
using SendGrid.Helpers.Mail;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CleanArchitecture.Infrastructure.Email
|
||||
{
|
||||
public class EmailService : IEmailService
|
||||
{
|
||||
public EmailSettings emailSettings { get; }
|
||||
public ILogger<EmailService> logger { get; }
|
||||
|
||||
public EmailService(IOptions<EmailSettings> _emailSettings, ILogger<EmailService> _logger)
|
||||
{
|
||||
emailSettings = _emailSettings.Value;
|
||||
logger = _logger;
|
||||
}
|
||||
|
||||
public async Task<bool> SendEmail(Application.Models.Email email)
|
||||
{
|
||||
var client = new SendGridClient(emailSettings.ApiKey);
|
||||
|
||||
var subject = email.Subject;
|
||||
var to = new EmailAddress(email.To);
|
||||
var emailBody = email.Body;
|
||||
|
||||
var from = new EmailAddress
|
||||
{
|
||||
Email = emailSettings.FromAddress,
|
||||
Name = emailSettings.FromName
|
||||
};
|
||||
|
||||
var sendGridMessage = MailHelper.CreateSingleEmail(from, to, subject, emailBody, emailBody);
|
||||
var response = await client.SendEmailAsync(sendGridMessage);
|
||||
|
||||
if(response.StatusCode == System.Net.HttpStatusCode.Accepted || response.StatusCode == System.Net.HttpStatusCode.OK) {
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
logger.LogError($"The email can not been send");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -48,7 +48,7 @@ namespace CleanArchitecture.Infrastructure.Repositories
|
||||
{
|
||||
IQueryable<T> query = context.Set<T>();
|
||||
if (disableTracking) query = query.AsNoTracking();
|
||||
if (includes != null) query = includes.Aggregate(query, (current, include) => current.Include(include);
|
||||
if (includes != null) query = includes.Aggregate(query, (current, include) => current.Include(include));
|
||||
if(predicate != null) query = query.Where(predicate);
|
||||
if (orderBy!=null)
|
||||
return await orderBy(query).ToListAsync();
|
||||
|
||||
Reference in New Issue
Block a user