84 lines
2.7 KiB
C#
84 lines
2.7 KiB
C#
using BankContracts.BindingModels;
|
|
using BankContracts.SearchModels;
|
|
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
|
|
{
|
|
public List<TransferViewModel> GetFullList()
|
|
{
|
|
using var context = new BankDatabase();
|
|
return context.Transfers.Include(x => x.Payment)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<TransferViewModel> GetFilteredList(TransferSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new BankDatabase();
|
|
return context.Transfers.Include(x => x.Payment)
|
|
.Where(x => x.Id == model.Id)
|
|
.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.Payment)
|
|
.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();
|
|
var transfer = context.Transfers.FirstOrDefault(x => x.Id == model.Id);
|
|
if (transfer == null)
|
|
{
|
|
return null;
|
|
}
|
|
transfer.Update(model);
|
|
context.SaveChanges();
|
|
return transfer.GetViewModel;
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|