2023-04-07 15:42:05 +04:00
|
|
|
|
using BankContracts.BindingModels;
|
|
|
|
|
using BankContracts.SearchModels;
|
|
|
|
|
using BankContracts.StoragesContracts;
|
|
|
|
|
using BankContracts.ViewModels;
|
|
|
|
|
using BankDatabaseImplement.Models;
|
2023-04-07 17:06:08 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-07 15:42:05 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class CreditProgramStorage : ICreditProgramStorage
|
|
|
|
|
{
|
|
|
|
|
public CreditProgramViewModel? Delete(CreditProgramBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var element = context.CreditPrograms.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.CreditPrograms.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CreditProgramViewModel? GetElement(CreditProgramSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 17:06:08 +04:00
|
|
|
|
return context.CreditPrograms.Include(x => x.BankOperator)
|
2023-04-07 15:42:05 +04:00
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CreditProgramViewModel> GetFilteredList(CreditProgramSearchModel model)
|
|
|
|
|
{
|
2023-04-07 17:06:08 +04:00
|
|
|
|
if (!model.Id.HasValue && !model.BankOperatorId.HasValue)
|
2023-04-07 15:42:05 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
2023-04-07 17:06:08 +04:00
|
|
|
|
if (model.BankOperatorId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
return context.CreditPrograms.Include(x => x.BankOperator)
|
|
|
|
|
.Where(x => x.BankOperatorId == model.BankOperatorId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
return context.CreditPrograms.Include(x => x.BankOperator)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-07 15:42:05 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CreditProgramViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
2023-04-07 17:06:08 +04:00
|
|
|
|
return context.CreditPrograms.Include(x => x.BankOperator)
|
2023-04-07 15:42:05 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CreditProgramViewModel? Insert(CreditProgramBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var newCreditProgram = CreditProgram.Create(context, model);
|
|
|
|
|
if (newCreditProgram == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.CreditPrograms.Add(newCreditProgram);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newCreditProgram.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CreditProgramViewModel? Update(CreditProgramBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankDatabase();
|
|
|
|
|
var creditProgram = context.CreditPrograms.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (creditProgram == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
creditProgram.Update(model);
|
|
|
|
|
creditProgram.UpdateCurrencies(context, model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return creditProgram.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|