63 lines
1.8 KiB
C#
Raw 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 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();
}
}
}