CourseWork_University/University/UniversityDatabaseImplement/Implements/DirectorStorage.cs

40 lines
1.8 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
namespace UniversityDatabaseImplement.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 UniversityDB();
// возможность поиска только по логину для проверки на уникальность, или поиска по логина и паролю
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 UniversityDB();
context.Directors.Add(newDirector);
context.SaveChanges();
return newDirector;
}
}
}