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

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

View File

@ -25,6 +25,8 @@ namespace BusinessLogic.Tools.Mail
public static void Send(Mail mail)
{
if (!mail.IsSendable) return;
using SmtpClient client = new SmtpClient(_smtpClientHost, _smtpClientPort);
client.Credentials = new NetworkCredential(_email, _password);
client.EnableSsl = true;

View File

@ -12,6 +12,10 @@ namespace BusinessLogic.Tools.Mail.MailTemplates
{
public MailRegistration(UserBindingModel user)
{
if (user.OnlyImportantMails)
{
IsSendable = false;
}
To = [user.Email];
Title = "Приветствуем Вас на нашем сайте!";
Body = $"Спасибо, {user.SecondName} {user.FirstName}, что выбрали НАС.\n" +

View File

@ -15,6 +15,7 @@ namespace Contracts.BindingModels
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!;
}
}

View File

@ -17,6 +17,7 @@ namespace Contracts.Converters
SecondName = model.SecondName,
Email = model.Email,
Birthday = model.Birthday,
OnlyImportantMails = model.OnlyImportantMails,
Role = RoleConverter.ToView(model.Role),
};
@ -27,6 +28,7 @@ namespace Contracts.Converters
SecondName = model.SecondName,
Email = model.Email,
Birthday = model.Birthday,
OnlyImportantMails = model.OnlyImportantMails,
Role = RoleConverter.ToBinding(model.Role),
};
}

View File

@ -10,5 +10,6 @@ namespace Contracts.SearchModels
{
public Guid? Id { get; set; }
public string? Email { get; set; }
public bool? OnlyImportantMails { get; set; }
}
}

View File

@ -13,6 +13,7 @@ namespace Contracts.ViewModels
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!;
}
}

View File

@ -13,5 +13,6 @@ namespace DataModels.Models
string PasswordHash { get; }
string Email { get; }
DateTime Birthday { get; }
bool OnlyImportantMails { get; }
}
}

View File

@ -60,14 +60,15 @@ namespace DatabaseImplement.Implements
.Include(u => u.Role)
.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 context.Users
.Where(u =>
(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))
|| (model.OnlyImportantMails.HasValue && u.OnlyImportantMails == model.OnlyImportantMails))
.Include(u => u.Role)
.Select(r => r.GetBindingModel());
}

View File

@ -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;
@ -32,6 +33,8 @@ namespace DatabaseImplement.Models
public Role? Role { get; set; }
public bool OnlyImportantMails { get; set; } = false;
public UserBindingModel GetBindingModel() => new()
{
Id = Id,
@ -40,6 +43,7 @@ namespace DatabaseImplement.Models
Email = Email,
PasswordHash = PasswordHash,
Birthday = Birthday,
OnlyImportantMails = OnlyImportantMails,
Role = Role?.GetBindingModel() ?? new()
};
@ -61,6 +65,7 @@ namespace DatabaseImplement.Models
Email = model.Email,
PasswordHash = model.PasswordHash,
Birthday = model.Birthday,
OnlyImportantMails = model.OnlyImportantMails,
Role = role
};
@ -71,12 +76,13 @@ namespace DatabaseImplement.Models
throw new ArgumentNullException("Update user: binding model is null");
}
Email = model.Email;
FirstName = model.FirstName;
SecondName = model.SecondName;
PasswordHash = model.PasswordHash;
Email = model.Email ?? Email;
FirstName = model.FirstName ?? FirstName;
SecondName = model.SecondName ?? SecondName;
PasswordHash = model.PasswordHash ?? PasswordHash;
Birthday = model.Birthday;
Role = role;
OnlyImportantMails = model.OnlyImportantMails;
Role = role ?? Role;
}
}
}