ISEbd-22_CourseWork_School/School/SchoolDataBaseImplement/Implements/ExecutorStorage.cs

40 lines
1.8 KiB
C#
Raw Permalink 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 SchoolContracts.BindingModels;
using SchoolContracts.SearchModels;
using SchoolContracts.StoragesContracts;
using SchoolContracts.ViewModels;
using SchoolDatabaseImplement.Models;
namespace SchoolDatabaseImplement.Implements
{
public class ExecutorStorage : IExecutorStorage
{
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 ExecutorViewModel? GetElement(ExecutorSearchModel model)
{
CheckSearchModel(model);
using var context = new SchoolDB();
// возможность поиска только по логину для проверки на уникальность, или поиска по логина и паролю
return context.Executors
.FirstOrDefault(x => x.Login.Equals(model.Login) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)));
}
public ExecutorViewModel? Insert(ExecutorBindingModel model)
{
if (model == null)
{
return null;
}
var newExecutor = Executor.Create(model);
using var context = new SchoolDB();
context.Executors.Add(newExecutor);
context.SaveChanges();
return newExecutor;
}
}
}