add BusinessLogic

This commit is contained in:
Zakharov_Rostislav 2024-05-05 23:59:47 +04:00
parent 3e9619c7c4
commit 50bc49151d
21 changed files with 741 additions and 45 deletions

View File

@ -5,9 +5,11 @@ VisualStudioVersion = 17.9.34723.18
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomDataModels", "CarShowroomDataModels\CarShowroomDataModels.csproj", "{7EECD77A-5D26-4093-85B0-F2D33C6F0C2C}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomDataModels", "CarShowroomDataModels\CarShowroomDataModels.csproj", "{7EECD77A-5D26-4093-85B0-F2D33C6F0C2C}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarShowroomContracts", "CarShowroomContracts\CarShowroomContracts.csproj", "{6274B5CD-C339-4FD3-AC45-DE8384AD4CB7}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomContracts", "CarShowroomContracts\CarShowroomContracts.csproj", "{6274B5CD-C339-4FD3-AC45-DE8384AD4CB7}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarShowroomDatabaseStorage", "CarShowroomDatabaseStorage\CarShowroomDatabaseStorage.csproj", "{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CarShowroomDatabaseStorage", "CarShowroomDatabaseStorage\CarShowroomDatabaseStorage.csproj", "{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CarShowroomBusinessLogic", "CarShowroomBusinessLogic\CarShowroomBusinessLogic.csproj", "{B0DA0319-2DBC-4785-BA5F-338AD5E3E437}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -27,6 +29,10 @@ Global
{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Debug|Any CPU.Build.0 = Debug|Any CPU {7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Release|Any CPU.ActiveCfg = Release|Any CPU {7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Release|Any CPU.Build.0 = Release|Any CPU {7F0A0E4D-6EFD-459D-AFFF-99F3185243F8}.Release|Any CPU.Build.0 = Release|Any CPU
{B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0DA0319-2DBC-4785-BA5F-338AD5E3E437}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,89 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class CarLogic : ICarLogic
{
private readonly ICarStorage _carStorage;
public CarLogic(ICarStorage storage)
{
_carStorage = storage;
}
public List<CarView>? ReadList(CarSearch? model)
{
var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public CarView? ReadElement(CarSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _carStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(CarDto model)
{
CheckModel(model);
if (_carStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(CarDto model)
{
CheckModel(model);
if (_carStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(CarDto model)
{
CheckModel(model, false);
if (_carStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(CarDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (model.ModelId < 0)
throw new InvalidOperationException();
if (string.IsNullOrEmpty(model.Color))
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,95 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class ClientLogic : IClientLogic
{
private readonly IClientStorage _clientStorage;
public ClientLogic(IClientStorage storage)
{
_clientStorage = storage;
}
public List<ClientView>? ReadList(ClientSearch? model)
{
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public ClientView? ReadElement(ClientSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _clientStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(ClientDto model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ClientDto model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ClientDto model)
{
CheckModel(model, false);
if (_clientStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ClientDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new InvalidOperationException();
if (string.IsNullOrEmpty(model.PhoneNumber))
throw new InvalidOperationException();
var element = _clientStorage.GetElement(new ClientSearch
{
PhoneNumber = model.PhoneNumber,
});
if (element != null && element.Id != model.Id)
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,97 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class EmployeeLogic : IEmployeeLogic
{
private readonly IEmployeeStorage _employeeStorage;
public EmployeeLogic(IEmployeeStorage storage)
{
_employeeStorage = storage;
}
public List<EmployeeView>? ReadList(EmployeeSearch? model)
{
var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public EmployeeView? ReadElement(EmployeeSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _employeeStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(EmployeeDto model)
{
CheckModel(model);
if (_employeeStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(EmployeeDto model)
{
CheckModel(model);
if (_employeeStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(EmployeeDto model)
{
CheckModel(model, false);
if (_employeeStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(EmployeeDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new InvalidOperationException();
if (string.IsNullOrEmpty(model.Email))
throw new InvalidOperationException();
if (string.IsNullOrEmpty(model.Password))
throw new InvalidOperationException();
var element = _employeeStorage.GetElement(new EmployeeSearch
{
Email = model.Email,
});
if (element != null && element.Id != model.Id)
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,93 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class MakeLogic : IMakeLogic
{
private readonly IMakeStorage _makeStorage;
public MakeLogic(IMakeStorage storage)
{
_makeStorage = storage;
}
public List<MakeView>? ReadList(MakeSearch? model)
{
var list = model == null ? _makeStorage.GetFullList() : _makeStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public MakeView? ReadElement(MakeSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _makeStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(MakeDto model)
{
CheckModel(model);
if (_makeStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(MakeDto model)
{
CheckModel(model);
if (_makeStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(MakeDto model)
{
CheckModel(model, false);
if (_makeStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(MakeDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new InvalidOperationException();
var element = _makeStorage.GetElement(new MakeSearch
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,97 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class ModelLogic : IModelLogic
{
private readonly IModelStorage _modelStorage;
public ModelLogic(IModelStorage storage)
{
_modelStorage = storage;
}
public List<ModelView>? ReadList(ModelSearch? model)
{
var list = model == null ? _modelStorage.GetFullList() : _modelStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public ModelView? ReadElement(ModelSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _modelStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(ModelDto model)
{
CheckModel(model);
if (_modelStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ModelDto model)
{
CheckModel(model);
if (_modelStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ModelDto model)
{
CheckModel(model, false);
if (_modelStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ModelDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new InvalidOperationException();
if (model.MakeId < 0)
throw new InvalidOperationException();
if (model.Price < 0)
throw new InvalidOperationException();
var element = _modelStorage.GetElement(new ModelSearch
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,91 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class SaleLogic : ISaleLogic
{
private readonly ISaleStorage _saleStorage;
public SaleLogic(ISaleStorage storage)
{
_saleStorage = storage;
}
public List<SaleView>? ReadList(SaleSearch? model)
{
var list = model == null ? _saleStorage.GetFullList() : _saleStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public SaleView? ReadElement(SaleSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _saleStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(SaleDto model)
{
CheckModel(model);
if (_saleStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(SaleDto model)
{
CheckModel(model);
if (_saleStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(SaleDto model)
{
CheckModel(model, false);
if (_saleStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(SaleDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (model.Cost < 0)
throw new InvalidOperationException();
if (model.EmployeeId < 0)
throw new InvalidOperationException();
if (model.ClientId < 0)
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,96 @@
using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using ServiceShowroomContracts.BusinessLogic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class ServiceLogic : IServiceLogic
{
private readonly IServiceStorage _serviceStorage;
public ServiceLogic(IServiceStorage storage)
{
_serviceStorage = storage;
}
public List<ServiceView>? ReadList(ServiceSearch? model)
{
var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public ServiceView? ReadElement(ServiceSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _serviceStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(ServiceDto model)
{
CheckModel(model);
if (_serviceStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ServiceDto model)
{
CheckModel(model);
if (_serviceStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ServiceDto model)
{
CheckModel(model, false);
if (_serviceStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ServiceDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new InvalidOperationException();
if (model.Cost < 0)
throw new InvalidOperationException();
var element = _serviceStorage.GetElement(new ServiceSearch
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
throw new InvalidOperationException();
}
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\CarShowroomContracts\CarShowroomContracts.csproj" />
<ProjectReference Include="..\CarShowroomDataModels\CarShowroomDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -1,4 +1,4 @@
using CarShowroomContracts.Dtos; using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.Views; using CarShowroomDataModels.Views;
using CarShowroomDataModels.SearchModel; using CarShowroomDataModels.SearchModel;
using System; using System;

View File

@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel
public class ClientSearch public class ClientSearch
{ {
public int? Id; public int? Id;
public string? PhoneNumber;
} }
} }

View File

@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel
public class MakeSearch public class MakeSearch
{ {
public int? Id; public int? Id;
public string? Name;
} }
} }

View File

@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel
public class ModelSearch public class ModelSearch
{ {
public int? Id; public int? Id;
public string? Name;
} }
} }

View File

@ -9,5 +9,6 @@ namespace CarShowroomDataModels.SearchModel
public class ServiceSearch public class ServiceSearch
{ {
public int? Id; public int? Id;
public string? Name;
} }
} }

View File

@ -17,8 +17,8 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Cars return context.Cars
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<CarView> GetFilteredList(CarSearch model) public List<CarView> GetFilteredList(CarSearch model)

View File

@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Clients return context.Clients
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<ClientView> GetFilteredList(ClientSearch model) public List<ClientView> GetFilteredList(ClientSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.PhoneNumber))
{ {
return new(); return new();
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Clients return context.Clients
.Where(x => !model.Id.HasValue || x.Id == model.Id) .Where(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.PhoneNumber) || x.PhoneNumber.Equals(model.PhoneNumber)))
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public ClientView? GetElement(ClientSearch model) public ClientView? GetElement(ClientSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.PhoneNumber))
{ {
return null; return null;
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Clients return context.Clients
.FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.PhoneNumber) || x.PhoneNumber.Equals(model.PhoneNumber)))
?.GetView(); ?.GetView();
} }

View File

@ -17,8 +17,8 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Employees return context.Employees
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<EmployeeView> GetFilteredList(EmployeeSearch model) public List<EmployeeView> GetFilteredList(EmployeeSearch model)
@ -31,10 +31,9 @@ namespace CarShowroomDatabaseStorage.Storages
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Employees return context.Employees
.Where(x => .Where(x => !model.Id.HasValue || x.Id == model.Id &&
!model.Id.HasValue || x.Id == model.Id && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && (string.IsNullOrEmpty(model.Email) || x.Password.Equals(model.Email)))
(string.IsNullOrEmpty(model.Email) || x.Password == model.Email))
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
@ -49,10 +48,9 @@ namespace CarShowroomDatabaseStorage.Storages
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Employees return context.Employees
.FirstOrDefault(x => .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id &&
!model.Id.HasValue || x.Id == model.Id && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password) && (string.IsNullOrEmpty(model.Email) || x.Password.Equals(model.Email)))
(string.IsNullOrEmpty(model.Email) || x.Password == model.Email))
?.GetView(); ?.GetView();
} }

View File

@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Makes return context.Makes
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<MakeView> GetFilteredList(MakeSearch model) public List<MakeView> GetFilteredList(MakeSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.Name))
{ {
return new(); return new();
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Makes return context.Makes
.Where(x => !model.Id.HasValue || x.Id == model.Id) .Where(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name)))
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public MakeView? GetElement(MakeSearch model) public MakeView? GetElement(MakeSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.Name))
{ {
return null; return null;
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Makes return context.Makes
.FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name)))
?.GetView(); ?.GetView();
} }

View File

@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Models return context.Models
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<ModelView> GetFilteredList(ModelSearch model) public List<ModelView> GetFilteredList(ModelSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.Name))
{ {
return new(); return new();
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Models return context.Models
.Where(x => !model.Id.HasValue || x.Id == model.Id) .Where(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name)))
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public ModelView? GetElement(ModelSearch model) public ModelView? GetElement(ModelSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.Name))
{ {
return null; return null;
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Models return context.Models
.FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name)))
?.GetView(); ?.GetView();
} }
@ -62,14 +66,14 @@ namespace CarShowroomDatabaseStorage.Storages
public ModelView? Update(ModelDto model) public ModelView? Update(ModelDto model)
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
var model = context.Models.FirstOrDefault(x => x.Id == model.Id); var entity = context.Models.FirstOrDefault(x => x.Id == model.Id);
if (model == null) if (entity == null)
{ {
return null; return null;
} }
model.Update(model); entity.Update(entity);
context.SaveChanges(); context.SaveChanges();
return model.GetView(); return entity.GetView();
} }
public ModelView? Delete(ModelDto model) public ModelView? Delete(ModelDto model)

View File

@ -17,8 +17,8 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Sales return context.Sales
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<SaleView> GetFilteredList(SaleSearch model) public List<SaleView> GetFilteredList(SaleSearch model)

View File

@ -17,32 +17,36 @@ namespace CarShowroomDatabaseStorage.Storages
{ {
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Services return context.Services
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public List<ServiceView> GetFilteredList(ServiceSearch model) public List<ServiceView> GetFilteredList(ServiceSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.Name))
{ {
return new(); return new();
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Services return context.Services
.Where(x => !model.Id.HasValue || x.Id == model.Id) .Where(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name)))
.Select(x => x.GetView()) .Select(x => x.GetView())
.ToList(); .ToList();
} }
public ServiceView? GetElement(ServiceSearch model) public ServiceView? GetElement(ServiceSearch model)
{ {
if (!model.Id.HasValue) if (!model.Id.HasValue &&
string.IsNullOrEmpty(model.Name))
{ {
return null; return null;
} }
using var context = new CarShowroomDatabase(); using var context = new CarShowroomDatabase();
return context.Services return context.Services
.FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id) .FirstOrDefault(x => !model.Id.HasValue || x.Id == model.Id &&
(string.IsNullOrEmpty(model.Name) || x.Name.Equals(model.Name)))
?.GetView(); ?.GetView();
} }