KOP_Library_v5/BusinessLogic/AuthorLogic.cs

66 lines
1.4 KiB
C#

using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.StorageContracts;
using Contracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BusinessLogic
{
public class AuthorLogic:IAuthorLogic
{
private readonly IAuthorStorage _authorStorage;
public AuthorLogic(IAuthorStorage authorStorage)
{
_authorStorage = authorStorage;
}
public void CreateOrUpdate(AuthorBindingModel model)
{
var element = _authorStorage.GetElement(
new AuthorBindingModel
{
FIO = model.FIO
});
if (element != null && element.Id != model.Id)
{
throw new Exception("Такой автор уже существует");
}
if (model.Id.HasValue)
{
_authorStorage.Update(model);
}
else
{
_authorStorage.Insert(model);
}
}
public void Delete(AuthorBindingModel model)
{
var element = _authorStorage.GetElement(new AuthorBindingModel { Id = model.Id });
if (element == null)
{
throw new Exception("Автор не найден");
}
_authorStorage.Delete(model);
}
public List<AuthorViewModel> Read(AuthorBindingModel model)
{
if (model == null)
{
return _authorStorage.GetFullList();
}
if (model.Id.HasValue)
{
return new List<AuthorViewModel> { _authorStorage.GetElement(model) };
}
return _authorStorage.GetFilteredList(model);
}
}
}