ЛАйк э босс

директор сторэдж
This commit is contained in:
Максим Куклев 2024-04-30 18:41:34 +04:00
parent eca2a3a9a5
commit 18c17156f1
3 changed files with 38 additions and 1 deletions

View File

@ -7,7 +7,6 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Implements\" />
<Folder Include="Models\" />
</ItemGroup>

View File

@ -0,0 +1,38 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterDatabaseImplement.Models;
namespace CarCenterDatabaseImplement.Implements
{
public class DirectorStorage : IDirectorStorage
{
private void CheckSearchModel(DirectorSearchModel model)
{
if (model == null)
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.Password))
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
}
public DirectorViewModel? GetElement(DirectorSearchModel model)
{
CheckSearchModel(model);
using var context = new CarCenterDb();
return context.Directors
.FirstOrDefault(x => x.Login.Equals(model.Login) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)));
}
public DirectorViewModel? Insert(DirectorBindingModel model)
{
if (model == null)
{
return null;
}
var newDirector = Director.Create(model);
using var context = new CarCenterDb();
context.Directors.Add(newDirector);
context.SaveChanges();
return newDirector;
}
}
}