141 lines
3.9 KiB
C#
141 lines
3.9 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.Net.NetworkInformation;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace BankYouBankruptDatabaseImplement.Implements
|
|
{
|
|
public class MoneyTransferStorage : IMoneyTransferStorage
|
|
{
|
|
public List<MoneyTransferViewModel> GetFullList()
|
|
{
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
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 BankYouBancruptDatabase();
|
|
var result = context.MoneyTransfers.Include(x => x.Cashier)
|
|
.Include(x => x.AccountPayeer)
|
|
.Include(x => x.AccountSender)
|
|
.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.DateFrom.HasValue) result = result.Where(x => x.DateOperation >= model.DateFrom).ToList();
|
|
|
|
if (model.DateTo.HasValue) result = result.Where(x => x.DateOperation <= model.DateTo).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 BankYouBancruptDatabase();
|
|
|
|
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 BankYouBancruptDatabase();
|
|
|
|
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 BankYouBancruptDatabase();
|
|
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 BankYouBancruptDatabase();
|
|
var element = context.MoneyTransfers
|
|
.Include(x => x.AccountPayeeId)
|
|
.Include(x => x.AccountSenderId)
|
|
.Include(x => x.Sum)
|
|
.Include(x => x.DateOperation)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
if (element != null)
|
|
{
|
|
context.MoneyTransfers.Remove(element);
|
|
context.SaveChanges();
|
|
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|