Сделал implements в database, осталось в бд продумать как подключаться к разным бд , а так бд закончена, внес некоторые необходимые правки , чтобы работал HasValue

This commit is contained in:
Алексей Крюков 2024-04-28 20:52:53 +04:00
parent b2412fe625
commit f4225ef4f8
14 changed files with 1080 additions and 30 deletions

View File

@ -10,18 +10,18 @@ namespace BankContracts.SearchModels.Cashier
// Для поиска сущности "Счёт"
public class AccountSearchModel
{
public int Id { get; set; }
public int? Id { get; set; }
public int CashierId { get; set; }
public int? CashierId { get; set; }
public int ClientId { get; set; }
public int? ClientId { get; set; }
public string AccountNumber { get; set; } = string.Empty;
public string? AccountNumber { get; set; } = string.Empty;
public double Balance { get; set; }
public double? Balance { get; set; }
public DateTime DateOpen { get; set; }
public DateTime? DateOpen { get; set; }
public StatusAccount StatusAccount { get; set; }
public StatusAccount? StatusAccount { get; set; }
}
}

View File

@ -9,18 +9,18 @@ namespace BankContracts.SearchModels.Cashier
// Для поиска сущности "Кассир"
public class CashierSearchModel
{
public int Id { get; set; }
public int? Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Name { get; set; } = string.Empty;
public string Surname { get; set; } = string.Empty;
public string? Surname { get; set; } = string.Empty;
public string Patronymic { get; set; } = string.Empty;
public string? Patronymic { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string? Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string? Password { get; set; } = string.Empty;
public string MobilePhone { get; set; } = string.Empty;
public string? MobilePhone { get; set; } = string.Empty;
}
}

View File

@ -9,18 +9,18 @@ namespace BankContracts.SearchModels.Client
// Для поиска сущности "Клиент"
public class ClientSearchModel
{
public int Id { get; set; }
public int? Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Name { get; set; } = string.Empty;
public string Surname { get; set; } = string.Empty;
public string? Surname { get; set; } = string.Empty;
public string Patronymic { get; set; } = string.Empty;
public string? Patronymic { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string? Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
public string? Password { get; set; } = string.Empty;
public string MobilePhone { get; set; } = string.Empty;
public string? MobilePhone { get; set; } = string.Empty;
}
}

View File

@ -21,8 +21,4 @@
<ProjectReference Include="..\BankDataModels\BankDataModels.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Implements\" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,145 @@
using BankContracts.BindingModels.Cashier;
using BankContracts.SearchModels.Cashier;
using BankContracts.StoragesModels.Cashier;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models.CashierModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.CashierImplements
{
public class AccountStorage : IAccountStorage
{
public List<AccountViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Accounts
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
public List<AccountViewModel> GetFilteredList(AccountSearchModel model)
{
using var context = new BankDatabase();
if (!string.IsNullOrEmpty(model.AccountNumber))
{
return context.Accounts
.Include(x => x.Client)
.Where(x => x.AccountNumber.Contains(model.AccountNumber))
.Select(x => x.GetViewModel)
.ToList();
}
if (model.CashierId.HasValue)
{
return context.Accounts
.Include(x => x.Client)
.Where(x => x.CashierId == model.CashierId)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.ClientId.HasValue)
{
return context.Accounts
.Include(x => x.Client)
.Where(x => x.ClientId == model.ClientId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Accounts
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
public AccountViewModel? GetElement(AccountSearchModel model)
{
if (string.IsNullOrEmpty(model.AccountNumber) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Accounts
.Include(x => x.Client)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.AccountNumber) && x.AccountNumber == model.AccountNumber) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public AccountViewModel? Insert(AccountBindingModel model)
{
using var context = new BankDatabase();
var newAccount = Account.Create(context, model);
if (newAccount == null)
{
return null;
}
context.Accounts.Add(newAccount);
context.SaveChanges();
return newAccount.GetViewModel;
}
public AccountViewModel? Update(AccountBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var account = context.Accounts.FirstOrDefault(rec => rec.Id == model.Id);
if (account == null)
{
return null;
}
account.Update(model);
context.SaveChanges();
transaction.Commit();
return context.Accounts
.Include(x => x.Client)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public AccountViewModel? Delete(AccountBindingModel model)
{
using var context = new BankDatabase();
var element = context.Accounts
.Include(x => x.AccountNumber)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Accounts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,123 @@
using BankContracts.BindingModels.Cashier;
using BankContracts.SearchModels.Cashier;
using BankContracts.StoragesModels.Cashier;
using BankContracts.ViewModels.Cashier.ViewModels;
using BankDatabaseImplement.Models.CashierModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.CashierImplements
{
public class CashWithdrawalStorage : ICashWithdrawalStorage
{
public List<CashWithdrawalViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.CashWithdrawals
.Include(x => x.Cashier)
.Include(x => x.Debiting)
.Include(x => x.Account)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CashWithdrawalViewModel> GetFilteredList(CashWithdrawalSearchModel model)
{
using var context = new BankDatabase();
var result = context.CashWithdrawals.Include(x => x.Cashier)
.Include(x => x.Debiting)
.Include(x => x.Account)
.ToList();
if (model.AccountId.HasValue) result = result.Where(x => x.AccountId == model.AccountId).ToList();
if (model.CashierId.HasValue) result = result.Where(x => x.CashierId == model.CashierId).ToList();
if (model.DateWithdrawal.HasValue) result = result.Where(x => x.DateWithdrawal >= model.DateWithdrawal).ToList();
if (model.ClientId.HasValue) result = result.Where(x => x.Account.ClientId == model.ClientId).ToList();
return result.Select(x => x.GetViewModel).ToList();
}
public CashWithdrawalViewModel? GetElement(CashWithdrawalSearchModel model)
{
using var context = new BankDatabase();
return context.CashWithdrawals
.Include(x => x.Cashier)
.Include(x => x.Debiting)
.Include(x => x.Account)
.FirstOrDefault(x => (!(model.AccountId < 0) && x.AccountId == model.AccountId) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public CashWithdrawalViewModel? Insert(CashWithdrawalBindingModel model)
{
using var context = new BankDatabase();
var newCashWithdrawal = CashWithdrawal.Create(context, model);
if (newCashWithdrawal == null)
{
return null;
}
context.CashWithdrawals.Add(newCashWithdrawal);
context.SaveChanges();
return newCashWithdrawal.GetViewModel;
}
public CashWithdrawalViewModel? Update(CashWithdrawalBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var cashWithdrawal = context.CashWithdrawals.FirstOrDefault(rec => rec.Id == model.Id);
if (cashWithdrawal == null)
{
return null;
}
cashWithdrawal.Update(model);
context.SaveChanges();
transaction.Commit();
return cashWithdrawal.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public CashWithdrawalViewModel? Delete(CashWithdrawalBindingModel model)
{
using var context = new BankDatabase();
var element = context.CashWithdrawals
.Include(x => x.AccountId)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.CashWithdrawals.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,104 @@
using BankContracts.BindingModels.Cashier;
using BankContracts.SearchModels.Cashier;
using BankContracts.StoragesModels.Cashier;
using BankContracts.ViewModels.Cashier.ViewModels;
using BankDatabaseImplement.Models.CashierModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.CashierImplements
{
public class CashierStorage : ICashierStorage
{
public List<CashierViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Cashiers
.Select(x => x.GetViewModel)
.ToList();
}
public List<CashierViewModel> GetFilteredList(CashierSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new BankDatabase();
return context.Cashiers
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public CashierViewModel? GetElement(CashierSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Cashiers
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public CashierViewModel? Insert(CashierBindingModel model)
{
using var context = new BankDatabase();
var newCashier = Cashier.Create(context, model);
if (newCashier == null)
{
return null;
}
context.Cashiers.Add(newCashier);
context.SaveChanges();
return newCashier.GetViewModel;
}
public CashierViewModel? Update(CashierBindingModel model)
{
using var context = new BankDatabase();
var cashier = context.Cashiers.FirstOrDefault(x => x.Id == model.Id);
if (cashier == null)
{
return null;
}
cashier.Update(model);
context.SaveChanges();
return cashier.GetViewModel;
}
public CashierViewModel? Delete(CashierBindingModel model)
{
using var context = new BankDatabase();
var element = context.Cashiers.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cashiers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,145 @@
using BankContracts.BindingModels.Cashier;
using BankContracts.SearchModels.Cashier;
using BankContracts.StoragesModels.Cashier;
using BankContracts.ViewModels.Cashier.ViewModels;
using BankDatabaseImplement.Models.CashierModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.CashierImplements
{
public class MoneyTransferStorage : IMoneyTransferStorage
{
public List<MoneyTransferViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.MoneyTransfers
.Include(x => x.AccountPayeer)
.Include(x => x.AccountSender)
.Include(x => x.Cashier)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MoneyTransferViewModel> GetFilteredList(MoneyTransferSearchModel model)
{
using var context = new BankDatabase();
var result = context.MoneyTransfers.Include(x => x.Cashier)
.Include(x => x.AccountPayeer)
.Include(x => x.AccountSender)
.ToList();
if (model.AccountPayeeId.HasValue && model.AccountSenderId.HasValue && model.AccountPayeeId == model.AccountSenderId)
{
return result.Where(x => (x.AccountSenderId == model.AccountSenderId || x.AccountPayeeId == model.AccountPayeeId)
&& x.AccountSender != null)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.AccountPayeeId.HasValue) result = result.Where(x => x.AccountPayeeId == model.AccountPayeeId).ToList();
if (model.AccountSenderId.HasValue) result = result.Where(x => x.AccountSenderId == model.AccountSenderId).ToList();
if (model.CashierId.HasValue) result = result.Where(x => x.CashierId == model.CashierId).ToList();
if (model.DateTransfer.HasValue) result = result.Where(x => x.DateTransfer >= model.DateTransfer).ToList();
if (model.ClientId.HasValue)
{
result = result.Where(x => x.AccountSender != null)
.Where(x => (x.AccountPayeer.ClientId == model.ClientId || x.AccountSender.ClientId == model.ClientId))
.ToList();
}
return result.Select(x => x.GetViewModel).ToList();
}
public MoneyTransferViewModel? GetElement(MoneyTransferSearchModel model)
{
if (model.AccountSenderId < 0 && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.MoneyTransfers
.Include(x => x.AccountPayeer)
.Include(x => x.AccountSender)
.Include(x => x.Cashier)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public MoneyTransferViewModel? Insert(MoneyTransferBindingModel model)
{
using var context = new BankDatabase();
var newMoneyTransfer = MoneyTransfer.Create(context, model);
if (newMoneyTransfer == null)
{
return null;
}
context.MoneyTransfers.Add(newMoneyTransfer);
context.SaveChanges();
return newMoneyTransfer.GetViewModel;
}
public MoneyTransferViewModel? Update(MoneyTransferBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var moneyTransfer = context.MoneyTransfers.FirstOrDefault(rec => rec.Id == model.Id);
if (moneyTransfer == null)
{
return null;
}
moneyTransfer.Update(model);
context.SaveChanges();
transaction.Commit();
return moneyTransfer.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public MoneyTransferViewModel? Delete(MoneyTransferBindingModel model)
{
using var context = new BankDatabase();
var element = context.MoneyTransfers
.Include(x => x.AccountPayeeId)
.Include(x => x.AccountSenderId)
.Include(x => x.Sum)
.Include(x => x.DateTransfer)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.MoneyTransfers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,143 @@
using BankContracts.BindingModels.Client;
using BankContracts.SearchModels.Client;
using BankContracts.StoragesModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using BankDatabaseImplement.Models.ClientModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.ClientImplements
{
public class CardStorage : ICardStorage
{
public CardViewModel? Delete(CardBindingModel model)
{
using var context = new BankDatabase();
var element = context.Cards.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Cards.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public CardViewModel? GetElement(CardSearchModel model)
{
if (string.IsNullOrEmpty(model.Number) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Cards
.Include(x => x.Client)
.Include(x => x.Account)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Number) && x.Number == model.Number) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public List<CardViewModel> GetFilteredList(CardSearchModel model)
{
using var context = new BankDatabase();
if (!string.IsNullOrEmpty(model.Number))
{
return context.Cards
.Include(x => x.Client)
.Include(x => x.Account)
.Where(x => x.Number.Contains(model.Number))
.Select(x => x.GetViewModel)
.ToList();
}
if (model.ClientID.HasValue)
{
return context.Cards
.Include(x => x.Client)
.Include(x => x.Account)
.Where(x => x.ClientId == model.ClientID)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.AccountId.HasValue)
{
return context.Cards
.Include(x => x.Client)
.Include(x => x.Account)
.Where(x => x.AccountId == model.AccountId)
.Select(x => x.GetViewModel)
.ToList();
}
return context.Cards
.Include(x => x.Client)
.Include(x => x.Account)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CardViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Cards
.Include(x => x.Client)
.Include(x => x.Account)
.Select(x => x.GetViewModel)
.ToList();
}
public CardViewModel? Insert(CardBindingModel model)
{
using var context = new BankDatabase();
var newCard = Card.Create(context, model);
if (newCard == null)
{
return null;
}
context.Cards.Add(newCard);
context.SaveChanges();
return newCard.GetViewModel;
}
public CardViewModel? Update(CardBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var card = context.Cards.FirstOrDefault(rec => rec.Id == model.Id);
if (card == null)
{
return null;
}
card.Update(model);
context.SaveChanges();
transaction.Commit();
return card.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
}
}

View File

@ -0,0 +1,92 @@
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? 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;
}
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;
}
}
}

View File

@ -0,0 +1,124 @@
using BankContracts.BindingModels.Client;
using BankContracts.SearchModels.Client;
using BankContracts.StoragesModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using BankDatabaseImplement.Models.ClientModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.ClientImplements
{
public class CreditingStorage : ICreditingStorage
{
public List<CreditingViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Creditings
.Include(x => x.Card)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CreditingViewModel> GetFilteredList(CreditingSearchModel model)
{
using var context = new BankDatabase();
var result = context.Creditings.Include(x => x.Card).ToList();
if (model.CardId.HasValue) result = result.Where(x => x.CardId == model.CardId).ToList();
if (model.ClientId.HasValue)
{
List<int> cards = context.Cards.Where(x => x.ClientId == model.ClientId).Select(x => x.Id).ToList();
result = result.Where(x => cards.Contains(x.CardId)).ToList();
}
return result.Select(x => x.GetViewModel).ToList();
}
public CreditingViewModel? GetElement(CreditingSearchModel model)
{
if (model.CardId < 0 && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Creditings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public CreditingViewModel? Insert(CreditingBindingModel model)
{
using var context = new BankDatabase();
var newCrediting = Crediting.Create(context, model);
if (newCrediting == null)
{
return null;
}
context.Creditings.Add(newCrediting);
context.SaveChanges();
return newCrediting.GetViewModel;
}
public CreditingViewModel? Update(CreditingBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var crediting = context.Creditings.FirstOrDefault(rec => rec.Id == model.Id);
if (crediting == null)
{
return null;
}
crediting.Update(model);
context.SaveChanges();
transaction.Commit();
return context.Creditings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public CreditingViewModel? Delete(CreditingBindingModel model)
{
using var context = new BankDatabase();
var element = context.Creditings
.Include(x => x.CardId)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Creditings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,122 @@
using BankContracts.BindingModels.Client;
using BankContracts.SearchModels.Client;
using BankContracts.StoragesModels.Client;
using BankContracts.ViewModels.Client.ViewModels;
using BankDatabaseImplement.Models.ClientModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements.ClientImplements
{
public class DebitingStorage : IDebitingStorage
{
public List<DebitingViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Debitings
.Include(x => x.Card)
.Select(x => x.GetViewModel)
.ToList();
}
public List<DebitingViewModel> GetFilteredList(DebitingSearchModel model)
{
using var context = new BankDatabase();
var result = context.Debitings.Include(x => x.Card).ToList();
//для получения всех заявок на стнятие
if (model.CardId.HasValue) result = result.Where(x => x.CardId == model.CardId).ToList();
if (model.ClientId.HasValue)
{
List<int> cards = context.Cards.Where(x => x.ClientId == model.ClientId).Select(x => x.Id).ToList();
result = result.Where(x => cards.Contains(x.CardId)).ToList();
}
return result.Select(x => x.GetViewModel).ToList();
}
public DebitingViewModel? GetElement(DebitingSearchModel model)
{
if (model.CardId < 0 && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Debitings
.Include(x => x.Card)
.FirstOrDefault(x => (!(model.CardId < 0) && x.CardId == model.CardId) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public DebitingViewModel? Insert(DebitingBindingModel model)
{
using var context = new BankDatabase();
var newDebiting = Debiting.Create(context, model);
if (newDebiting == null)
{
return null;
}
context.Debitings.Add(newDebiting);
context.SaveChanges();
return newDebiting.GetViewModel;
}
public DebitingViewModel? Update(DebitingBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var debiting = context.Debitings.FirstOrDefault(rec => rec.Id == model.Id);
debiting.Update(model);
context.SaveChanges();
transaction.Commit();
return context.Debitings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DebitingViewModel? Delete(DebitingBindingModel model)
{
using var context = new BankDatabase();
var element = context.Debitings
.Include(x => x.CardId)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Debitings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,4 +1,6 @@
using BankDatabaseImplement.Models.ClientModels;
using BankContracts.BindingModels.Cashier;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models.ClientModels;
using BankDataModels.Enums;
using BankDataModels.Models.Cashier;
using System;
@ -15,22 +17,76 @@ namespace BankDatabaseImplement.Models.CashierModels
public class Account : IAccountModel
{
public int Id { get; set; }
[Required]
public string AccountNumber { get; set; } = string.Empty;
[Required]
public int CashierId { get; set; }
[Required]
public int ClientId { get; set; }
//чтобы передать ФИО клиета
//для передачи ФИО клиента
public virtual Client Client { get; set; }
[Required]
public double Balance { get; set; }
[Required]
public DateTime DateOpen { get; set; }
public DateTime DateOpen { get; set; } = DateTime.Now;
[Required]
public StatusAccount StatusAccount { get; set; }
public StatusAccount StatusAccount { get; set; } = StatusAccount.Открыт;
//для реализации связи один ко многим со Снятием наличных
[ForeignKey("AccountId")]
public virtual List<CashWithdrawal> CashWithdrawals { get; set; } = new();
//для реализации связи один ко многим с Переводом денег
[NotMapped]
[ForeignKey("AccountSenderId")]
public virtual List<MoneyTransfer> MoneyTransferSenders { get; set; } = new();
[NotMapped]
[ForeignKey("AccountPayeeId")]
public virtual List<MoneyTransfer> MoneyTransferPayees { get; set; } = new();
//для реализации связи один ко многим с Картами
[ForeignKey("AccountId")]
public virtual List<Card> Cards { get; set; } = new();
public static Account Create(BankDatabase context, AccountBindingModel model)
{
return new Account()
{
Id = model.Id,
ClientId = model.ClientId,
Client = context.Clients.First(x => x.Id == model.ClientId),
Balance = model.Balance,
DateOpen = model.DateOpen,
CashierId = model.CashierId,
AccountNumber = model.AccountNumber,
StatusAccount = model.StatusAccount
};
}
public void Update(AccountBindingModel model)
{
Balance = model.Balance;
}
public AccountViewModel GetViewModel => new()
{
Id = Id,
CashierId = CashierId,
ClientId = ClientId,
Name = Client.Name,
Patronymic = Client.Patronymic,
AccountNumber = AccountNumber,
Balance = Balance,
DateOpen = DateOpen,
StatusAccount = StatusAccount
};
}
}

View File

@ -38,7 +38,7 @@ namespace BankDatabaseImplement.Models.ClientModels
public DateTime Period { get; set; } = DateTime.Now;
[Required]
public StatusCard StatusCard { get; set; } = StatusCard.Закрыта;
public StatusCard StatusCard { get; set; } = StatusCard.Открыта;
[Required]
public string? ClientSurname { get; set; }