This commit is contained in:
Николай 2023-04-02 17:13:48 +04:00
parent cf82bd4fe7
commit 2debfe4ba8
5 changed files with 54 additions and 14 deletions

View File

@ -137,5 +137,6 @@ namespace HardwareShopContracts.BusinessLogicsContracts
_logger.LogInformation("ReadList. Count:{Count}", list.Count); _logger.LogInformation("ReadList. Count:{Count}", list.Count);
return sum; return sum;
} }
} }
} }

View File

@ -123,5 +123,23 @@ namespace HardwareShopContracts.BusinessLogicsContracts
} }
_logger.LogInformation("Purchase. UserId:{UserId}. PurchaseID:{Id}. Sum:{ Sum}", model.UserId, model.Id, model.Sum); _logger.LogInformation("Purchase. UserId:{UserId}. PurchaseID:{Id}. Sum:{ Sum}", model.UserId, model.Id, model.Sum);
} }
public double Price(PurchaseSearchModel model)
{
_logger.LogInformation("ReadList. Id:{Id}", model?.Id);
var list = _purchaseStorage.GetFilteredListBuild(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return 0;
}
double sum = 0;
foreach (var build in list)
{
sum += build.Value.Item1* build.Value.Item2.Price;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return sum;
}
} }
} }

View File

@ -8,6 +8,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts
{ {
List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model); List<PurchaseViewModel>? ReadList(PurchaseSearchModel? model);
CommentViewModel? ReadElement(CommentSearchModel model); CommentViewModel? ReadElement(CommentSearchModel model);
double Price(PurchaseSearchModel model);
bool Create(PurchaseBindingModel model); bool Create(PurchaseBindingModel model);
bool Update(PurchaseBindingModel model); bool Update(PurchaseBindingModel model);
bool Delete(PurchaseBindingModel model); bool Delete(PurchaseBindingModel model);

View File

@ -1,6 +1,7 @@
using HardwareShopContracts.BindingModels; using HardwareShopContracts.BindingModels;
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDataModels.Models;
namespace HardwareShopContracts.StoragesContracts namespace HardwareShopContracts.StoragesContracts
{ {
@ -8,6 +9,7 @@ namespace HardwareShopContracts.StoragesContracts
{ {
List<PurchaseViewModel> GetFullList(); List<PurchaseViewModel> GetFullList();
List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model); List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model);
Dictionary<int, (int, IBuildModel)> GetFilteredListBuild(PurchaseSearchModel model);
PurchaseViewModel? GetElement(PurchaseSearchModel model); PurchaseViewModel? GetElement(PurchaseSearchModel model);
PurchaseViewModel? Insert(PurchaseBindingModel model); PurchaseViewModel? Insert(PurchaseBindingModel model);
PurchaseViewModel? Update(PurchaseBindingModel model); PurchaseViewModel? Update(PurchaseBindingModel model);

View File

@ -2,7 +2,9 @@
using HardwareShopContracts.SearchModels; using HardwareShopContracts.SearchModels;
using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.StoragesContracts;
using HardwareShopContracts.ViewModels; using HardwareShopContracts.ViewModels;
using HardwareShopDatabaseImplement.Models.ManyToMany;
using HardwareShopDatabaseImplement.Models.Worker; using HardwareShopDatabaseImplement.Models.Worker;
using HardwareShopDataModels.Models;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace HardwareShopDatabaseImplement.Implements.Worker namespace HardwareShopDatabaseImplement.Implements.Worker
@ -15,6 +17,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases return context.Purchases
.Include(x => x.Builds) .Include(x => x.Builds)
.Include(x => x.Goods) .Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User) .Include(x => x.User)
.Select(x => x.GetViewModel) .Select(x => x.GetViewModel)
.ToList(); .ToList();
@ -23,25 +26,36 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model) public List<PurchaseViewModel> GetFilteredList(PurchaseSearchModel model)
{ {
using var context = new HardwareShopDatabase(); using var context = new HardwareShopDatabase();
if (model.UserId.HasValue) if (!model.UserId.HasValue)
{ {
return context.Purchases return new();
.Include(x => x.Builds)
.Include(x => x.Goods)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
} }
return context.Purchases return context.Purchases
.Include(x => x.Builds) .Include(x => x.Builds)
.Include(x => x.Goods) .Include(x => x.Goods)
.Include(x => x.User) .ThenInclude(x => x.Good)
.Where(x => x.Id == model.Id) .Include(x => x.User)
.Select(x => x.GetViewModel) .Where(x => x.UserId == model.UserId)
.ToList(); .Select(x => x.GetViewModel)
.ToList();
} }
public Dictionary<int, (int, IBuildModel)> GetFilteredListBuild(PurchaseSearchModel model)
{
using var context = new HardwareShopDatabase();
var listBuilds = context.PurchasesBuilds
.Include(x => x.Build)
.Where(x => x.PurchaseId == model.Id)
.ToList();
Dictionary<int,(int, IBuildModel)>? _purchaseBuilds = null;
foreach(var build in listBuilds)
{
_purchaseBuilds.Add(build.BuildId, (build.Count, build.Build));
}
return _purchaseBuilds;
}
public PurchaseViewModel? GetElement(PurchaseSearchModel model) public PurchaseViewModel? GetElement(PurchaseSearchModel model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue)
@ -52,6 +66,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases return context.Purchases
.Include(x => x.Builds) .Include(x => x.Builds)
.Include(x => x.Goods) .Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User) .Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel; ?.GetViewModel;
@ -70,6 +85,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
return context.Purchases return context.Purchases
.Include(x => x.Builds) .Include(x => x.Builds)
.Include(x => x.Goods) .Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User) .Include(x => x.User)
.FirstOrDefault(x => x.Id == newPurchase.Id) .FirstOrDefault(x => x.Id == newPurchase.Id)
?.GetViewModel; ?.GetViewModel;
@ -84,6 +100,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
var purchase = context.Purchases var purchase = context.Purchases
.Include(x => x.Builds) .Include(x => x.Builds)
.Include(x => x.Goods) .Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User) .Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id); .FirstOrDefault(x => x.Id == model.Id);
if (purchase == null) if (purchase == null)
@ -109,6 +126,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
var element = context.Purchases var element = context.Purchases
.Include(x => x.Builds) .Include(x => x.Builds)
.Include(x => x.Goods) .Include(x => x.Goods)
.ThenInclude(x => x.Good)
.Include(x => x.User) .Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id); .FirstOrDefault(rec => rec.Id == model.Id);
if (element != null) if (element != null)