CourseWork_Bank/Bank/BankDatabaseImplement/Implements/PaymentStorage.cs

99 lines
3.2 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.Include(x => x.Operator)
.Select(x => x.GetViewModel)
.ToList();
}
public List<PaymentViewModel> GetFilteredList(PaymentSearchModel model)
{
if (!model.Id.HasValue && !model.OperatorId.HasValue)
{
return new();
}
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();
}
}
public PaymentViewModel? GetElement(PaymentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.Payments.Include(x => x.Operator)
.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;
}
}
}