add mail sending policy for user (API)

This commit is contained in:
mfnefd 2024-06-22 21:15:11 +04:00
parent d615a86f33
commit 536a8bfe91
10 changed files with 258 additions and 238 deletions

View File

@ -6,10 +6,11 @@ using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail namespace BusinessLogic.Tools.Mail
{ {
public class Mail public class Mail
{ {
public IEnumerable<string> To { get; set; } = null!; public IEnumerable<string> To { get; set; } = null!;
public string Title { get; set; } = null!; public string Title { get; set; } = null!;
public string Body { get; set; } = null!; public string Body { get; set; } = null!;
} public bool IsSendable { get; set; } = true;
}
} }

View File

@ -8,39 +8,41 @@ using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail namespace BusinessLogic.Tools.Mail
{ {
public class MailSender public class MailSender
{ {
private static string _email; private static string _email;
private static string _password; private static string _password;
private static string _smtpClientHost; private static string _smtpClientHost;
private static short _smtpClientPort; private static short _smtpClientPort;
public void SetupMailOptions(MailOptions options) public void SetupMailOptions(MailOptions options)
{ {
_email = options.Email; _email = options.Email;
_password = options.Password; _password = options.Password;
_smtpClientHost = options.SmtpClientHost; _smtpClientHost = options.SmtpClientHost;
_smtpClientPort = options.SmtpClientPort; _smtpClientPort = options.SmtpClientPort;
} }
public static void Send(Mail mail) public static void Send(Mail mail)
{ {
using SmtpClient client = new SmtpClient(_smtpClientHost, _smtpClientPort); if (!mail.IsSendable) return;
client.Credentials = new NetworkCredential(_email, _password);
client.EnableSsl = true;
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); using MailMessage message = new MailMessage();
foreach (string to in mail.To)
{
message.To.Add(to);
}
message.Subject = mail.Title; message.From = new MailAddress(_email);
message.Body = mail.Body; foreach (string to in mail.To)
{
message.To.Add(to);
}
client.Send(message); message.Subject = mail.Title;
} message.Body = mail.Body;
}
client.Send(message);
}
}
} }

View File

@ -8,14 +8,18 @@ using System.Threading.Tasks;
namespace BusinessLogic.Tools.Mail.MailTemplates namespace BusinessLogic.Tools.Mail.MailTemplates
{ {
public class MailRegistration : Mail public class MailRegistration : Mail
{ {
public MailRegistration(UserBindingModel user) public MailRegistration(UserBindingModel user)
{ {
To = [user.Email]; if (user.OnlyImportantMails)
Title = "Приветствуем Вас на нашем сайте!"; {
Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" + IsSendable = false;
$"Надеемся, что Вам что-то уже приглянулось!"; }
} To = [user.Email];
} Title = "Приветствуем Вас на нашем сайте!";
Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" +
$"Надеемся, что Вам что-то уже приглянулось!";
}
}
} }

View File

@ -6,15 +6,16 @@ using System.Threading.Tasks;
namespace Contracts.BindingModels namespace Contracts.BindingModels
{ {
public class UserBindingModel public class UserBindingModel
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string FirstName { get; set; } = string.Empty; public string FirstName { get; set; } = string.Empty;
public string SecondName { get; set; } = string.Empty; public string SecondName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
public string PasswordHash { get; set; } = string.Empty; public string PasswordHash { get; set; } = string.Empty;
public string? Password { get; set; } public string? Password { get; set; }
public DateTime Birthday { get; set; } public DateTime Birthday { get; set; }
public RoleBindingModel Role { get; set; } = null!; public bool OnlyImportantMails { get; set; }
} public RoleBindingModel Role { get; set; } = null!;
}
} }

View File

@ -8,26 +8,28 @@ using System.Threading.Tasks;
namespace Contracts.Converters namespace Contracts.Converters
{ {
public static class UserConverter public static class UserConverter
{ {
public static UserViewModel ToView(UserBindingModel model) => new() public static UserViewModel ToView(UserBindingModel model) => new()
{ {
Id = model.Id, Id = model.Id,
FirstName = model.FirstName, FirstName = model.FirstName,
SecondName = model.SecondName, SecondName = model.SecondName,
Email = model.Email, Email = model.Email,
Birthday = model.Birthday, Birthday = model.Birthday,
Role = RoleConverter.ToView(model.Role), OnlyImportantMails = model.OnlyImportantMails,
}; Role = RoleConverter.ToView(model.Role),
};
public static UserBindingModel ToBinding(UserViewModel model) => new() public static UserBindingModel ToBinding(UserViewModel model) => new()
{ {
Id = model.Id, Id = model.Id,
FirstName = model.FirstName, FirstName = model.FirstName,
SecondName = model.SecondName, SecondName = model.SecondName,
Email = model.Email, Email = model.Email,
Birthday = model.Birthday, Birthday = model.Birthday,
Role = RoleConverter.ToBinding(model.Role), OnlyImportantMails = model.OnlyImportantMails,
}; Role = RoleConverter.ToBinding(model.Role),
} };
}
} }

View File

@ -6,9 +6,10 @@ using System.Threading.Tasks;
namespace Contracts.SearchModels namespace Contracts.SearchModels
{ {
public class UserSearchModel public class UserSearchModel
{ {
public Guid? Id { get; set; } public Guid? Id { get; set; }
public string? Email { get; set; } public string? Email { get; set; }
} public bool? OnlyImportantMails { get; set; }
}
} }

View File

@ -6,13 +6,14 @@ using System.Threading.Tasks;
namespace Contracts.ViewModels namespace Contracts.ViewModels
{ {
public class UserViewModel public class UserViewModel
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
public string FirstName { get; set; } = string.Empty; public string FirstName { get; set; } = string.Empty;
public string SecondName { get; set; } = string.Empty; public string SecondName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
public DateTime Birthday { get; set; } public DateTime Birthday { get; set; }
public RoleViewModel Role { get; set; } = null!; public bool OnlyImportantMails { get; set; }
} public RoleViewModel Role { get; set; } = null!;
}
} }

View File

@ -6,12 +6,13 @@ using System.Threading.Tasks;
namespace DataModels.Models namespace DataModels.Models
{ {
public interface IUser : IId public interface IUser : IId
{ {
string FirstName { get; } string FirstName { get; }
string SecondName { get; } string SecondName { get; }
string PasswordHash { get; } string PasswordHash { get; }
string Email { get; } string Email { get; }
DateTime Birthday { get; } DateTime Birthday { get; }
} bool OnlyImportantMails { get; }
}
} }

View File

@ -12,98 +12,99 @@ using System.Threading.Tasks;
namespace DatabaseImplement.Implements namespace DatabaseImplement.Implements
{ {
public class UserStorage : IUserStorage public class UserStorage : IUserStorage
{ {
public UserBindingModel? Delete(UserSearchModel model) public UserBindingModel? Delete(UserSearchModel model)
{ {
if (model.Id is null && model.Email is null) if (model.Id is null && model.Email is null)
{ {
return null; return null;
} }
var context = new Database(); var context = new Database();
var user = context.Users.FirstOrDefault(u => var user = context.Users.FirstOrDefault(u =>
(model.Id.HasValue && u.Id == model.Id) (model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))); || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)));
if (user is null) if (user is null)
{ {
return null; return null;
} }
context.Remove(user); context.Remove(user);
context.SaveChanges(); context.SaveChanges();
return user.GetBindingModel(); return user.GetBindingModel();
} }
public UserBindingModel? GetElement(UserSearchModel model) public UserBindingModel? GetElement(UserSearchModel model)
{ {
if (model.Id is null && model.Email is null) if (model.Id is null && model.Email is null)
{ {
return null; return null;
} }
var context = new Database(); var context = new Database();
return context.Users return context.Users
.Include(u => u.Role) .Include(u => u.Role)
.FirstOrDefault(u => .FirstOrDefault(u =>
(model.Id.HasValue && u.Id == model.Id) (model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))) || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email)))
?.GetBindingModel(); ?.GetBindingModel();
} }
public IEnumerable<UserBindingModel> GetList(UserSearchModel? model) public IEnumerable<UserBindingModel> GetList(UserSearchModel? model)
{ {
var context = new Database(); var context = new Database();
if (model is null) if (model is null)
{ {
return context.Users return context.Users
.Include(u => u.Role) .Include(u => u.Role)
.Select(r => r.GetBindingModel()); .Select(r => r.GetBindingModel());
} }
if (model.Id is null && model.Email is null) if (model.Id is null && model.Email is null && !model.OnlyImportantMails is null)
{ {
return []; return [];
} }
return context.Users return context.Users
.Where(u => .Where(u =>
(model.Id.HasValue && u.Id == model.Id) (model.Id.HasValue && u.Id == model.Id)
|| (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))) || (!string.IsNullOrEmpty(u.Email) && u.Email.Contains(model.Email))
.Include(u => u.Role) || (model.OnlyImportantMails.HasValue && u.OnlyImportantMails == model.OnlyImportantMails))
.Select(r => r.GetBindingModel()); .Include(u => u.Role)
} .Select(r => r.GetBindingModel());
}
public UserBindingModel? Insert(UserBindingModel model) public UserBindingModel? Insert(UserBindingModel model)
{ {
var context = new Database(); var context = new Database();
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id); var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
if (role is null) if (role is null)
{ {
return null; return null;
} }
var newUser = Models.User.ToUserFromBinding(model, role); var newUser = Models.User.ToUserFromBinding(model, role);
context.Users.Add(newUser); context.Users.Add(newUser);
context.SaveChanges(); context.SaveChanges();
return newUser.GetBindingModel(); return newUser.GetBindingModel();
} }
public UserBindingModel? Update(UserBindingModel model) public UserBindingModel? Update(UserBindingModel model)
{ {
var context = new Database(); var context = new Database();
var user = context.Users var user = context.Users
.FirstOrDefault(u => u.Id == model.Id); .FirstOrDefault(u => u.Id == model.Id);
var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id); var role = context.Roles.FirstOrDefault(r => r.Id == model.Role.Id);
if (user is null || role is null) if (user is null || role is null)
{ {
return null; return null;
} }
user.Update(model, role); user.Update(model, role);
context.SaveChanges(); context.SaveChanges();
return user.GetBindingModel(); return user.GetBindingModel();
} }
} }
} }

View File

@ -4,6 +4,7 @@ using Contracts.ViewModels;
using DataModels.Models; using DataModels.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -11,72 +12,77 @@ using System.Threading.Tasks;
namespace DatabaseImplement.Models namespace DatabaseImplement.Models
{ {
public class User : IUser public class User : IUser
{ {
public Guid Id { get; set; } public Guid Id { get; set; }
[Required] [Required]
public string FirstName { get; set; } = string.Empty; public string FirstName { get; set; } = string.Empty;
[Required] [Required]
public string SecondName { get; set; } = string.Empty; public string SecondName { get; set; } = string.Empty;
[Required] [Required]
public string PasswordHash { get; set; } = string.Empty; public string PasswordHash { get; set; } = string.Empty;
[Required] [Required]
public string Email { get; set; } = string.Empty; public string Email { get; set; } = string.Empty;
[Required] [Required]
public DateTime Birthday { get; set; } public DateTime Birthday { get; set; }
public Role? Role { get; set; } public Role? Role { get; set; }
public UserBindingModel GetBindingModel() => new() public bool OnlyImportantMails { get; set; } = false;
{
Id = Id,
FirstName = FirstName,
SecondName = SecondName,
Email = Email,
PasswordHash = PasswordHash,
Birthday = Birthday,
Role = Role?.GetBindingModel() ?? new()
};
public static User ToUserFromView(UserViewModel model, Role role) => new() public UserBindingModel GetBindingModel() => new()
{ {
Id = model.Id, Id = Id,
FirstName = model.FirstName, FirstName = FirstName,
SecondName = model.SecondName, SecondName = SecondName,
Email = model.Email, Email = Email,
Birthday = model.Birthday, PasswordHash = PasswordHash,
Role = role Birthday = Birthday,
}; OnlyImportantMails = OnlyImportantMails,
Role = Role?.GetBindingModel() ?? new()
};
public static User ToUserFromBinding(UserBindingModel model, Role role) => new() public static User ToUserFromView(UserViewModel model, Role role) => new()
{ {
Id = model.Id, Id = model.Id,
FirstName = model.FirstName, FirstName = model.FirstName,
SecondName = model.SecondName, SecondName = model.SecondName,
Email = model.Email, Email = model.Email,
PasswordHash = model.PasswordHash, Birthday = model.Birthday,
Birthday = model.Birthday, Role = role
Role = role };
};
public void Update(UserBindingModel model, Role role) public static User ToUserFromBinding(UserBindingModel model, Role role) => new()
{ {
if (model is null) Id = model.Id,
{ FirstName = model.FirstName,
throw new ArgumentNullException("Update user: binding model is null"); SecondName = model.SecondName,
} Email = model.Email,
PasswordHash = model.PasswordHash,
Birthday = model.Birthday,
OnlyImportantMails = model.OnlyImportantMails,
Role = role
};
Email = model.Email; public void Update(UserBindingModel model, Role role)
FirstName = model.FirstName; {
SecondName = model.SecondName; if (model is null)
PasswordHash = model.PasswordHash; {
Birthday = model.Birthday; throw new ArgumentNullException("Update user: binding model is null");
Role = role; }
}
} 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;
}
}
} }