This commit is contained in:
kagbie3nn@mail.ru 2024-05-31 12:40:15 +04:00
parent 19606c2a5c
commit 4a65ea5456
8 changed files with 221 additions and 2 deletions

View File

@ -0,0 +1,114 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.BuisnessLogicsContracts;
using ZooContracts.BusinessLogicsContracts;
using ZooContracts.SearchModels;
using ZooContracts.StorageContracts;
using ZooContracts.ViewModels;
namespace ZooBusinessLogic.BusinessLogic
{
public class CostLogic : ICostLogic
{
private readonly ILogger _logger;
private readonly ICostStorage _CostStorage;
public CostLogic(ILogger<CostLogic> logger, ICostStorage CostStorage)
{
_logger = logger;
_CostStorage = CostStorage;
}
public List<CostViewModel>? ReadList(CostSearchModel? model)
{
_logger.LogInformation("ReadList. CostName:{CostName}. Id:{ Id}", model?.CostName, model?.Id);
var list = model == null ? _CostStorage.GetFullList() : _CostStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public CostViewModel? ReadElement(CostSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. CostName:{CostName}.Id:{ Id}", model.CostName, model.Id);
var element = _CostStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(CostBindingModel model)
{
CheckModel(model);
if (_CostStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(CostBindingModel model)
{
CheckModel(model);
if (_CostStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(CostBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_CostStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(CostBindingModel model, bool withParams =
true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.CostPrice <= 0)
{
throw new ArgumentNullException("сумма затрат должна быть больше 0",
nameof(model.CostPrice));
}
_logger.LogInformation("Cost. CostName:{CostName}.CostPrice:{CostPrice} Id: { Id}", model.CostName, model.CostPrice, model.Id);
var element = _CostStorage.GetElement(new CostSearchModel
{
CostName = model.CostName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("статья затрат с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooDataModels.Models;
namespace ZooContracts.BindingModels
{
public class CostBindingModel : ICostModel
{
public int Id { get; set; }
public string CostName { get; set; } = string.Empty;
public double CostPrice { get; set; }
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModels;
using ZooContracts.ViewModels;
namespace ZooContracts.BusinessLogicsContracts
{
public interface ICostLogic
{
List<CostViewModel>? ReadList(CostSearchModel? model);
CostViewModel? ReadElement(CostSearchModel model);
bool Create(CostBindingModel model);
bool Update(CostBindingModel model);
bool Delete(CostBindingModel model);
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZooContracts.SearchModels
{
public class CostSearchModel
{
public int? Id { get; set; }
public string? CostName { get; set; }
public double CostPrice { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooContracts.BindingModels;
using ZooContracts.SearchModels;
using ZooContracts.ViewModels;
namespace ZooContracts.StorageContracts
{
public interface ICostStorage
{
List<CostViewModel> GetFullList();
List<CostViewModel> GetFilteredList(CostSearchModel model);
CostViewModel? GetElement(CostSearchModel model);
CostViewModel? Insert(CostBindingModel model);
CostViewModel? Update(CostBindingModel model);
CostViewModel? Delete(CostBindingModel model);
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZooDataModels.Models;
namespace ZooContracts.ViewModels
{
public class CostViewModel : ICostModel
{
public int Id { get; set; }
[DisplayName("название статьи затрат")]
public string CostName { get; set; } = string.Empty;
[DisplayName("сумма затраты")]
public double CostPrice { get; set; }
}
}

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ZooDataModels.Models
{
public interface ICostModel : IId
{
string CostName { get; }
double CostPrice { get; }
}
}

View File

@ -7,8 +7,8 @@ using ZooDataModels.Enums;
namespace ZooDataModels.Models
{
public interface IRouteModel
{
public interface IRouteModel : IId
{
int ClientId { get; }
string RouteName { get; }
double RoutePrice { get; }