63 lines
1.8 KiB
C#
63 lines
1.8 KiB
C#
using BookContract.BindingModels;
|
||
using BookContract.BusinessLogicContracts;
|
||
using BookContract.StorageContracts;
|
||
using BookContract.ViewModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace BookBusinessLogic.BusinessLogic
|
||
{
|
||
public class ReaderLogic : IReaderLogic
|
||
{
|
||
private readonly IReaderStorage _readerStorage;
|
||
|
||
public ReaderLogic(IReaderStorage readerStorage)
|
||
{
|
||
_readerStorage = readerStorage;
|
||
}
|
||
|
||
public void CreateOrUpdate(ReaderBindingModel model)
|
||
{
|
||
var element = _readerStorage.GetElement(new ReaderBindingModel { Name = model.Name });
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new Exception("Книги с таким названием тут нет");
|
||
}
|
||
if (model.Id.HasValue)
|
||
{
|
||
_readerStorage.Update(model);
|
||
}
|
||
else
|
||
{
|
||
_readerStorage.Insert(model);
|
||
}
|
||
}
|
||
|
||
public void Delete(ReaderBindingModel model)
|
||
{
|
||
var element = _readerStorage.GetElement(new ReaderBindingModel { Name = model.Name });
|
||
if (element == null)
|
||
{
|
||
throw new Exception("Книги с таким названием тут нет");
|
||
}
|
||
_readerStorage.Delete(model);
|
||
}
|
||
|
||
public List<ReaderViewModel> Read(ReaderBindingModel model)
|
||
{
|
||
if (model == null)
|
||
return _readerStorage.GetFullList();
|
||
if (model.Id.HasValue)
|
||
{
|
||
return new List<ReaderViewModel> {
|
||
_readerStorage.GetElement(model)
|
||
};
|
||
}
|
||
return _readerStorage.GetFullList();
|
||
}
|
||
}
|
||
}
|