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;
|
2023-04-06 18:53:42 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankDatabaseImplement.Implements
|
|
|
|
|
{
|
2023-04-07 01:20:11 +04:00
|
|
|
|
public class DealStorage : IDealStorage
|
2023-04-06 18:53:42 +04:00
|
|
|
|
{
|
2023-04-07 01:20:11 +04:00
|
|
|
|
public List<DealViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Deals.Include(x => x.Operator).Include(x => x.CreditProgram)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
public List<DealViewModel> GetFilteredList(DealSearchModel model)
|
|
|
|
|
{
|
2023-04-07 13:15:01 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.OperatorId.HasValue)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2023-04-07 13:15:01 +04:00
|
|
|
|
if (model.OperatorId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Deals.Include(x => x.Operator).Include(x => x.CreditProgram)
|
2023-04-07 13:15:01 +04:00
|
|
|
|
.Where(x => x.OperatorId == model.OperatorId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Deals.Include(x => x.Operator).Include(x => x.CreditProgram)
|
2023-04-07 13:15:01 +04:00
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-07 01:20:11 +04:00
|
|
|
|
}
|
|
|
|
|
public DealViewModel? GetElement(DealSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 13:42:01 +04:00
|
|
|
|
return context.Deals.Include(x => x.Operator).Include(x => x.CreditProgram)
|
2023-04-07 01:20:11 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public DealViewModel? Insert(DealBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var newDeal = Deal.Create(context, model);
|
|
|
|
|
if (newDeal == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Deals.Add(newDeal);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newDeal.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public DealViewModel? Update(DealBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var deal = context.Deals.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (deal == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
deal.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return deal.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public DealViewModel? Delete(DealBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var element = context.Deals.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Deals.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-04-06 18:53:42 +04:00
|
|
|
|
}
|
|
|
|
|
}
|