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 GetFullList() { using var context = new BankDatabase(); return context.Payments .Select(x => x.GetViewModel) .ToList(); } public List 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 deal = context.Payments.FirstOrDefault(x => x.Id == model.Id); if (deal == null) { return null; } deal.Update(model); context.SaveChanges(); return deal.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; } } }