62 lines
1.8 KiB
C#
Raw Normal View History

using BookContract.BindingModels;
using BookContract.BusinessLogicContracts;
using BookContract.StorageContracts;
using BookContract.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace BookBusinessLogic.BusinessLogic
{
public class BookLogic : IBookLogic
{
private readonly IBookStorage _bookStorage;
public BookLogic(IBookStorage bookStorage)
{
_bookStorage = bookStorage;
}
public void CreateOrUpdate(BookBindingModel model)
{
var element = _bookStorage.GetElement(new BookBindingModel { Title = model.Title });
if (element != null && element.Id != model.Id) {
throw new Exception("Книги с таким названием тут нет");
}
if (model.Id.HasValue)
{
_bookStorage.Update(model);
}
else
{
_bookStorage.Insert(model);
}
}
public void Delete(BookBindingModel model)
{
var element = _bookStorage.GetElement(new BookBindingModel { Title = model.Title });
if (element == null)
{
throw new Exception("Книги с таким названием тут нет");
}
_bookStorage.Delete(model);
}
public List<BookViewModel> Read(BookBindingModel model)
{
if(model == null)
return _bookStorage.GetFullList();
if (model.Id.HasValue) {
return new List<BookViewModel> {
_bookStorage.GetElement(model)
};
}
return _bookStorage.GetFullList();
}
}
}