From 536a8bfe913a481252e40186d15df0b295464bfa Mon Sep 17 00:00:00 2001 From: mfnefd Date: Sat, 22 Jun 2024 21:15:11 +0400 Subject: [PATCH 1/3] add mail sending policy for user (API) --- BusinessLogic/Tools/Mail/Mail.cs | 13 +- BusinessLogic/Tools/Mail/MailSender.cs | 60 ++++--- .../Mail/MailTemplates/MailRegistration.cs | 24 +-- Contracts/BindingModels/UserBindingModel.cs | 23 +-- Contracts/Converters/UserConverter.cs | 44 ++--- Contracts/SearchModels/UserSearchModel.cs | 11 +- Contracts/ViewModels/UserViewModel.cs | 19 +- DataModels/Models/IUser.cs | 17 +- DatabaseImplement/Implements/UserStorage.cs | 165 +++++++++--------- DatabaseImplement/Models/User.cs | 120 +++++++------ 10 files changed, 258 insertions(+), 238 deletions(-) diff --git a/BusinessLogic/Tools/Mail/Mail.cs b/BusinessLogic/Tools/Mail/Mail.cs index 5adba23..44cc1a8 100644 --- a/BusinessLogic/Tools/Mail/Mail.cs +++ b/BusinessLogic/Tools/Mail/Mail.cs @@ -6,10 +6,11 @@ using System.Threading.Tasks; namespace BusinessLogic.Tools.Mail { - public class Mail - { - public IEnumerable To { get; set; } = null!; - public string Title { get; set; } = null!; - public string Body { get; set; } = null!; - } + public class Mail + { + public IEnumerable To { get; set; } = null!; + public string Title { get; set; } = null!; + public string Body { get; set; } = null!; + public bool IsSendable { get; set; } = true; + } } \ No newline at end of file diff --git a/BusinessLogic/Tools/Mail/MailSender.cs b/BusinessLogic/Tools/Mail/MailSender.cs index 329b392..3df9c48 100644 --- a/BusinessLogic/Tools/Mail/MailSender.cs +++ b/BusinessLogic/Tools/Mail/MailSender.cs @@ -8,39 +8,41 @@ 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 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 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; + public static void Send(Mail mail) + { + if (!mail.IsSendable) return; - using MailMessage message = new MailMessage(); + using SmtpClient client = new SmtpClient(_smtpClientHost, _smtpClientPort); + client.Credentials = new NetworkCredential(_email, _password); + client.EnableSsl = true; - message.From = new MailAddress(_email); - foreach (string to in mail.To) - { - message.To.Add(to); - } + using MailMessage message = new MailMessage(); - message.Subject = mail.Title; - message.Body = mail.Body; + message.From = new MailAddress(_email); + foreach (string to in mail.To) + { + message.To.Add(to); + } - client.Send(message); - } - } + message.Subject = mail.Title; + message.Body = mail.Body; + + client.Send(message); + } + } } \ No newline at end of file diff --git a/BusinessLogic/Tools/Mail/MailTemplates/MailRegistration.cs b/BusinessLogic/Tools/Mail/MailTemplates/MailRegistration.cs index fdd33d9..3a6c2dc 100644 --- a/BusinessLogic/Tools/Mail/MailTemplates/MailRegistration.cs +++ b/BusinessLogic/Tools/Mail/MailTemplates/MailRegistration.cs @@ -8,14 +8,18 @@ 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" + - $"Надеемся, что Вам что-то уже приглянулось!"; - } - } + public class MailRegistration : Mail + { + public MailRegistration(UserBindingModel user) + { + if (user.OnlyImportantMails) + { + IsSendable = false; + } + To = [user.Email]; + Title = "Приветствуем Вас на нашем сайте!"; + Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" + + $"Надеемся, что Вам что-то уже приглянулось!"; + } + } } \ No newline at end of file diff --git a/Contracts/BindingModels/UserBindingModel.cs b/Contracts/BindingModels/UserBindingModel.cs index d5e19ae..6c192d6 100644 --- a/Contracts/BindingModels/UserBindingModel.cs +++ b/Contracts/BindingModels/UserBindingModel.cs @@ -6,15 +6,16 @@ using System.Threading.Tasks; namespace Contracts.BindingModels { - public class UserBindingModel - { - public Guid Id { get; set; } - public string FirstName { get; set; } = string.Empty; - public string SecondName { get; set; } = string.Empty; - public string Email { get; set; } = string.Empty; - public string PasswordHash { get; set; } = string.Empty; - public string? Password { get; set; } - public DateTime Birthday { get; set; } - public RoleBindingModel Role { get; set; } = null!; - } + public class UserBindingModel + { + public Guid Id { get; set; } + public string FirstName { get; set; } = string.Empty; + public string SecondName { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public string PasswordHash { get; set; } = string.Empty; + public string? Password { get; set; } + public DateTime Birthday { get; set; } + public bool OnlyImportantMails { get; set; } + public RoleBindingModel Role { get; set; } = null!; + } } \ No newline at end of file diff --git a/Contracts/Converters/UserConverter.cs b/Contracts/Converters/UserConverter.cs index b951d11..ae6fd8a 100644 --- a/Contracts/Converters/UserConverter.cs +++ b/Contracts/Converters/UserConverter.cs @@ -8,26 +8,28 @@ using System.Threading.Tasks; namespace Contracts.Converters { - public static class UserConverter - { - public static UserViewModel ToView(UserBindingModel model) => new() - { - Id = model.Id, - FirstName = model.FirstName, - SecondName = model.SecondName, - Email = model.Email, - Birthday = model.Birthday, - Role = RoleConverter.ToView(model.Role), - }; + public static class UserConverter + { + public static UserViewModel ToView(UserBindingModel model) => new() + { + Id = model.Id, + FirstName = model.FirstName, + SecondName = model.SecondName, + Email = model.Email, + Birthday = model.Birthday, + OnlyImportantMails = model.OnlyImportantMails, + Role = RoleConverter.ToView(model.Role), + }; - public static UserBindingModel ToBinding(UserViewModel model) => new() - { - Id = model.Id, - FirstName = model.FirstName, - SecondName = model.SecondName, - Email = model.Email, - Birthday = model.Birthday, - Role = RoleConverter.ToBinding(model.Role), - }; - } + public static UserBindingModel ToBinding(UserViewModel model) => new() + { + Id = model.Id, + FirstName = model.FirstName, + SecondName = model.SecondName, + Email = model.Email, + Birthday = model.Birthday, + OnlyImportantMails = model.OnlyImportantMails, + Role = RoleConverter.ToBinding(model.Role), + }; + } } \ No newline at end of file diff --git a/Contracts/SearchModels/UserSearchModel.cs b/Contracts/SearchModels/UserSearchModel.cs index 2df2131..37271fc 100644 --- a/Contracts/SearchModels/UserSearchModel.cs +++ b/Contracts/SearchModels/UserSearchModel.cs @@ -6,9 +6,10 @@ using System.Threading.Tasks; namespace Contracts.SearchModels { - public class UserSearchModel - { - public Guid? Id { get; set; } - public string? Email { get; set; } - } + public class UserSearchModel + { + public Guid? Id { get; set; } + public string? Email { get; set; } + public bool? OnlyImportantMails { get; set; } + } } \ No newline at end of file diff --git a/Contracts/ViewModels/UserViewModel.cs b/Contracts/ViewModels/UserViewModel.cs index 6174d56..6b3ccd4 100644 --- a/Contracts/ViewModels/UserViewModel.cs +++ b/Contracts/ViewModels/UserViewModel.cs @@ -6,13 +6,14 @@ using System.Threading.Tasks; namespace Contracts.ViewModels { - public class UserViewModel - { - public Guid Id { get; set; } - public string FirstName { get; set; } = string.Empty; - public string SecondName { get; set; } = string.Empty; - public string Email { get; set; } = string.Empty; - public DateTime Birthday { get; set; } - public RoleViewModel Role { get; set; } = null!; - } + public class UserViewModel + { + public Guid Id { get; set; } + public string FirstName { get; set; } = string.Empty; + public string SecondName { get; set; } = string.Empty; + public string Email { get; set; } = string.Empty; + public DateTime Birthday { get; set; } + public bool OnlyImportantMails { get; set; } + public RoleViewModel Role { get; set; } = null!; + } } \ No newline at end of file diff --git a/DataModels/Models/IUser.cs b/DataModels/Models/IUser.cs index 9eb2ef6..fb5020d 100644 --- a/DataModels/Models/IUser.cs +++ b/DataModels/Models/IUser.cs @@ -6,12 +6,13 @@ using System.Threading.Tasks; namespace DataModels.Models { - public interface IUser : IId - { - string FirstName { get; } - string SecondName { get; } - string PasswordHash { get; } - string Email { get; } - DateTime Birthday { get; } - } + public interface IUser : IId + { + string FirstName { get; } + string SecondName { get; } + string PasswordHash { get; } + string Email { get; } + DateTime Birthday { get; } + bool OnlyImportantMails { get; } + } } \ No newline at end of file diff --git a/DatabaseImplement/Implements/UserStorage.cs b/DatabaseImplement/Implements/UserStorage.cs index fdbdf0a..2755bbf 100644 --- a/DatabaseImplement/Implements/UserStorage.cs +++ b/DatabaseImplement/Implements/UserStorage.cs @@ -12,98 +12,99 @@ using System.Threading.Tasks; namespace DatabaseImplement.Implements { - public class UserStorage : IUserStorage - { - public UserBindingModel? Delete(UserSearchModel model) - { - if (model.Id is null && model.Email is null) - { - return null; - } + public class UserStorage : IUserStorage + { + public UserBindingModel? Delete(UserSearchModel model) + { + if (model.Id is null && model.Email is null) + { + return null; + } - var context = new Database(); - var user = context.Users.FirstOrDefault(u => - (model.Id.HasValue && u.Id == model.Id) - || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))); + var context = new Database(); + var user = context.Users.FirstOrDefault(u => + (model.Id.HasValue && u.Id == model.Id) + || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))); - if (user is null) - { - return null; - } - context.Remove(user); - context.SaveChanges(); + if (user is null) + { + return null; + } + context.Remove(user); + context.SaveChanges(); - return user.GetBindingModel(); - } + return user.GetBindingModel(); + } - public UserBindingModel? GetElement(UserSearchModel model) - { - if (model.Id is null && model.Email is null) - { - return null; - } - var context = new Database(); - return context.Users - .Include(u => u.Role) - .FirstOrDefault(u => - (model.Id.HasValue && u.Id == model.Id) - || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))) - ?.GetBindingModel(); - } + public UserBindingModel? GetElement(UserSearchModel model) + { + if (model.Id is null && model.Email is null) + { + return null; + } + var context = new Database(); + return context.Users + .Include(u => u.Role) + .FirstOrDefault(u => + (model.Id.HasValue && u.Id == model.Id) + || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))) + ?.GetBindingModel(); + } - public IEnumerable GetList(UserSearchModel? model) - { - var context = new Database(); - if (model is null) - { - return context.Users - .Include(u => u.Role) - .Select(r => r.GetBindingModel()); - } - if (model.Id is null && model.Email is null) - { - return []; - } - return context.Users - .Where(u => - (model.Id.HasValue && u.Id == model.Id) - || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))) - .Include(u => u.Role) - .Select(r => r.GetBindingModel()); - } + public IEnumerable GetList(UserSearchModel? model) + { + var context = new Database(); + if (model is null) + { + return context.Users + .Include(u => u.Role) + .Select(r => r.GetBindingModel()); + } + if (model.Id is null && model.Email is null && !model.OnlyImportantMails is null) + { + return []; + } + return context.Users + .Where(u => + (model.Id.HasValue && u.Id == model.Id) + || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)) + || (model.OnlyImportantMails.HasValue && u.OnlyImportantMails == model.OnlyImportantMails)) + .Include(u => u.Role) + .Select(r => r.GetBindingModel()); + } - public UserBindingModel? Insert(UserBindingModel model) - { - var context = new Database(); - var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id); - if (role is null) - { - return null; - } - var newUser = Models.User.ToUserFromBinding(model, role); + public UserBindingModel? Insert(UserBindingModel model) + { + var context = new Database(); + var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id); + if (role is null) + { + return null; + } + var newUser = Models.User.ToUserFromBinding(model, role); - context.Users.Add(newUser); - context.SaveChanges(); + context.Users.Add(newUser); + context.SaveChanges(); - return newUser.GetBindingModel(); - } + return newUser.GetBindingModel(); + } - public UserBindingModel? Update(UserBindingModel model) - { - var context = new Database(); - var user = context.Users - .FirstOrDefault(u => u.Id == model.Id); - var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id); + public UserBindingModel? Update(UserBindingModel model) + { + var context = new Database(); + var user = context.Users + .FirstOrDefault(u => u.Id == model.Id); + var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id); - if (user is null || role is null) - { - return null; - } + if (user is null || role is null) + { + return null; + } - user.Update(model, role); + user.Update(model, role); - context.SaveChanges(); - return user.GetBindingModel(); - } - } + context.SaveChanges(); + return user.GetBindingModel(); + } + } } \ No newline at end of file diff --git a/DatabaseImplement/Models/User.cs b/DatabaseImplement/Models/User.cs index a2f2ecf..4de0fd1 100644 --- a/DatabaseImplement/Models/User.cs +++ b/DatabaseImplement/Models/User.cs @@ -4,6 +4,7 @@ using Contracts.ViewModels; using DataModels.Models; using System; using System.Collections.Generic; +using System.ComponentModel; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; @@ -11,72 +12,77 @@ using System.Threading.Tasks; namespace DatabaseImplement.Models { - public class User : IUser - { - public Guid Id { get; set; } + public class User : IUser + { + public Guid Id { get; set; } - [Required] - public string FirstName { get; set; } = string.Empty; + [Required] + public string FirstName { get; set; } = string.Empty; - [Required] - public string SecondName { get; set; } = string.Empty; + [Required] + public string SecondName { get; set; } = string.Empty; - [Required] - public string PasswordHash { get; set; } = string.Empty; + [Required] + public string PasswordHash { get; set; } = string.Empty; - [Required] - public string Email { get; set; } = string.Empty; + [Required] + public string Email { get; set; } = string.Empty; - [Required] - public DateTime Birthday { get; set; } + [Required] + public DateTime Birthday { get; set; } - public Role? Role { get; set; } + public Role? Role { get; set; } - public UserBindingModel GetBindingModel() => new() - { - Id = Id, - FirstName = FirstName, - SecondName = SecondName, - Email = Email, - PasswordHash = PasswordHash, - Birthday = Birthday, - Role = Role?.GetBindingModel() ?? new() - }; + public bool OnlyImportantMails { get; set; } = false; - public static User ToUserFromView(UserViewModel model, Role role) => new() - { - Id = model.Id, - FirstName = model.FirstName, - SecondName = model.SecondName, - Email = model.Email, - Birthday = model.Birthday, - Role = role - }; + public UserBindingModel GetBindingModel() => new() + { + Id = Id, + FirstName = FirstName, + SecondName = SecondName, + Email = Email, + PasswordHash = PasswordHash, + Birthday = Birthday, + OnlyImportantMails = OnlyImportantMails, + Role = Role?.GetBindingModel() ?? new() + }; - public static User ToUserFromBinding(UserBindingModel model, Role role) => new() - { - Id = model.Id, - FirstName = model.FirstName, - SecondName = model.SecondName, - Email = model.Email, - PasswordHash = model.PasswordHash, - Birthday = model.Birthday, - Role = role - }; + public static User ToUserFromView(UserViewModel model, Role role) => new() + { + Id = model.Id, + FirstName = model.FirstName, + SecondName = model.SecondName, + Email = model.Email, + Birthday = model.Birthday, + Role = role + }; - public void Update(UserBindingModel model, Role role) - { - if (model is null) - { - throw new ArgumentNullException("Update user: binding model is null"); - } + public static User ToUserFromBinding(UserBindingModel model, Role role) => new() + { + Id = model.Id, + FirstName = model.FirstName, + SecondName = model.SecondName, + Email = model.Email, + PasswordHash = model.PasswordHash, + Birthday = model.Birthday, + OnlyImportantMails = model.OnlyImportantMails, + Role = role + }; - Email = model.Email; - FirstName = model.FirstName; - SecondName = model.SecondName; - PasswordHash = model.PasswordHash; - Birthday = model.Birthday; - Role = role; - } - } + public void Update(UserBindingModel model, Role role) + { + if (model is null) + { + throw new ArgumentNullException("Update user: binding model is null"); + } + + Email = model.Email ?? Email; + FirstName = model.FirstName ?? FirstName; + SecondName = model.SecondName ?? SecondName; + PasswordHash = model.PasswordHash ?? PasswordHash; + Birthday = model.Birthday; + OnlyImportantMails = model.OnlyImportantMails; + Role = role ?? Role; + } + } } \ No newline at end of file From bbe1fba99ee47d69fdab619e52ca2d98970f0236 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Sat, 22 Jun 2024 21:15:42 +0400 Subject: [PATCH 2/3] add mail sending policy for user (Web) --- WebApp/Pages/SignUp.cshtml | 7 +++++++ WebApp/Pages/User/Settings.cshtml | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/WebApp/Pages/SignUp.cshtml b/WebApp/Pages/SignUp.cshtml index fb018dc..31c7539 100644 --- a/WebApp/Pages/SignUp.cshtml +++ b/WebApp/Pages/SignUp.cshtml @@ -74,6 +74,13 @@ +
+ + +
+