SUBD-Petrushin-Egor-PIbd-22/TaskTrackerBusinessLogics/BusinessLogic/OrganizationLogic.cs
2024-05-13 14:29:34 +04:00

104 lines
3.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TaskTrackerContracts.BindingModels;
using TaskTrackerContracts.BusinessLogicsContracts;
using TaskTrackerContracts.SearchModels;
using TaskTrackerContracts.StoragesContracts;
using TaskTrackerContracts.ViewModels;
namespace TaskTrackerBusinessLogics.BusinessLogic
{
public class OrganizationLogic : IOrganizationLogic
{
private readonly IOrganizationStorage _organizationStorage;
public OrganizationLogic(IOrganizationStorage organizationStorage)
{
_organizationStorage = organizationStorage;
}
public List<OrganizationViewModel>? ReadList(OrganizationSearchModel? model)
{
var list = model == null ? _organizationStorage.GetFullList() : _organizationStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public OrganizationViewModel? ReadElement(OrganizationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _organizationStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(OrganizationBindingModel model)
{
CheckModel(model);
if (_organizationStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(OrganizationBindingModel model)
{
CheckModel(model);
if (_organizationStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(OrganizationBindingModel model)
{
CheckModel(model, false);
if (_organizationStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(OrganizationBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.OrganizationName))
{
throw new ArgumentNullException("Нет названия", nameof(model.OrganizationName));
}
var element = _organizationStorage.GetElement(new OrganizationSearchModel
{
OrganizationName = model.OrganizationName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Организация с таким названием уже есть");
}
}
}
}