OrderStorage changed lab 6

This commit is contained in:
VictoriaPresnyakova 2023-04-17 18:53:16 +04:00
parent 30394150d8
commit 374bc28468
2 changed files with 51 additions and 28 deletions

View File

@ -1,4 +1,5 @@
using System;
using JewelryStoreDataModels.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -13,5 +14,7 @@ namespace JewelryStoreContracts.SearchModels
public DateTime? DateFrom { get; set; }
public DateTime? DateTo { get; set; }
public int? ImplementerId { get; set; }
public List<OrderStatus>? Statuses { get; set; }
}
}

View File

@ -19,7 +19,7 @@ namespace JewelryStoreDatabaseImplement.Implements
using var context = new JewelryStoreDataBase();
var element = context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).FirstOrDefault(rec => rec.Id == model.Id);
.Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
@ -41,37 +41,53 @@ namespace JewelryStoreDatabaseImplement.Implements
using var context = new JewelryStoreDataBase();
return context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
return context.Orders
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x =>
(model.Statuses == null || model.Statuses != null && model.Statuses.Contains(x.Status)) &&
model.ImplementerId.HasValue && x.ImplementerId == model.ImplementerId ||
model.Id.HasValue && x.Id == model.Id
)
?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.ClientId.HasValue)
if (model.Id.HasValue)
{
var result = GetElement(model);
return result != null ? new() { result } : new();
}
using var context = new JewelryStoreDataBase();
IQueryable<Order>? queryWhere = null;
if (model.DateFrom.HasValue && model.DateTo.HasValue)
{
queryWhere = context.Orders
.Where(x => model.DateFrom <= x.DateCreate.Date &&
x.DateCreate.Date <= model.DateTo);
}
else if (model.Statuses != null)
{
queryWhere = context.Orders.Where(x => model.Statuses.Contains(x.Status));
}
else if (model.ClientId.HasValue)
{
queryWhere = context.Orders.Where(x => x.ClientId == model.ClientId);
}
else
{
return new();
}
using var context = new JewelryStoreDataBase();
if (model.DateFrom.HasValue)
{
return context.Orders
.Include(x => x.Jewel)
return queryWhere
.Include(x => x.Client)
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.ClientId.HasValue)
return context.Orders
.Include(x => x.Jewel)
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
return context.Orders
.Include(x => x.Jewel)
.Include(x => x.Client)
.Where(x => x.Id == model.Id)
.Include(x => x.Implementer)
.Select(x => x.GetViewModel)
.ToList();
}
@ -81,7 +97,7 @@ namespace JewelryStoreDatabaseImplement.Implements
using var context = new JewelryStoreDataBase();
return context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).Select(x => x.GetViewModel).ToList();
.Include(x => x.Client).Include(x => x.Implementer).Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
@ -98,7 +114,11 @@ namespace JewelryStoreDatabaseImplement.Implements
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
return context.Orders.Include(x => x.Jewel)
.Include(x => x.Client)
.Include(x => x.Implementer)
.FirstOrDefault(x => x.Id == newOrder.Id)
?.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
@ -106,7 +126,7 @@ namespace JewelryStoreDatabaseImplement.Implements
using var context = new JewelryStoreDataBase();
var order = context.Orders.Include(x => x.Jewel)
.Include(x => x.Client).FirstOrDefault(x => x.Id == model.Id);
.Include(x => x.Client).Include(x => x.Implementer).FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;