diff --git a/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj b/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj index 24ad2e8..aed3a89 100644 --- a/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj +++ b/CarCenterDatabaseImplement/CarCenterDatabaseImplement.csproj @@ -7,7 +7,6 @@ - diff --git a/CarCenterDatabaseImplement/CarCenteDb.cs b/CarCenterDatabaseImplement/CarCenterDb.cs similarity index 100% rename from CarCenterDatabaseImplement/CarCenteDb.cs rename to CarCenterDatabaseImplement/CarCenterDb.cs diff --git a/CarCenterDatabaseImplement/Implements/DirectorStorage.cs b/CarCenterDatabaseImplement/Implements/DirectorStorage.cs new file mode 100644 index 0000000..4bbbcf0 --- /dev/null +++ b/CarCenterDatabaseImplement/Implements/DirectorStorage.cs @@ -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; + } + } +}