This commit is contained in:
Николай 2023-04-02 16:32:22 +04:00
parent 8203d22849
commit ff2f0d57db
2 changed files with 24 additions and 23 deletions

View File

@ -8,7 +8,5 @@ namespace HardwareShopContracts.SearchModels
public string? BuildName { get; set; } = string.Empty;
public int? UserId { get; set; }
public int? PurchaseId { get; set; }
}
}

View File

@ -13,9 +13,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
{
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.ToList()
.Select(x => x.GetViewModel)
@ -24,17 +23,25 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
public List<BuildViewModel> GetFilteredList(BuildSearchModel model)
{
if (!model.UserId.HasValue)
if (string.IsNullOrEmpty(model.BuildName) && !model.UserId.HasValue)
{
return new();
}
using var context = new HardwareShopDatabase();
if (!string.IsNullOrEmpty(model.BuildName))
{
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.BuildName.Contains(model.BuildName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.Builds
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.UserId == model.UserId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
@ -48,9 +55,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
}
using var context = new HardwareShopDatabase();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) ||
@ -69,9 +75,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
context.Builds.Add(newBuild);
context.SaveChanges();
return context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(x => x.Id == newBuild.Id)
@ -85,9 +90,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
try
{
var build = context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.FirstOrDefault(x => x.Id == model.Id);
@ -112,9 +116,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker
{
using var context = new HardwareShopDatabase();
var element = context.Builds
.Include(x => x.Components)
.ThenInclude(x => x.Component)
.Include(x => x.Comments)
.Include(x => x.Purchases)
.ThenInclude(x => x.Purchase)
.Include(x => x.User)
.Where(x => x.UserId == model.Id)
.FirstOrDefault(rec => rec.Id == model.Id);