62 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.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();
}
}
}