124 lines
4.1 KiB
C#
124 lines
4.1 KiB
C#
using EventVisitorLogic.BindingModels;
|
|
using EventVisitorLogic.StoragesContracts;
|
|
using EventVisitorLogic.ViewModels;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace EventVisitorLogic.Logic
|
|
{
|
|
public class EventLogic : IEventLogic
|
|
{
|
|
private readonly IEventStorage _eventStorage;
|
|
public EventLogic(IEventStorage eventStorage)
|
|
{
|
|
_eventStorage = eventStorage;
|
|
}
|
|
public bool Create(EventBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
var result = _eventStorage.Insert(model);
|
|
|
|
if (result == null)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public bool Delete(EventBindingModel model)
|
|
{
|
|
CheckModel(model, false);
|
|
if (_eventStorage.Delete(model) == null)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
public EventViewModel? ReadElement(EventBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
var element = _eventStorage.GetElement(model);
|
|
if (element == null)
|
|
{
|
|
return null;
|
|
}
|
|
return element;
|
|
}
|
|
|
|
public List<EventViewModel>? ReadList(EventBindingModel? model)
|
|
{
|
|
var list = model == null ? _eventStorage.GetFullList() : _eventStorage.GetFilteredList(model);
|
|
if (list == null)
|
|
{
|
|
return null;
|
|
}
|
|
return list;
|
|
}
|
|
|
|
public bool Update(EventBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_eventStorage.Update(model) == null)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private void CheckModel(EventBindingModel 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.Type))
|
|
{
|
|
throw new ArgumentNullException("Нет типа мероприятия", nameof(model.Type));
|
|
}
|
|
if (string.IsNullOrEmpty(model.ContactPhone))
|
|
{
|
|
throw new ArgumentNullException("Нет контактного телефона", nameof(model.ContactPhone));
|
|
}
|
|
if (string.IsNullOrEmpty(model.Address))
|
|
{
|
|
throw new ArgumentNullException("Нет адреса мероприятия", nameof(model.Address));
|
|
}
|
|
if (string.IsNullOrEmpty(model.City))
|
|
{
|
|
throw new ArgumentNullException("Нет города", nameof(model.City));
|
|
}
|
|
if (string.IsNullOrEmpty(model.Status))
|
|
{
|
|
throw new ArgumentNullException("Нет статуса меропрития", nameof(model.Status));
|
|
}
|
|
if (model.CountVisitors < 0)
|
|
{
|
|
throw new ArgumentNullException("Некорректный количество посетителей, nameof(model.CountVisitors)");
|
|
}
|
|
if (model.TimeStart <= DateTime.Now.AddHours(2))
|
|
{
|
|
throw new ArgumentNullException("Нельзя выбрать время меньше, чем через 2 часа от текущего", nameof(model.Date));
|
|
}
|
|
if (model.TimeEnd <= DateTime.Now)
|
|
{
|
|
throw new ArgumentNullException("Нельзя выбрать время меньше, чем через 2 часа от текущего", nameof(model.Date));
|
|
}
|
|
}
|
|
}
|
|
}
|