CourseWork_Bank/Bank/BankDatabaseImplement/Implements/EmployeeStorage.cs

42 lines
1.9 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 BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
namespace BankDatabaseImplement.Implements
{
public class EmployeeStorage : IEmployeeStorage
{
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;
}
}
}