2024-11-03 11:19:43 +04:00
|
|
|
|
using EventVisitorLogic.BindingModels;
|
|
|
|
|
using EventVisitorLogic.StoragesContracts;
|
|
|
|
|
using EventVisitorLogic.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2024-11-09 23:28:41 +04:00
|
|
|
|
using System.Security.Cryptography;
|
2024-11-03 11:19:43 +04:00
|
|
|
|
using System.Text;
|
2024-11-09 23:28:41 +04:00
|
|
|
|
using System.Text.RegularExpressions;
|
2024-11-03 11:19:43 +04:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace EventVisitorLogic.Logic
|
|
|
|
|
{
|
|
|
|
|
public class OrganizerLogic : IOrganizerLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly IOrganizerStorage _organizerStorage;
|
|
|
|
|
public OrganizerLogic(IOrganizerStorage organizerStorage)
|
|
|
|
|
{
|
|
|
|
|
_organizerStorage = organizerStorage;
|
|
|
|
|
}
|
2024-11-09 23:28:41 +04:00
|
|
|
|
|
|
|
|
|
private string EncryptPassword(string password)
|
|
|
|
|
{
|
|
|
|
|
byte[] hashedBytes = SHA256.HashData(Encoding.UTF8.GetBytes(password));
|
|
|
|
|
return Convert.ToBase64String(hashedBytes);
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 11:19:43 +04:00
|
|
|
|
public bool Create(OrganizerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-11-09 23:28:41 +04:00
|
|
|
|
model.Password = EncryptPassword(model.Password);
|
2024-11-03 11:19:43 +04:00
|
|
|
|
var result = _organizerStorage.Insert(model);
|
|
|
|
|
|
|
|
|
|
if (result == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(OrganizerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
if (_organizerStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public OrganizerViewModel? ReadElement(OrganizerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
2024-11-09 23:28:41 +04:00
|
|
|
|
|
2024-11-03 11:19:43 +04:00
|
|
|
|
var element = _organizerStorage.GetElement(model);
|
2024-11-09 23:28:41 +04:00
|
|
|
|
if (element != null)
|
2024-11-03 11:19:43 +04:00
|
|
|
|
{
|
2024-11-09 23:28:41 +04:00
|
|
|
|
string hashedPassword = element.Password;
|
|
|
|
|
if (element != null && model.Password != element.Password && model.Password != null)
|
|
|
|
|
{
|
|
|
|
|
hashedPassword = EncryptPassword(model.Password);
|
|
|
|
|
}
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (element.Password == hashedPassword)
|
|
|
|
|
{
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-03 11:19:43 +04:00
|
|
|
|
}
|
2024-11-09 23:28:41 +04:00
|
|
|
|
return null;
|
2024-11-03 11:19:43 +04:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 22:09:38 +04:00
|
|
|
|
public OrganizerViewModel? ReadElementEmail(OrganizerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var element = _organizerStorage.GetElement(model);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-03 11:19:43 +04:00
|
|
|
|
public List<OrganizerViewModel>? ReadList(OrganizerBindingModel? model)
|
|
|
|
|
{
|
|
|
|
|
//var list = model == null ? _organizerStorage.GetFullList() : _organizerStorage.GetFilteredList(model);
|
|
|
|
|
var list = _organizerStorage.GetFullList();
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(OrganizerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_organizerStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(OrganizerBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет имени", nameof(model.Name));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Surname))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет фамилии", nameof(model.Surname));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.OrganizationName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет названия компании", nameof(model.OrganizationName));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Phone))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Нет телефона", nameof(model.Phone));
|
|
|
|
|
}
|
2024-11-03 19:42:19 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Email))
|
2024-11-03 11:19:43 +04:00
|
|
|
|
{
|
2024-11-03 19:42:19 +04:00
|
|
|
|
throw new ArgumentNullException("Нет почты", nameof(model.Email));
|
2024-11-03 11:19:43 +04:00
|
|
|
|
}
|
2024-11-03 19:42:19 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Password))
|
2024-11-03 11:19:43 +04:00
|
|
|
|
{
|
2024-11-03 19:42:19 +04:00
|
|
|
|
throw new ArgumentNullException("Нет пароля", nameof(model.Password));
|
2024-11-03 11:19:43 +04:00
|
|
|
|
}
|
2024-11-09 23:28:41 +04:00
|
|
|
|
if (!Regex.IsMatch(model.Email, @"^[^@\s]+@[^@\s]+\.[^@\s]+$", RegexOptions.IgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Неправильно введенный email", nameof(model.Email));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Regex.IsMatch(model.Password, @"^^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$", RegexOptions.IgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Неправильно введенный пароль", nameof(model.Password));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var element = _organizerStorage.GetElement(new OrganizerBindingModel
|
|
|
|
|
{
|
|
|
|
|
Email = model.Email
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Пользователь с такой почтой уже есть");
|
|
|
|
|
}
|
2024-11-03 11:19:43 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|