2024-04-28 20:12:32 +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 20:12:32 +04:00
|
|
|
|
public class ClientStorage : IClientStorage
|
2024-04-28 18:50:40 +04:00
|
|
|
|
{
|
2024-04-28 20:12:32 +04:00
|
|
|
|
private void CheckSearchModel(ClientSearchModel 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 ClientViewModel? GetElement(ClientSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckSearchModel(model);
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
|
|
|
|
|
return context.Clients.FirstOrDefault(x => x.PhoneNumber.Equals(model.PhoneNumber) && (string.IsNullOrEmpty(model.Password) || x.Password.Equals(model.Password)))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public ClientViewModel? Insert(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var newClient = Client.Create(model);
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
context.Clients.Add(newClient);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newClient.GetViewModel;
|
|
|
|
|
}
|
2024-04-28 18:50:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|