ISEbd-22_CourseWork_School/School/SchoolDataBaseImplement/Implements/ImplementerStorage.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 ImplementerStorage : IImplementerStorage
{
private void CheckSearchModel(ImplementerSearchModel 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 ImplementerViewModel? GetElement(ImplementerSearchModel model)
{
CheckSearchModel(model);
using var context = new SchoolDB();
// возможность поиска только по логину для проверки на уникальность, или поиска по логина и паролю
return context.Implementers
.FirstOrDefault(x => x.Login.Equals(model.Login) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)));
}
public ImplementerViewModel? Insert(ImplementerBindingModel model)
{
if (model == null)
{
return null;
}
var newStudent = Implementer.Create(model);
using var context = new SchoolDB();
context.Implementers.Add(newStudent);
context.SaveChanges();
return newStudent;
}
}
}