158 lines
5.2 KiB
C#
158 lines
5.2 KiB
C#
using EventVisitorLogic.BindingModels;
|
||
using EventVisitorLogic.StoragesContracts;
|
||
using EventVisitorLogic.ViewModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Cryptography;
|
||
using System.Text;
|
||
using System.Text.RegularExpressions;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace EventVisitorLogic.Logic
|
||
{
|
||
public class OrganizerLogic : IOrganizerLogic
|
||
{
|
||
private readonly IOrganizerStorage _organizerStorage;
|
||
public OrganizerLogic(IOrganizerStorage organizerStorage)
|
||
{
|
||
_organizerStorage = organizerStorage;
|
||
}
|
||
|
||
private string EncryptPassword(string password)
|
||
{
|
||
byte[] hashedBytes = SHA256.HashData(Encoding.UTF8.GetBytes(password));
|
||
return Convert.ToBase64String(hashedBytes);
|
||
}
|
||
|
||
public bool Create(OrganizerBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
model.Password = EncryptPassword(model.Password);
|
||
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));
|
||
}
|
||
|
||
var element = _organizerStorage.GetElement(model);
|
||
if (element != null)
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
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));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Email))
|
||
{
|
||
throw new ArgumentNullException("Нет почты", nameof(model.Email));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Password))
|
||
{
|
||
throw new ArgumentNullException("Нет пароля", nameof(model.Password));
|
||
}
|
||
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("Пользователь с такой почтой уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|