add storages

This commit is contained in:
Zakharov_Rostislav 2024-04-28 21:09:30 +04:00
parent 9512a118d0
commit 33a815d7cb
9 changed files with 435 additions and 10 deletions

View File

@ -9,7 +9,8 @@ namespace BankContracts.SearchModels
public class TransferSearchModel
{
public int? Id { get; set; }
public DateTime? TransferTime { get; set; }
public DateTime? DateTo { get; set; }
public DateTime? DateFrom { get; set; }
public int? OperationId { get; set; }
public int? SenderAccountId { get; set; }
public int? RecipientAccountId { get; set; }

View File

@ -9,7 +9,8 @@ namespace BankContracts.SearchModels
public class WithdrawalSearchModel
{
public int? Id { get; set; }
public DateTime? WithdrawalTime { get; set; }
public DateTime? DateTo { get; set; }
public DateTime? DateFrom { get; set; }
public int? RequestId { get; set; }
}
}

View File

@ -11,11 +11,11 @@ namespace BankContracts.StoragesContracts
{
public interface ITransferStorage
{
List<WithdrawalViewModel> GetFullList();
List<WithdrawalViewModel> GetFilteredList(WithdrawalSearchModel model);
WithdrawalViewModel? GetElement(WithdrawalSearchModel model);
WithdrawalViewModel? Insert(WithdrawalBindingModel model);
WithdrawalViewModel? Update(WithdrawalBindingModel model);
WithdrawalViewModel? Delete(WithdrawalBindingModel model);
List<TransferViewModel> GetFullList();
List<TransferViewModel> GetFilteredList(TransferSearchModel model);
TransferViewModel? GetElement(TransferSearchModel model);
TransferViewModel? Insert(TransferBindingModel model);
TransferViewModel? Update(TransferBindingModel model);
TransferViewModel? Delete(TransferBindingModel model);
}
}

View File

@ -19,6 +19,5 @@ namespace BankContracts.ViewModels
[DisplayName("Статус заявки")]
public RequestStatus Status { get; set; }
public Dictionary<int, ICardModel> CardRequests { get; set; } = new();
}
}

View File

@ -16,6 +16,6 @@ namespace BankContracts.ViewModels
public DateTime WithdrawalTime { get; set; } = DateTime.Now;
[DisplayName("Номер заявки")]
public int? RequestId { get; set; }
public Dictionary<int, IAccountModel> WithdrawalAccounts = new Dictionary<int, IAccountModel>();
public Dictionary<int, IAccountModel> WithdrawalAccounts { get; set; } = new();
}
}

View File

@ -0,0 +1,111 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
internal class AccountStorage : IAccountStorage
{
public List<AccountViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Accounts
.Include(x => x.ManagerId)
.Include(x => x.AccountWithdrawals)
.Include(x => x.SenderTransfers)
.Include(x => x.RecipientTransfers)
.Select(x => x.GetViewModel).ToList();
}
public List<AccountViewModel> GetFilteredList(AccountSearchModel model)
{
using var context = new BankDatabase();
return context.Accounts
.Include(x => x.ManagerId)
.Include(x => x.AccountWithdrawals)
.Include(x => x.SenderTransfers)
.Include(x => x.RecipientTransfers)
.Where(x =>
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.ManagerId.HasValue || x.ManagerId == model.ManagerId) &&
(string.IsNullOrEmpty(model.Number) || x.Number == model.Number)
)
.Select(x => x.GetViewModel)
.ToList();
}
public AccountViewModel? GetElement(AccountSearchModel model)
{
using var context = new BankDatabase();
if (model == null || (!model.Id.HasValue && string.IsNullOrEmpty(model.Number)))
{
return null;
}
return context.Accounts
.Include(x => x.ManagerId)
.Include(x => x.AccountWithdrawals)
.Include(x => x.SenderTransfers)
.Include(x => x.RecipientTransfers)
.FirstOrDefault(x =>
(!model.Id.HasValue || x.Id == model.Id) &&
(string.IsNullOrEmpty(model.Number) || x.Number == model.Number)
)?.GetViewModel;
}
public AccountViewModel? Insert(AccountBindingModel model)
{
var newAccount = Account.Create(model);
if (newAccount == null)
{
return null;
}
using var context = new BankDatabase();
context.Accounts.Add(newAccount);
context.SaveChanges();
return newAccount.GetViewModel;
}
public AccountViewModel? Update(AccountBindingModel model)
{
using var context = new BankDatabase();
var account = context.Accounts
.Include(x => x.ManagerId)
.Include(x => x.AccountWithdrawals)
.Include(x => x.SenderTransfers)
.Include(x => x.RecipientTransfers)
.FirstOrDefault(x => x.Id == model.Id);
if (account == null)
{
return null;
}
account.Update(model);
context.SaveChanges();
return account.GetViewModel;
}
public AccountViewModel? Delete(AccountBindingModel model)
{
using var context = new BankDatabase();
var account = context.Accounts
.Include(x => x.ManagerId)
.Include(x => x.AccountWithdrawals)
.Include(x => x.SenderTransfers).Include(x => x.RecipientTransfers)
.FirstOrDefault(rec => rec.Id == model.Id);
if (account != null)
{
context.Accounts.Remove(account);
context.SaveChanges();
return account.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,95 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class ManagerStorage : IManagerStorage
{
public List<ManagerViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Managers.Include(x => x.Accounts).Select(x => x.GetViewModel).ToList();
}
public List<ManagerViewModel> GetFilteredList(ManagerSearchModel model)
{
if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password))
{
return new();
}
using var context = new BankDatabase();
return context.Managers
.Include(x => x.Accounts)
.Where(x =>
string.IsNullOrEmpty(model.Fio) || x.Fio.Contains(model.Fio) &&
(string.IsNullOrEmpty(model.Email) || x.Email.Contains(model.Email)) &&
(string.IsNullOrEmpty(model.Password) || x.Password.Contains(model.Password)))
.Select(x => x.GetViewModel).ToList();
}
public ManagerViewModel? GetElement(ManagerSearchModel model)
{
if (string.IsNullOrEmpty(model.Fio) && string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Managers
.Include(x => x.Accounts)
.FirstOrDefault(x =>
(string.IsNullOrEmpty(model.Fio) || x.Fio == model.Fio) &&
(!model.Id.HasValue || x.Id == model.Id) &&
(string.IsNullOrEmpty(model.Email) || x.Email == model.Email) &&
(string.IsNullOrEmpty(model.Password) || x.Password == model.Password))?
.GetViewModel;
}
public ManagerViewModel? Insert(ManagerBindingModel model)
{
var newManager = Manager.Create(model);
if (newManager == null)
{
return null;
}
using var context = new BankDatabase();
context.Managers.Add(newManager);
context.SaveChanges();
return newManager.GetViewModel;
}
public ManagerViewModel? Update(ManagerBindingModel model)
{
using var context = new BankDatabase();
var manager = context.Managers.FirstOrDefault(x => x.Id == model.Id);
if (manager == null)
{
return null;
}
manager.Update(model);
context.SaveChanges();
return manager.GetViewModel;
}
public ManagerViewModel? Delete(ManagerBindingModel model)
{
using var context = new BankDatabase();
var element = context.Managers.Include(x => x.Accounts).FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Managers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,104 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class TransferStorage : ITransferStorage
{
public List<TransferViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Transfers
.Include(x => x.SenderAccount)
.Include(x => x.RecipientAccount)
.Include (x => x.Operation)
.Select(x => x.GetViewModel).ToList();
}
public List<TransferViewModel> GetFilteredList(TransferSearchModel model)
{
using var context = new BankDatabase();
return context.Transfers
.Include(x => x.SenderAccount)
.Include(x => x.RecipientAccount)
.Include(x => x.Operation)
.Where(x =>
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.SenderAccountId.HasValue || x.SenderAccountId == model.SenderAccountId) &&
(!model.RecipientAccountId.HasValue || x.RecipientAccountId == model.RecipientAccountId) &&
(!model.OperationId.HasValue || x.OperationId == model.OperationId) &&
(!model.DateFrom.HasValue || x.TransferTime >= model.DateFrom) &&
(!model.DateTo.HasValue || x.TransferTime <= model.DateTo)
).Select(x => x.GetViewModel).ToList();
}
public TransferViewModel? GetElement(TransferSearchModel model)
{
if (!model.Id.HasValue)
return null;
using var context = new BankDatabase();
return context.Transfers
.Include(x => x.SenderAccount)
.Include(x => x.RecipientAccount)
.Include(x => x.Operation)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?
.GetViewModel;
}
public TransferViewModel? Insert(TransferBindingModel model)
{
var newTransfer = Transfer.Create(model);
if (newTransfer == null)
{
return null;
}
using var context = new BankDatabase();
context.Transfers.Add(newTransfer);
context.SaveChanges();
return newTransfer.GetViewModel;
}
public TransferViewModel? Update(TransferBindingModel model)
{
using var context = new BankDatabase();
var operation = context.Transfers
.Include(x => x.SenderAccount)
.Include(x => x.RecipientAccount)
.Include(x => x.Operation)
.FirstOrDefault(x => x.Id == model.Id);
if (operation == null)
{
return null;
}
operation.Update(model);
context.SaveChanges();
return operation.GetViewModel;
}
public TransferViewModel? Delete(TransferBindingModel model)
{
using var context = new BankDatabase();
var transfer = context.Transfers
.Include(x => x.SenderAccount)
.Include(x => x.RecipientAccount)
.Include(x => x.Operation)
.FirstOrDefault(rec => rec.Id == model.Id);
if (transfer != null)
{
context.Transfers.Remove(transfer);
context.SaveChanges();
return transfer.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,114 @@
using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDatabaseImplement.Implements
{
public class WithdrawalStorage : IWithdrawalStorage
{
public List<WithdrawalViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.Withdrawals
.Include(x => x.Request)
.Include(x => x.Accounts)
.ThenInclude(x => x.Account)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<WithdrawalViewModel> GetFilteredList(WithdrawalSearchModel model)
{
using var context = new BankDatabase();
return context.Withdrawals
.Include(x => x.Request)
.Include(x => x.Accounts)
.ThenInclude(x => x.Account)
.Where(x =>
(!model.Id.HasValue || x.Id == model.Id) &&
(!model.RequestId.HasValue || x.RequestId == model.RequestId) &&
(!model.DateFrom.HasValue || x.WithdrawalTime >= model.DateFrom) &&
(!model.DateTo.HasValue || x.WithdrawalTime <= model.DateTo))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public WithdrawalViewModel? GetElement(WithdrawalSearchModel model)
{
if (!model.Id.HasValue)
return null;
using var context = new BankDatabase();
return context.Withdrawals
.Include(x => x.Request)
.Include(x => x.Accounts)
.ThenInclude(x => x.Account)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
}
public WithdrawalViewModel? Insert(WithdrawalBindingModel model)
{
using var context = new BankDatabase();
var newWithdrawal = Withdrawal.Create(context, model);
if (newWithdrawal == null)
{
return null;
}
context.Withdrawals.Add(newWithdrawal);
context.SaveChanges();
return newWithdrawal.GetViewModel;
}
public WithdrawalViewModel? Update(WithdrawalBindingModel model)
{
using var context = new BankDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var Withdrawal = context.Withdrawals.FirstOrDefault(rec =>
rec.Id == model.Id);
if (Withdrawal == null)
{
return null;
}
Withdrawal.Update(model);
context.SaveChanges();
Withdrawal.UpdateAccounts(context, model);
transaction.Commit();
return Withdrawal.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public WithdrawalViewModel? Delete(WithdrawalBindingModel model)
{
using var context = new BankDatabase();
var element = context.Withdrawals
.Include(x => x.Request)
.Include(x => x.Accounts)
.ThenInclude(x => x.Account)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Withdrawals.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}