CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/AccountStorage.cs

143 lines
3.2 KiB
C#

using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
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()
{
using var context = new BankYouBancruptDatabase();
return context.Accounts
.Include(x => x.Client)
.Select(x => x.GetViewModel)
.ToList();
}
public List<AccountViewModel> GetFilteredList(AccountSearchModel model)
{
using var context = new BankYouBancruptDatabase();
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 BankYouBancruptDatabase();
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 BankYouBancruptDatabase();
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 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;
}
}
public AccountViewModel? Delete(AccountBindingModel model)
{
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;
}
}
}