2024-04-28 20:12:32 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using BankDatabaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
2024-04-28 18:50:40 +04:00
|
|
|
|
|
|
|
|
|
namespace BankDatabaseImplement.Implements
|
|
|
|
|
{
|
2024-04-28 20:12:32 +04:00
|
|
|
|
public class PaymentStorage : IPaymentStorage
|
2024-04-28 18:50:40 +04:00
|
|
|
|
{
|
2024-04-28 20:12:32 +04:00
|
|
|
|
private static IIncludableQueryable<Payment, Operation?> Payments(BankDB context)
|
|
|
|
|
=> context.Payments.Include(x => x.OperationByPurchase).ThenInclude(x => x.Operation);
|
|
|
|
|
|
|
|
|
|
public List<PaymentViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
return Payments(context)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<PaymentViewModel> GetFilteredList(PaymentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model), "Получена пустая поисковая модель");
|
|
|
|
|
}
|
|
|
|
|
if (model.DateFrom.HasValue && !model.DateTo.HasValue || model.DateTo.HasValue && !model.DateFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Получена поисковая модель только с началом или концом периода");
|
|
|
|
|
}
|
|
|
|
|
if (!model.DateFrom.HasValue && !model.OperationId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model.OperationId), "Получена поисковая модель без OperationId");
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
if (!model.DateFrom.HasValue && !model.PurchaseId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model.PurchaseId), "Получена поисковая модель без PurchaseId");
|
|
|
|
|
}
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
return Payments(context)
|
|
|
|
|
.Where(x => model.DateFrom.Value <= x.Date && x.Date <= model.DateTo.Value)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return Payments(context)
|
|
|
|
|
.Where(x => x.OperationByPurchase != null &&
|
|
|
|
|
x.OperationByPurchase.OperationId == model.OperationId &&
|
|
|
|
|
x.OperationByPurchase.PurchaseId == model.PurchaseId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public PaymentViewModel? GetElement(PaymentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
return Payments(context)
|
|
|
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public PaymentViewModel? Insert(PaymentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
var newPayment = Payment.Create(model);
|
|
|
|
|
using var context = new BankDB();
|
|
|
|
|
context.Payments.Add(newPayment);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newPayment.GetViewModel;
|
|
|
|
|
}
|
2024-04-28 18:50:40 +04:00
|
|
|
|
}
|
|
|
|
|
}
|