work2
This commit is contained in:
parent
b6635051dc
commit
90023dfdac
@ -7,18 +7,18 @@ using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace BusinessLogics.BusinessLogic
|
||||
{
|
||||
public class OrderLogic : ICostLogic
|
||||
public class OrderLogic : IOrderLogic
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ICostStorage _costStorage;
|
||||
private readonly IOrderStorage _orderStorage;
|
||||
|
||||
public OrderLogic(ILogger<OrderLogic> logger, ICostStorage costStorage)
|
||||
public OrderLogic(ILogger<OrderLogic> logger, IOrderStorage orderStorage)
|
||||
{
|
||||
_logger = logger;
|
||||
_costStorage = costStorage;
|
||||
_orderStorage = orderStorage;
|
||||
}
|
||||
|
||||
public void CheckModel(CostBindingModel model, bool checkParams = true)
|
||||
public void CheckModel(OrderBindingModel model, bool checkParams = true)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
@ -29,7 +29,7 @@ namespace BusinessLogics.BusinessLogic
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrEmpty(model.NameOfCost))
|
||||
if (string.IsNullOrEmpty(model.NameOrder))
|
||||
{
|
||||
throw new ArgumentNullException($"Имя затраты не должно быть пустым");
|
||||
}
|
||||
@ -40,12 +40,12 @@ namespace BusinessLogics.BusinessLogic
|
||||
}
|
||||
}
|
||||
|
||||
public bool Create(CostBindingModel model)
|
||||
public bool Create(OrderBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
CheckModel(model);
|
||||
var result = _costStorage.Insert(model);
|
||||
var result = _orderStorage.Insert(model);
|
||||
if (result == null)
|
||||
{
|
||||
throw new ArgumentNullException($"Затрата не создана");
|
||||
@ -60,12 +60,12 @@ namespace BusinessLogics.BusinessLogic
|
||||
}
|
||||
}
|
||||
|
||||
public bool Delete(CostBindingModel model)
|
||||
public bool Delete(OrderBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
CheckModel(model, false);
|
||||
var result = _costStorage.Delete(model);
|
||||
var result = _orderStorage.Delete(model);
|
||||
if (result == null)
|
||||
{
|
||||
throw new ArgumentNullException($"Удалить затрату не удалось");
|
||||
@ -80,11 +80,11 @@ namespace BusinessLogics.BusinessLogic
|
||||
}
|
||||
}
|
||||
|
||||
public CostViewModel ReadElement(CostSearchModel model)
|
||||
public OrderViewModel ReadElement(OrderSearchModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = _costStorage.GetElement(model);
|
||||
var result = _orderStorage.GetElement(model);
|
||||
if (result == null)
|
||||
{
|
||||
throw new ArgumentNullException($"Не получилось получить id {model.Id}");
|
||||
@ -99,11 +99,11 @@ namespace BusinessLogics.BusinessLogic
|
||||
}
|
||||
}
|
||||
|
||||
public List<CostViewModel> ReadList(CostSearchModel? model = null)
|
||||
public List<OrderViewModel> ReadList(OrderSearchModel? model = null)
|
||||
{
|
||||
try
|
||||
{
|
||||
var results = model != null ? _costStorage.GetFilteredList(model) : _costStorage.GetFullList();
|
||||
var results = model != null ? _orderStorage.GetFilteredList(model) : _orderStorage.GetFullList();
|
||||
_logger.LogDebug("Список полученных статей затрат {@costs}", results);
|
||||
_logger.LogInformation("Извлечение списка c затрат по {@CostSearchModel} модели", model);
|
||||
return results;
|
||||
@ -115,12 +115,12 @@ namespace BusinessLogics.BusinessLogic
|
||||
}
|
||||
}
|
||||
|
||||
public bool Update(CostBindingModel model)
|
||||
public bool Update(OrderBindingModel model)
|
||||
{
|
||||
try
|
||||
{
|
||||
//CheckModel(model);
|
||||
var result = _costStorage.Update(model);
|
||||
var result = _orderStorage.Update(model);
|
||||
if (result == null)
|
||||
{
|
||||
throw new ArgumentNullException($"Не получилось обновить затраты");
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@ -85,11 +85,11 @@ namespace BusinessLogics.OfficePackage
|
||||
$"ожидается Purchase; Получен объект типа: {pc.GetType()}", nameof(info));
|
||||
}
|
||||
|
||||
if (purchase.CostViewModels == null)
|
||||
if (purchase.OrderViewModels == null)
|
||||
{
|
||||
throw new ArgumentNullException($"Получена модель статьи затрат c нулевыми полями");
|
||||
}
|
||||
foreach (var cost in purchase.CostViewModels)
|
||||
foreach (var order in purchase.OrderViewModels)
|
||||
{
|
||||
CreateRow(new PdfRowParameters
|
||||
{
|
||||
@ -97,8 +97,8 @@ namespace BusinessLogics.OfficePackage
|
||||
{
|
||||
purchase.DatePurchase.ToShortDateString(),
|
||||
purchase.Id.ToString(),
|
||||
cost.NameOfCost,
|
||||
(cost.Price* cost.PurchaseModels[purchase.Id].Count).ToString(),
|
||||
order.NameOrder,
|
||||
(order.Price* order.PurchaseModels[purchase.Id].Count).ToString(),
|
||||
},
|
||||
Style = "Normal",
|
||||
ParagraphAlignment = PdfParagraphAlignmentType.Left
|
||||
|
@ -8,7 +8,7 @@ namespace Contracts.BindingModel
|
||||
public int ClientId { get; set; }
|
||||
public DateOnly DatePurchase { get; set; }
|
||||
public Dictionary<int, OperationByPurchaseModel> OperationsModel { get; set; } = new();
|
||||
public List<CostByPurchaseModel> CostsModel { get; set; } = new();
|
||||
public List<OrderByPurchaseModel> OrdersModel { get; set; } = new();
|
||||
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@ -6,11 +6,11 @@ namespace Contracts.StoragesContracts
|
||||
{
|
||||
public interface IOrderStorage
|
||||
{
|
||||
List<CostViewModel> GetFullList();
|
||||
List<CostViewModel> GetFilteredList(OrderSearchModel model);
|
||||
CostViewModel? GetElement(OrderSearchModel model);
|
||||
CostViewModel? Insert(CostBindingModel model);
|
||||
CostViewModel? Update(CostBindingModel model);
|
||||
CostViewModel? Delete(CostBindingModel model);
|
||||
List<OrderViewModel> GetFullList();
|
||||
List<OrderViewModel> GetFilteredList(OrderSearchModel model);
|
||||
OrderViewModel? GetElement(OrderSearchModel model);
|
||||
OrderViewModel? Insert(OrderBindingModel model);
|
||||
OrderViewModel? Update(OrderBindingModel model);
|
||||
OrderViewModel? Delete(OrderBindingModel model);
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,6 @@ namespace Contracts.ViewModels
|
||||
[DisplayName("Стоимость")]
|
||||
public double Price { get; set; }
|
||||
|
||||
public Dictionary<int, CostByPurchaseModel> PurchaseModels { get; set; } = new();
|
||||
public Dictionary<int, OrderByPurchaseModel> PurchaseModels { get; set; } = new();
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@ -2,10 +2,10 @@
|
||||
|
||||
namespace DataModels.ProxyModels
|
||||
{
|
||||
public class CostByPurchaseModel : IId
|
||||
public class OrderByPurchaseModel : IId
|
||||
{
|
||||
public virtual int Id { get; set; }
|
||||
public virtual int CostId { get; set; }
|
||||
public virtual int OrderId { get; set; }
|
||||
public virtual int PurchaseId { get; set; }
|
||||
public virtual int Count { get; set; }
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
using DatabaseImplement.Models;
|
||||
using BankDatabaseImplement.Models;
|
||||
using DatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace DatabaseImplement
|
||||
@ -20,7 +21,7 @@ namespace DatabaseImplement
|
||||
}
|
||||
|
||||
public virtual DbSet<Client> Clients { set; get; }
|
||||
public virtual DbSet<Order> Costs { set; get; }
|
||||
public virtual DbSet<Order> Order { set; get; }
|
||||
public virtual DbSet<OrderByPurchase> OrderByPurchases { set; get; }
|
||||
public virtual DbSet<Employee> Employees { set; get; }
|
||||
public virtual DbSet<Operation> Operations { set; get; }
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@ -1,13 +1,13 @@
|
||||
using Contracts.BindingModels;
|
||||
using Contracts.BindingModel;
|
||||
using Contracts.SearchModels;
|
||||
using Contracts.StoragesContracts;
|
||||
using Contracts.ViewModels;
|
||||
using DatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace kDatabaseImplement.Implements
|
||||
namespace DatabaseImplement.Implements
|
||||
{
|
||||
public class CostStorage : IOrderStorage
|
||||
public class OrderStorage : IOrderStorage
|
||||
{
|
||||
private void CheckSearchModel(OrderSearchModel model)
|
||||
{
|
||||
@ -16,20 +16,20 @@ namespace kDatabaseImplement.Implements
|
||||
if (!model.Id.HasValue && !model.EmployeeId.HasValue)
|
||||
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
|
||||
}
|
||||
public CostViewModel? Delete(CostBindingModel model)
|
||||
public OrderViewModel? Delete(OrderBindingModel model)
|
||||
{
|
||||
using var context = new AutoShopDB();
|
||||
var element = context.Costs.FirstOrDefault(x => x.Id == model.Id);
|
||||
var element = context.Order.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Costs.Remove(element);
|
||||
context.Order.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public CostViewModel? GetElement(OrderSearchModel model)
|
||||
public OrderViewModel? GetElement(OrderSearchModel model)
|
||||
{
|
||||
CheckSearchModel(model);
|
||||
using var context = new AutoShopDB();
|
||||
@ -37,60 +37,60 @@ namespace kDatabaseImplement.Implements
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return context.Costs
|
||||
.Include(cost => cost.Employee)
|
||||
.Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase)
|
||||
return context.Order
|
||||
.Include(Order => Order.OrderId)
|
||||
.Include(Order => Order.Purchases).ThenInclude(OrderByPurchase => OrderByPurchase.Purchase)
|
||||
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel;
|
||||
}
|
||||
|
||||
public List<CostViewModel> GetFilteredList(OrderSearchModel model)
|
||||
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
|
||||
{
|
||||
CheckSearchModel(model);
|
||||
using var context = new AutoShopDB();
|
||||
return context.Costs
|
||||
.Include(cost => cost.Employee)
|
||||
.Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase)
|
||||
.Where(x => x.EmployeeId == model.EmployeeId || x.Id == model.Id)
|
||||
return context.Order
|
||||
.Include(order => order.OrderId)
|
||||
.Include(order => order.Purchases).ThenInclude(orderByPurchase => orderByPurchase.Purchase)
|
||||
.Where(x => x.OrderId == model.Id || x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<CostViewModel> GetFullList()
|
||||
public List<OrderViewModel> GetFullList()
|
||||
{
|
||||
using var context = new AutoShopDB();
|
||||
return context.Costs
|
||||
.Include(cost => cost.Employee)
|
||||
.Include(cost => cost.Purchases).ThenInclude(costByPurchase => costByPurchase.Purchase)
|
||||
return context.Order
|
||||
.Include(order => order.OrderId)
|
||||
.Include(order => order.Purchases).ThenInclude(orderByPurchase => orderByPurchase.Purchase)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public CostViewModel? Insert(CostBindingModel model)
|
||||
public OrderViewModel? Insert(OrderBindingModel model)
|
||||
{
|
||||
var newCost = Cost.Create(model);
|
||||
if (newCost == null)
|
||||
var newOrder = Order.Create(model);
|
||||
if (newOrder == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new AutoShopDB();
|
||||
|
||||
context.Costs.Add(newCost);
|
||||
context.Order.Add(newOrder);
|
||||
context.SaveChanges();
|
||||
return newCost.GetViewModel;
|
||||
return newOrder.GetViewModel;
|
||||
}
|
||||
public CostViewModel? Update(CostBindingModel model)
|
||||
public OrderViewModel? Update(OrderBindingModel model)
|
||||
{
|
||||
using var context = new AutoShopDB();
|
||||
var cost = context.Costs.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (cost == null)
|
||||
var order = context.OrderByPurchases.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (order == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
cost.Update(model);
|
||||
order.Update(model);
|
||||
context.SaveChanges();
|
||||
cost.UpdatePurchases(context, model);
|
||||
order.UpdatePurchases(context, model);
|
||||
context.SaveChanges();
|
||||
return cost.GetViewModel;
|
||||
return order.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using BankDataModels.ProxyModels;
|
||||
|
||||
namespace BankDatabaseImplement.Models
|
||||
{
|
||||
public class CostByPurchase : CostByPurchaseModel
|
||||
{
|
||||
[Required]
|
||||
public Order? Cost { get; private set; }
|
||||
[Required]
|
||||
public Purchase? Purchase { get; private set; }
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
using BankContracts.BindingModels;
|
||||
using BankContracts.ViewModels;
|
||||
using BankDataModels;
|
||||
using Contracts.BindingModel;
|
||||
using Contracts.ViewModels;
|
||||
using DataModels;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace BankDatabaseImplement.Models
|
||||
@ -24,7 +24,7 @@ namespace BankDatabaseImplement.Models
|
||||
[Required]
|
||||
public List<Operation>? Operations { get; private set; }
|
||||
[Required]
|
||||
public List<Order>? Costs { get; private set; }
|
||||
public List<Order>? Orders { get; private set; }
|
||||
|
||||
public static Employee Create(EmployeeBindingModel model)
|
||||
{
|
||||
|
@ -15,10 +15,10 @@ namespace DatabaseImplement.Models
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public double Price { get; private set; }
|
||||
private Dictionary<int, CostByPurchaseModel>? _cachedPurchases;
|
||||
public Dictionary<int, CostByPurchaseModel> PurchasesModels => _cachedPurchases ??= Purchases.Select(x => (CostByPurchaseModel)x).ToDictionary(x => x.PurchaseId, x => x);
|
||||
private Dictionary<int, OrderByPurchaseModel>? _cachedPurchases;
|
||||
public Dictionary<int, OrderByPurchaseModel> PurchasesModels => _cachedPurchases ??= Purchases.Select(x => (OrderByPurchaseModel)x).ToDictionary(x => x.PurchaseId, x => x);
|
||||
[Required]
|
||||
public Order? Order{ get; private set; }
|
||||
public Order? Orders{ get; private set; }
|
||||
[Required]
|
||||
public List<OrderByPurchase> Purchases { get; private set; } = new();
|
||||
|
||||
@ -40,10 +40,10 @@ namespace DatabaseImplement.Models
|
||||
|
||||
public OrderViewModel GetViewModel => new()
|
||||
{
|
||||
PhoneNumber = Employee?.PhoneNumber??string.Empty,
|
||||
PhoneNumber = IEmployee?.PhoneNumber??string.Empty,
|
||||
PurchaseModels = PurchasesModels,
|
||||
EmployeeId = EmployeeId,
|
||||
NameOfCost = NameOfCost,
|
||||
OrderId = OrderId,
|
||||
NameOrder = NameOrder,
|
||||
Price = Price,
|
||||
Id = Id
|
||||
};
|
||||
@ -59,12 +59,12 @@ namespace DatabaseImplement.Models
|
||||
/// </summary>
|
||||
public void UpdatePurchases(AutoShopDB context, OrderBindingModel model)
|
||||
{
|
||||
var oldPurchases = context.CostByPurchases.Where(x => x.CostId == model.Id).ToDictionary(x => x.PurchaseId, x => x);
|
||||
var oldPurchases = context.OrderByPurchases.Where(x => x.OrderId == model.Id).ToDictionary(x => x.PurchaseId, x => x);
|
||||
var newPurchases = model.PurchasesModels.ToDictionary(
|
||||
x => x.Key,
|
||||
x => new OrderByPurchase()
|
||||
{
|
||||
CostId = model.Id,
|
||||
OrderId = model.Id,
|
||||
PurchaseId = x.Key,
|
||||
Count = x.Value.Count,
|
||||
}
|
||||
|
19
DatabaseImplement/Models/OrderByPurchase.cs
Normal file
19
DatabaseImplement/Models/OrderByPurchase.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Contracts.BindingModel;
|
||||
using DataModels.ProxyModels;
|
||||
|
||||
namespace DatabaseImplement.Models
|
||||
{
|
||||
public class OrderByPurchase : OrderByPurchaseModel
|
||||
{
|
||||
[Required]
|
||||
public Order? Order { get; private set; }
|
||||
[Required]
|
||||
public Purchase? Purchase { get; private set; }
|
||||
|
||||
internal void Update(OrderBindingModel model)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
@ -48,7 +48,7 @@ namespace DatabaseImplement.Models
|
||||
ClientId = ClientId,
|
||||
ClientPhoneNumber = Client?.PhoneNumber ?? string.Empty,
|
||||
|
||||
CostViewModels = Costs?
|
||||
OrderViewModels = Order?
|
||||
.Select(x => x.Cost.GetViewModel)
|
||||
.ToList() ?? new(),
|
||||
OperationViewModels = Operations?
|
||||
@ -64,7 +64,7 @@ namespace DatabaseImplement.Models
|
||||
ClientId = ClientId,
|
||||
ClientPhoneNumber = Client?.PhoneNumber ?? string.Empty,
|
||||
|
||||
CostViewModels = Costs?
|
||||
OrderViewModels = Order?
|
||||
.Select(x => x.Cost.GetViewModel)
|
||||
.ToList() ?? new()
|
||||
};
|
||||
|
@ -9,6 +9,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
Loading…
Reference in New Issue
Block a user