исправление дебильных ошибок по составлению фильтрованного списка заказов, лучше поздно, чем никогда #5

Merged
Igor-Melnikov merged 1 commits from lab4 into lab5 2023-03-27 17:59:27 +04:00
3 changed files with 52 additions and 14 deletions

View File

@ -24,15 +24,26 @@ namespace BlacksmithWorkshopDatabaseImplement.Implements
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
if (!model.Id.HasValue && (!model.DateFrom.HasValue || !model.DateTo.HasValue))
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
List<OrderViewModel> orderList = context.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
List<OrderViewModel> orderList = new();
if (model.Id.HasValue) //сначала ищем по Id
{
orderList = context.Orders
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.DateFrom.HasValue && model.DateTo.HasValue) //если у модели нет Id, ищем по датам
{
orderList = context.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
foreach (var order in orderList)
{
string manufactureName = context.Manufactures

View File

@ -27,14 +27,25 @@ namespace BlacksmithWorkshopFileImplement.Implements
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
if (!model.Id.HasValue && (!model.DateFrom.HasValue || !model.DateTo.HasValue))
{
return new();
}
return source.Orders
if (model.Id.HasValue)//сначала ищем по Id
{
return source.Orders
.Where(x => x.Id == model.Id)
.Select(x => AddManufactureName(x.GetViewModel))
.ToList();
}
else if (model.DateFrom.HasValue && model.DateTo.HasValue)//если Id в модели нет, ищем по датам
{
return source.Orders
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
.Select(x => AddManufactureName(x.GetViewModel))
.ToList();
}
return new();//если нет ни того, ни другого, возвращаем пустой список
}
public OrderViewModel? GetElement(OrderSearchModel model)
{

View File

@ -28,18 +28,34 @@ namespace BlacksmithWorkshopListImplement.Implements
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
if (!model.Id.HasValue && (!model.DateFrom.HasValue || !model.DateTo.HasValue))
{
return result;
}
foreach (var order in _source.Orders)
if (model.Id.HasValue)//сначала ищем по Id
{
if (model.Id.HasValue && order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
foreach (var order in _source.Orders)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
result.Add(vm);
if (model.Id == order.Id)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
result.Add(vm);
}
}
}
else if (model.DateFrom.HasValue && model.DateTo.HasValue)//если у модели нет Id, ищем по датам
{
foreach (var order in _source.Orders)
{
if (order.DateCreate >= model.DateFrom && order.DateCreate <= model.DateTo)
{
OrderViewModel vm = order.GetViewModel;
var manufacture = _source.Manufactures.Find(x => x.Id == order.ManufactureId);
vm.ManufactureName = manufacture?.ManufactureName ?? string.Empty;
result.Add(vm);
}
}
}
return result;