registration back #7

Merged
mfnefd merged 4 commits from registration into main 2024-06-15 01:59:03 +04:00
10 changed files with 159 additions and 2 deletions
Showing only changes of commit 64cb4f1ac9 - Show all commits

View File

@ -1,4 +1,6 @@
using BusinessLogic.Tools;
using BusinessLogic.Tools.Mail;
using BusinessLogic.Tools.Mail.MailTemplates;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Converters;
@ -39,6 +41,8 @@ namespace BusinessLogic.BusinessLogic
throw new Exception("Insert operation failed.");
}
MailSender.Send(new MailRegistration(user));
return UserConverter.ToView(user);
}
@ -52,6 +56,7 @@ namespace BusinessLogic.BusinessLogic
{
throw new ElementNotFoundException();
}
MailSender.Send(new MailDeleteUser(user));
return UserConverter.ToView(user);
}
@ -99,6 +104,9 @@ namespace BusinessLogic.BusinessLogic
{
throw new Exception("Update operation failed.");
}
MailSender.Send(new MailUpdateUserData(user));
return UserConverter.ToView(user);
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail
{
public class Mail
{
public IEnumerable<string> To { get; set; } = null!;
public string Title { get; set; } = null!;
public string Body { get; set; } = null!;
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail
{
public class MailOptions
{
public string Email { get; set; } = null!;
public string Password { get; set; } = null!;
public string SmtpClientHost { get; set; } = null!;
public short SmtpClientPort { get; set; }
}
}

View File

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail
{
public class MailSender
{
private static string _email;
private static string _password;
private static string _smtpClientHost;
private static short _smtpClientPort;
public void SetupMailOptions(MailOptions options)
{
_email = options.Email;
_password = options.Password;
_smtpClientHost = options.SmtpClientHost;
_smtpClientPort = options.SmtpClientPort;
}
public static void Send(Mail mail)
{
using SmtpClient client = new SmtpClient(_smtpClientHost, _smtpClientPort);
client.Credentials = new NetworkCredential(_email, _password);
client.EnableSsl = true;
using MailMessage message = new MailMessage();
message.From = new MailAddress(_email);
foreach (string to in mail.To)
{
message.To.Add(to);
}
message.Subject = mail.Title;
message.Body = mail.Body;
client.Send(message);
}
}
}

View File

@ -0,0 +1,20 @@
using Contracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail.MailTemplates
{
public class MailDeleteUser : Mail
{
public MailDeleteUser(UserBindingModel user)
{
To = [user.Email];
Title = "Ваш аккаунт был удален!";
Body = $"Уважаемый {user.SecondName} {user.FirstName}, Ваш аккаунт был удален навсегда насовсем.\n" +
$"Если это были не Вы... Тут уже ничего не поможет, приносим наши извинения.";
}
}
}

View File

@ -0,0 +1,21 @@
using Contracts.BindingModels;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail.MailTemplates
{
public class MailRegistration : Mail
{
public MailRegistration(UserBindingModel user)
{
To = [user.Email];
Title = "Приветствуем Вас на нашем сайте!";
Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" +
$"Надеемся, что Вам что-то уже приглянулось!";
}
}
}

View File

@ -0,0 +1,20 @@
using Contracts.BindingModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail.MailTemplates
{
public class MailUpdateUserData : Mail
{
public MailUpdateUserData(UserBindingModel user)
{
To = [user.Email];
Title = "Данные пользователя были обновлены!";
Body = $"Уважаемый {user.SecondName} {user.FirstName}, Ваши данные были обвновлены.\n" +
$"Если это были не Вы, то что поделать, Вам придется менять пароль.";
}
}
}

View File

@ -56,13 +56,11 @@ namespace RestAPI.Controllers
catch (AccountException ex)
{
_logger.LogWarning(ex, "Wrong registration data");
throw;
return Results.BadRequest(ex.Message);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error create user");
throw;
return Results.Problem(ex.Message);
}
}

View File

@ -49,6 +49,13 @@ jwtProvider?.SetupJwtOptions(new()
SecretKey = getSection("JwtOptions:SecretKey") ?? string.Empty,
ExpiresHours = Convert.ToInt16(getSection("JwtOptions:ExpiresHours"))
});
mailSender?.SetupMailOptions(new()
{
Email = getSection("MailOptions:Email") ?? string.Empty,
Password = getSection("MailOptions:Password") ?? string.Empty,
SmtpClientHost = getSection("MailOptions:SmtpClientHost") ?? string.Empty,
SmtpClientPort = Convert.ToInt16(getSection("MailOptions:SmtpClientPort"))
});
#endregion Setup config

View File

@ -9,5 +9,11 @@
"SecretKey": "secretkey_secretkey_secretkey_secretkey",
"ExpiresHours": 24
},
"MailOptions": {
"Email": "21.guns.site@gmail.com",
"Password": "tiss lpgf nhap qnfc",
"SmtpClientHost": "smtp.gmail.com",
"SmtpClientPort": "587"
},
"AllowedHosts": "*"
}