Удалены LINQ методы из ListImplement

This commit is contained in:
Данияр Аглиуллов 2023-03-13 17:17:36 +04:00
commit 509d618c30
3 changed files with 123 additions and 101 deletions

View File

@ -5,6 +5,7 @@ using ConfectioneryContracts.ViewModels;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -20,25 +21,30 @@ namespace ConfectioneryListImplement
public ClientViewModel? Delete(ClientBindingModel model) public ClientViewModel? Delete(ClientBindingModel model)
{ {
var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id); for (int i = 0; i < _source.Clients.Count; ++i)
if (res != null)
{ {
_source.Clients.Remove(res); if (_source.Clients[i].Id == model.Id)
{
var element = _source.Clients[i];
_source.Clients.RemoveAt(i);
return element.GetViewModel;
} }
return res?.GetViewModel; }
return null;
} }
public ClientViewModel? GetElement(ClientSearchModel model) public ClientViewModel? GetElement(ClientSearchModel model)
{ {
if (model.Id.HasValue) foreach (var client in _source.Clients)
return _source.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; {
if (model.Email != null && model.Password != null) if (model.Id.HasValue && model.Id == client.Id)
return _source.Clients return client.GetViewModel;
.FirstOrDefault(x => x.Email.Equals(model.Email) if (model.Email != null && model.Password != null &&
&& x.Password.Equals(model.Password)) client.Email.Equals(model.Email) && client.Password.Equals(model.Password))
?.GetViewModel; return client.GetViewModel;
if (model.Email != null) if (model.Email != null && client.Email.Equals(model.Email))
return _source.Clients.FirstOrDefault(x => x.Email.Equals(model.Email))?.GetViewModel; return client.GetViewModel;
}
return null; return null;
} }
@ -48,24 +54,20 @@ namespace ConfectioneryListImplement
{ {
return new(); return new();
} }
if (model.Id.HasValue) // У нас нет каких либо специфических условий для выборки списка клиентов,
{ // поэтому получаем лишь один элемент по модели
var res = GetElement(model); var res = GetElement(model);
return res != null ? new() { res } : new(); return res != null ? new() { res } : new();
} }
if (model.Email != null)
{
return _source.Clients
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public List<ClientViewModel> GetFullList() public List<ClientViewModel> GetFullList()
{ {
return _source.Clients.Select(x => x.GetViewModel).ToList(); var result = new List<ClientViewModel>();
foreach (var client in _source.Clients)
{
result.Add(client.GetViewModel);
}
return result;
} }
public ClientViewModel? Insert(ClientBindingModel model) public ClientViewModel? Insert(ClientBindingModel model)
@ -81,9 +83,15 @@ namespace ConfectioneryListImplement
public ClientViewModel? Update(ClientBindingModel model) public ClientViewModel? Update(ClientBindingModel model)
{ {
var res = _source.Clients.FirstOrDefault(x => x.Id == model.Id); foreach (var client in _source.Clients)
res?.Update(model); {
return res?.GetViewModel; if (client.Id == model.Id)
{
client.Update(model);
return client.GetViewModel;
}
}
return null;
} }
} }
} }

View File

@ -2,6 +2,7 @@
using ConfectioneryContracts.SearchModels; using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels; using ConfectioneryContracts.ViewModels;
using System.Collections.Generic;
namespace ConfectioneryListImplement namespace ConfectioneryListImplement
{ {
@ -15,25 +16,30 @@ namespace ConfectioneryListImplement
public ImplementerViewModel? Delete(ImplementerBindingModel model) public ImplementerViewModel? Delete(ImplementerBindingModel model)
{ {
var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); for (int i = 0; i < _source.Implementers.Count; ++i)
if (res != null)
{ {
_source.Implementers.Remove(res); if (_source.Implementers[i].Id == model.Id)
{
var element = _source.Implementers[i];
_source.Implementers.RemoveAt(i);
return element.GetViewModel;
} }
return res?.GetViewModel; }
return null;
} }
public ImplementerViewModel? GetElement(ImplementerSearchModel model) public ImplementerViewModel? GetElement(ImplementerSearchModel model)
{ {
if (model.Id.HasValue) foreach (var x in _source.Implementers)
return _source.Implementers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; {
if (model.ImplementerFIO != null && model.Password != null) if (model.Id.HasValue && x.Id == model.Id)
return _source.Implementers return x.GetViewModel;
.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO) if (model.ImplementerFIO != null && model.Password != null &&
&& x.Password.Equals(model.Password)) x.ImplementerFIO.Equals(model.ImplementerFIO) && x.Password.Equals(model.Password))
?.GetViewModel; return x.GetViewModel;
if (model.ImplementerFIO != null) if (model.ImplementerFIO != null && x.ImplementerFIO.Equals(model.ImplementerFIO))
return _source.Implementers.FirstOrDefault(x => x.ImplementerFIO.Equals(model.ImplementerFIO))?.GetViewModel; return x.GetViewModel;
}
return null; return null;
} }
@ -48,19 +54,30 @@ namespace ConfectioneryListImplement
var res = GetElement(model); var res = GetElement(model);
return res != null ? new() { res } : new(); return res != null ? new() { res } : new();
} }
if (model.ImplementerFIO != null) // На случай если фио не будет уникальным (по заданию оно уникально) // На случай если при расширении проекта фио не будет уникальным
// (по заданию оно уникально)
List<ImplementerViewModel> result = new();
if (model.ImplementerFIO != null)
{ {
return _source.Implementers foreach (var implementer in _source.Implementers)
.Where(x => x.ImplementerFIO.Equals(model.ImplementerFIO)) {
.Select(x => x.GetViewModel) if (implementer.ImplementerFIO.Equals(model.ImplementerFIO))
.ToList(); {
result.Add(implementer.GetViewModel);
} }
return new(); }
}
return result;
} }
public List<ImplementerViewModel> GetFullList() public List<ImplementerViewModel> GetFullList()
{ {
return _source.Implementers.Select(x => x.GetViewModel).ToList(); var result = new List<ImplementerViewModel>();
foreach (var implementer in _source.Implementers)
{
result.Add(implementer.GetViewModel);
}
return result;
} }
public ImplementerViewModel? Insert(ImplementerBindingModel model) public ImplementerViewModel? Insert(ImplementerBindingModel model)
@ -76,9 +93,15 @@ namespace ConfectioneryListImplement
public ImplementerViewModel? Update(ImplementerBindingModel model) public ImplementerViewModel? Update(ImplementerBindingModel model)
{ {
var res = _source.Implementers.FirstOrDefault(x => x.Id == model.Id); foreach (var implementer in _source.Implementers)
res?.Update(model); {
return res?.GetViewModel; if (implementer.Id == model.Id)
{
implementer.Update(model);
return implementer.GetViewModel;
}
}
return null;
} }
} }
} }

View File

@ -3,6 +3,7 @@ using ConfectioneryContracts.SearchModels;
using ConfectioneryContracts.StoragesContract; using ConfectioneryContracts.StoragesContract;
using ConfectioneryContracts.ViewModels; using ConfectioneryContracts.ViewModels;
using ConfectioneryListImplement.Models; using ConfectioneryListImplement.Models;
using System.Security.Cryptography.X509Certificates;
namespace ConfectioneryListImplement namespace ConfectioneryListImplement
{ {
@ -30,17 +31,6 @@ namespace ConfectioneryListImplement
public OrderViewModel? GetElement(OrderSearchModel model) public OrderViewModel? GetElement(OrderSearchModel model)
{ {
if (model.ImplementerId.HasValue && model.Statusses != null)
{
return _source.Orders
.FirstOrDefault(x => x.ImplementerId == model.ImplementerId &&
model.Statusses.Contains(x.Status))
?.GetViewModel;
}
if (model.ImplementerId.HasValue)
{
return _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId)?.GetViewModel;
}
if (!model.Id.HasValue) if (!model.Id.HasValue)
{ {
return null; return null;
@ -51,6 +41,15 @@ namespace ConfectioneryListImplement
{ {
return GetViewModel(order); return GetViewModel(order);
} }
else if (model.ImplementerId.HasValue && model.Statusses != null &&
order.ImplementerId == model.ImplementerId && model.Statusses.Contains(order.Status))
{
return GetViewModel(order);
}
else if (model.ImplementerId.HasValue && model.ImplementerId == order.ImplementerId)
{
return GetViewModel(order);
}
} }
return null; return null;
} }
@ -58,40 +57,32 @@ namespace ConfectioneryListImplement
public List<OrderViewModel> GetFilteredList(OrderSearchModel model) public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{ {
var result = new List<OrderViewModel>(); var result = new List<OrderViewModel>();
if (!model.Id.HasValue && model.DateFrom.HasValue && model.DateTo.HasValue) // если не ищем по айдишнику, значит ищем по диапазону дат
{
return _source.Orders
.Where(x => model.DateFrom <= x.DateCreate.Date && x.DateCreate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
if (!model.Id.HasValue && model.ClientId.HasValue)
{
return _source.Orders
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
if (!model.Id.HasValue && model.Statusses != null)
{
return _source.Orders
.Where(x => model.Statusses.Contains(x.Status))
.Select(x => x.GetViewModel)
.ToList();
}
if (model.ImplementerId.HasValue)
{
var res = _source.Orders.FirstOrDefault(x => x.ImplementerId == model.ImplementerId);
return res != null ? new() { res.GetViewModel } : new();
}
foreach (var order in _source.Orders) foreach (var order in _source.Orders)
{ {
if (order.Id == model.Id) if (order.Id == model.Id)
{ {
return new() { GetViewModel(order) }; return new() { GetViewModel(order) };
} }
// если не ищем по айдишнику, значит ищем по диапазону дат
else if (model.DateFrom.HasValue && model.DateTo.HasValue &&
model.DateFrom <= order.DateCreate.Date && order.DateCreate.Date <= model.DateTo)
{
result.Add(GetViewModel(order));
} }
return new(); else if (model.ClientId.HasValue && order.ClientId == model.ClientId)
{
result.Add(GetViewModel(order));
}
else if (model.ImplementerId.HasValue && order.ImplementerId == model.ImplementerId)
{
result.Add(GetViewModel(order));
}
else if (model.Statusses != null && model.Statusses.Contains(order.Status))
{
result.Add(GetViewModel(order));
}
}
return result;
} }
public List<OrderViewModel> GetFullList() public List<OrderViewModel> GetFullList()