using BankContracts.BindingModels; using BankContracts.SearchModels; using BankContracts.StoragesContracts; using BankContracts.ViewModels; using BankDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Query; namespace BankDatabaseImplement.Implements { public class PaymentStorage : IPaymentStorage { private static IIncludableQueryable Payments(BankDB context) => context.Payments.Include(x => x.OperationByPurchase).ThenInclude(x => x.Operation); public List GetFullList() { using var context = new BankDB(); return Payments(context) .Select(x => x.GetViewModel) .ToList(); } public List 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; } } }