CourseWork_Bank/Bank/BankDatabaseImplement/Implements/ClientStorage.cs

41 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 ClientStorage : IClientStorage
{
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;
}
}
}