2023-04-01 20:49:00 +04:00
|
|
|
|
using BankYouBankruptContracts.BindingModels;
|
|
|
|
|
using BankYouBankruptContracts.SearchModels;
|
|
|
|
|
using BankYouBankruptContracts.StoragesContracts;
|
|
|
|
|
using BankYouBankruptContracts.ViewModels;
|
2023-04-01 22:54:15 +04:00
|
|
|
|
using BankYouBankruptDatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-01 20:49:00 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankYouBankruptDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class AccountStorage : IAccountStorage
|
|
|
|
|
{
|
|
|
|
|
public List<AccountViewModel> GetFullList()
|
|
|
|
|
{
|
2023-04-01 22:54:15 +04:00
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Accounts
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-01 20:49:00 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<AccountViewModel> GetFilteredList(AccountSearchModel model)
|
|
|
|
|
{
|
2023-04-01 22:54:15 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.AccountNumber))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Accounts
|
|
|
|
|
.Where(x => x.AccountNumber.Contains(model.AccountNumber))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-01 20:49:00 +04:00
|
|
|
|
}
|
2023-04-01 22:54:15 +04:00
|
|
|
|
|
2023-04-01 20:49:00 +04:00
|
|
|
|
public AccountViewModel? GetElement(AccountSearchModel model)
|
|
|
|
|
{
|
2023-04-01 22:54:15 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.AccountNumber) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Accounts
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.AccountNumber) && x.AccountNumber == model.AccountNumber) ||
|
|
|
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
|
|
|
?.GetViewModel;
|
2023-04-01 20:49:00 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AccountViewModel? Insert(AccountBindingModel model)
|
|
|
|
|
{
|
2023-04-01 22:54:15 +04:00
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
var newAccount = Account.Create(context, model);
|
|
|
|
|
|
|
|
|
|
if (newAccount == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Accounts.Add(newAccount);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newAccount.GetViewModel;
|
2023-04-01 20:49:00 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public AccountViewModel? Update(AccountBindingModel model)
|
|
|
|
|
{
|
2023-04-01 22:54:15 +04:00
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
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 account.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
2023-04-01 20:49:00 +04:00
|
|
|
|
}
|
2023-04-01 22:54:15 +04:00
|
|
|
|
|
2023-04-01 20:49:00 +04:00
|
|
|
|
public AccountViewModel? Delete(AccountBindingModel model)
|
|
|
|
|
{
|
2023-04-01 22:54:15 +04:00
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
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;
|
2023-04-01 20:49:00 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|