2024-05-05 23:59:47 +04:00
|
|
|
|
using CarShowroomContracts.BusinessLogic;
|
|
|
|
|
using CarShowroomContracts.StorageContracts;
|
|
|
|
|
using CarShowroomDataModels.Dtos;
|
|
|
|
|
using CarShowroomDataModels.SearchModel;
|
|
|
|
|
using CarShowroomDataModels.Views;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace CarShowroomBusinessLogic.BusinessLogic
|
|
|
|
|
{
|
|
|
|
|
public class SaleLogic : ISaleLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly ISaleStorage _saleStorage;
|
|
|
|
|
|
|
|
|
|
public SaleLogic(ISaleStorage storage)
|
|
|
|
|
{
|
|
|
|
|
_saleStorage = storage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<SaleView>? ReadList(SaleSearch? model)
|
|
|
|
|
{
|
|
|
|
|
var list = model == null ? _saleStorage.GetFullList() : _saleStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SaleView? ReadElement(SaleSearch model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
var element = _saleStorage.GetElement(model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Create(SaleDto model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
2024-05-13 15:58:08 +04:00
|
|
|
|
model.SaleTime = DateTime.UtcNow;
|
2024-05-05 23:59:47 +04:00
|
|
|
|
if (_saleStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Update(SaleDto model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
if (_saleStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Delete(SaleDto model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
if (_saleStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CheckModel(SaleDto model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
if (!withParams)
|
|
|
|
|
return;
|
2024-05-13 15:21:18 +04:00
|
|
|
|
if (model.Price < 0)
|
2024-05-05 23:59:47 +04:00
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
if (model.EmployeeId < 0)
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
if (model.ClientId < 0)
|
|
|
|
|
throw new InvalidOperationException();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|