87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using BankContracts.BindingModels;
|
|
using BankContracts.SearchModels;
|
|
using BankContracts.StoragesContracts;
|
|
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 PaymentStorage : IPaymentStorage
|
|
{
|
|
public List<PaymentViewModel> GetFullList()
|
|
{
|
|
using var context = new BankDatabase();
|
|
return context.Payments
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<PaymentViewModel> GetFilteredList(PaymentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new BankDatabase();
|
|
return context.Payments
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public PaymentViewModel? GetElement(PaymentSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new BankDatabase();
|
|
return context.Payments
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
?.GetViewModel;
|
|
}
|
|
public PaymentViewModel? Insert(PaymentBindingModel model)
|
|
{
|
|
using var context = new BankDatabase();
|
|
var newPayment = Payment.Create(context, model);
|
|
if (newPayment == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Payments.Add(newPayment);
|
|
context.SaveChanges();
|
|
return newPayment.GetViewModel;
|
|
}
|
|
public PaymentViewModel? Update(PaymentBindingModel model)
|
|
{
|
|
using var context = new BankDatabase();
|
|
var payment = context.Payments.FirstOrDefault(x => x.Id == model.Id);
|
|
if (payment == null)
|
|
{
|
|
return null;
|
|
}
|
|
payment.Update(model);
|
|
payment.UpdateDeals(context, model);
|
|
payment.UpdateCurrencies(context, model);
|
|
context.SaveChanges();
|
|
return payment.GetViewModel;
|
|
}
|
|
public PaymentViewModel? Delete(PaymentBindingModel model)
|
|
{
|
|
using var context = new BankDatabase();
|
|
var element = context.Payments.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Payments.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
}
|