2024-04-28 19:31:02 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using BankDatabaseImplement.Models;
|
2024-04-28 18:50:40 +04:00
|
|
|
|
|
|
|
|
|
namespace BankDatabaseImplement.Implements
|
|
|
|
|
{
|
2024-04-28 19:31:02 +04:00
|
|
|
|
public class EmployeeStorage : IEmployeeStorage
|
2024-04-28 18:50:40 +04:00
|
|
|
|
{
|
2024-04-28 19:31:02 +04:00
|
|
|
|
private void CheckSearchModel(EmployeeSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
throw new ArgumentNullException("Передаваемая модель для поиска равна нулю", nameof(model));
|
|
|
|
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.PhoneNumber) && string.IsNullOrEmpty(model.Password))
|
|
|
|
|
throw new ArgumentException("Все передаваемые поля поисковой модели оказались пусты или равны null");
|
|
|
|
|
if (!model.Id.HasValue && (string.IsNullOrEmpty(model.PhoneNumber) && !string.IsNullOrEmpty(model.Password)))
|
|
|
|
|
throw new ArgumentException("Для нахождения соответствующего пользователя вместе с паролем нужен логин");
|
|
|
|
|
}
|
|
|
|
|
public EmployeeViewModel? GetElement(EmployeeSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckSearchModel(model);
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
|
|
|
|
|
return context.Employees
|
|
|
|
|
.FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public EmployeeViewModel? Insert(EmployeeBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var newEmployee = Employee.Create(model);
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
context.Employees.Add(newEmployee);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newEmployee.GetViewModel;
|
|
|
|
|
}
|
2024-04-28 18:50:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|