2024-02-18 12:22:10 +04:00
using ComputersShopContracts.BindingModels ;
using ComputersShopContracts.BusinessLogicContracts ;
using ComputersShopContracts.SearchModels ;
using ComputersShopContracts.StoragesContracts ;
using ComputersShopContracts.ViewModels ;
using ComputersShopDataModels.Models ;
using Microsoft.Extensions.Logging ;
using System ;
using System.Collections.Generic ;
using System.Linq ;
2024-03-18 20:59:24 +04:00
using System.Reflection ;
2024-02-18 12:22:10 +04:00
using System.Text ;
using System.Threading.Tasks ;
namespace ComputersShopBusinessLogic.BusinessLogics
{
public class ShopLogic : IShopLogic
{
private readonly ILogger _logger ;
private readonly IShopStorage _shopStorage ;
public ShopLogic ( ILogger < ShopLogic > logger , IShopStorage ShopStorage )
{
_logger = logger ;
_shopStorage = ShopStorage ;
}
public bool AddComputer ( ShopSearchModel model , IComputerModel computer , int quantity )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
if ( quantity < = 0 )
{
2024-03-18 20:59:24 +04:00
return false ;
2024-02-18 12:22:10 +04:00
throw new ArgumentException ( "Количество добавляемого изделия должно быть больше 0" , nameof ( quantity ) ) ;
}
_logger . LogInformation ( "AddComputerInShop. ShopName:{ShopName}.Id:{ Id}" , model . ShopName , model . Id ) ;
var element = _shopStorage . GetElement ( model ) ;
if ( element = = null )
{
_logger . LogWarning ( "AddComputerInShop element not found" ) ;
return false ;
}
2024-03-18 20:59:24 +04:00
if ( element . Capacity - element . Computers . Select ( x = > x . Value . Item2 ) . Sum ( ) < quantity )
{
throw new ArgumentNullException ( "В магазине не хватает места" , nameof ( quantity ) ) ;
}
2024-02-18 12:22:10 +04:00
if ( element . Computers . TryGetValue ( computer . Id , out var pair ) )
{
element . Computers [ computer . Id ] = ( computer , quantity + pair . Item2 ) ;
_logger . LogInformation ( "AddComputerInShop. Has been added {quantity} {Computer} in {ShopName}" , quantity , computer . ComputerName , element . ShopName ) ;
}
else
{
element . Computers [ computer . Id ] = ( computer , quantity ) ;
_logger . LogInformation ( "AddPastryInShop. Has been added {quantity} new Computer {Computer} in {ShopName}" , quantity , computer . ComputerName , element . ShopName ) ;
}
_shopStorage . Update ( new ( )
{
Id = element . Id ,
ShopAddress = element . ShopAddress ,
ShopName = element . ShopName ,
DateOpening = element . DateOpening ,
2024-03-18 20:59:24 +04:00
Computers = element . Computers ,
Capacity = element . Capacity ,
2024-02-18 12:22:10 +04:00
} ) ;
return true ;
}
2024-03-18 20:59:24 +04:00
public bool AddComputers ( IComputerModel computer , int quantity )
{
if ( computer = = null )
{
throw new ArgumentNullException ( nameof ( computer ) ) ;
}
if ( quantity < = 0 )
{
throw new ArgumentException ( "Количество документов должно быть больше 0" , nameof ( quantity ) ) ;
}
_logger . LogInformation ( "AddComputers. ShopName:{ShopName}. Id:{Id}" , computer . ComputerName , computer . Id ) ;
var allFreeQuantity = _shopStorage . GetFullList ( ) . Select ( x = > x . Capacity - x . Computers . Select ( x = > x . Value . Item2 ) . Sum ( ) ) . Sum ( ) ;
if ( allFreeQuantity < quantity )
{
_logger . LogWarning ( "AddComputers operation failed." ) ;
return false ;
}
foreach ( var shop in _shopStorage . GetFullList ( ) )
{
int freeQuantity = shop . Capacity - shop . Computers . Select ( x = > x . Value . Item2 ) . Sum ( ) ;
if ( freeQuantity < = 0 )
{
continue ;
}
if ( freeQuantity < quantity )
{
if ( ! AddComputer ( new ( ) { Id = shop . Id } , computer , freeQuantity ) )
{
_logger . LogWarning ( "AddComputers operation failed." ) ;
return false ;
}
quantity - = freeQuantity ;
}
else
{
if ( ! AddComputer ( new ( ) { Id = shop . Id } , computer , quantity ) )
{
_logger . LogWarning ( "AddComputers operation failed." ) ;
return false ;
}
quantity = 0 ;
}
if ( quantity = = 0 )
{
return true ;
}
}
_logger . LogWarning ( "AddComputers operation failed." ) ;
return false ;
}
2024-02-18 12:22:10 +04:00
public bool Create ( ShopBindingModel model )
{
CheckModel ( model ) ;
model . Computers = new ( ) ;
if ( _shopStorage . Insert ( model ) = = null )
{
_logger . LogWarning ( "Insert operation failed" ) ;
return false ;
}
return true ;
}
public bool Delete ( ShopBindingModel model )
{
CheckModel ( model , false ) ;
_logger . LogInformation ( "Delete. Id:{Id}" , model . Id ) ;
if ( _shopStorage . Delete ( model ) = = null )
{
_logger . LogWarning ( "Delete operation failed" ) ;
return false ;
}
return true ;
}
public ShopViewModel ? ReadElement ( ShopSearchModel model )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
_logger . LogInformation ( "ReadElement. ShopName:{ShopName}.Id:{ Id}" , model . ShopName , model . Id ) ;
var element = _shopStorage . GetElement ( model ) ;
if ( element = = null )
{
_logger . LogWarning ( "ReadElement element not found" ) ;
return null ;
}
_logger . LogInformation ( "ReadElement find. Id:{Id}" , element . Id ) ;
return element ;
}
public List < ShopViewModel > ? ReadList ( ShopSearchModel ? model )
{
_logger . LogInformation ( "ReadList. ShopName:{ShopName}.Id:{ Id} " , model ? . ShopName , model ? . Id ) ;
var list = ( model = = null ) ? _shopStorage . GetFullList ( ) : _shopStorage . GetFilteredList ( model ) ;
if ( list = = null )
{
_logger . LogWarning ( "ReadList return null list" ) ;
return null ;
}
_logger . LogInformation ( "ReadList. Count:{Count}" , list . Count ) ;
return list ;
}
2024-03-18 20:59:24 +04:00
public bool SellComputers ( IComputerModel computer , int quantity )
{
return _shopStorage . SellComputers ( computer , quantity ) ;
}
2024-02-18 12:22:10 +04:00
public bool Update ( ShopBindingModel model )
{
CheckModel ( model , false ) ;
if ( string . IsNullOrEmpty ( model . ShopName ) )
{
throw new ArgumentNullException ( "Нет названия магазина" , nameof ( model . ShopName ) ) ;
}
if ( _shopStorage . Update ( model ) = = null )
{
_logger . LogWarning ( "Update operation failed" ) ;
return false ;
}
return true ;
}
private void CheckModel ( ShopBindingModel model , bool withParams = true )
{
if ( model = = null )
{
throw new ArgumentNullException ( nameof ( model ) ) ;
}
if ( ! withParams )
{
return ;
}
if ( string . IsNullOrEmpty ( model . ShopName ) )
{
throw new ArgumentNullException ( "Нет названия магазина" , nameof ( model . ShopName ) ) ;
}
_logger . LogInformation ( "Shop. ShopName:{0}.ShopAdress:{1}. Id: {2}" , model . ShopName , model . ShopAddress , model . Id ) ;
var element = _shopStorage . GetElement ( new ShopSearchModel
{
ShopName = model . ShopName
} ) ;
if ( element ! = null & & element . Id ! = model . Id & & element . ShopName = = model . ShopName )
{
throw new InvalidOperationException ( "Магазин с таким названием уже есть" ) ;
}
}
}
2024-03-18 20:59:24 +04:00
}