2023-04-07 01:20:11 +04:00
|
|
|
|
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();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Payments.Include(x => x.Operator)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<PaymentViewModel> GetFilteredList(PaymentSearchModel 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.Payments.Include(x => x.Operator)
|
|
|
|
|
.Where(x => x.OperatorId == model.OperatorId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
return context.Payments.Include(x => x.Operator)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 01:20:11 +04:00
|
|
|
|
}
|
|
|
|
|
public PaymentViewModel? GetElement(PaymentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Payments.Include(x => x.Operator)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
.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();
|
2023-04-07 11:58:10 +04:00
|
|
|
|
var payment = context.Payments.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (payment == null)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-07 11:58:10 +04:00
|
|
|
|
payment.Update(model);
|
|
|
|
|
payment.UpdateDeals(context, model);
|
|
|
|
|
payment.UpdateCurrencies(context, model);
|
2023-04-07 01:20:11 +04:00
|
|
|
|
context.SaveChanges();
|
2023-04-07 11:58:10 +04:00
|
|
|
|
return payment.GetViewModel;
|
2023-04-07 01:20:11 +04:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|