2024-05-01 01:54:31 +04:00

103 lines
3.2 KiB
C#

using BankContracts.BindingModels.Client;
using BankContracts.SearchModels.Client;
using BankContracts.StoragesModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using BankDatabaseImplement.Models.ClientModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.ClientImplements
{
// Реализация интерфейса хранилища для сущности "Клиент"
public class ClientStorage : IClientStorage
{
public ClientViewModel? GetElement(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
// Для поиска почты для отправки файла
if (model.Id.HasValue && string.IsNullOrEmpty(model.Password))
{
return context.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
return context.Clients.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email
&& !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new BankDatabase();
return context.Clients.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newComponent = Client.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new BankDatabase();
context.Clients.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new BankDatabase();
var component = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new BankDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}