2023-04-07 01:20:11 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.SearchModels;
|
2023-04-07 13:15:01 +04:00
|
|
|
|
using BankContracts.StoragesContracts;
|
2023-04-07 01:20:11 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2023-04-07 13:15:01 +04:00
|
|
|
|
public class TransferStorage : ITransferStorage
|
2023-04-07 01:20:11 +04:00
|
|
|
|
{
|
|
|
|
|
public List<TransferViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<TransferViewModel> GetFilteredList(TransferSearchModel model)
|
|
|
|
|
{
|
2023-04-07 13:42:01 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.OperatorId.HasValue)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2023-04-07 13:42:01 +04:00
|
|
|
|
if (model.OperatorId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
|
|
|
|
|
.Where(x => x.OperatorId == model.OperatorId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-07 01:20:11 +04:00
|
|
|
|
}
|
|
|
|
|
public TransferViewModel? GetElement(TransferSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Transfers.Include(x => x.Payment).Include(x => x.OperatorId)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public TransferViewModel? Insert(TransferBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var newTransfer = Transfer.Create(context, model);
|
|
|
|
|
if (newTransfer == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Transfers.Add(newTransfer);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newTransfer.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public TransferViewModel? Update(TransferBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 11:58:10 +04:00
|
|
|
|
var transfer = context.Transfers.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (transfer == null)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-07 11:58:10 +04:00
|
|
|
|
transfer.Update(model);
|
2023-04-07 01:20:11 +04:00
|
|
|
|
context.SaveChanges();
|
2023-04-07 11:58:10 +04:00
|
|
|
|
return transfer.GetViewModel;
|
2023-04-07 01:20:11 +04:00
|
|
|
|
}
|
|
|
|
|
public TransferViewModel? Delete(TransferBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var element = context.Transfers.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Transfers.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|