CourseWork_Bank/Bank/BankDatabaseImplement/Implements/CurrencyPurchaseStorage.cs

109 lines
4.0 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 CurrencyPurchaseStorage : ICurrencyPurchaseStorage
{
public CurrencyPurchaseViewModel? Delete(CurrencyPurchaseBindingModel model)
{
using var context = new BankDatabase();
var currencyPurchase = context.CurrencyPurchases.FirstOrDefault(x => x.Id == model.Id);
if (currencyPurchase == null)
{
return null;
}
currencyPurchase.Update(model);
context.SaveChanges();
return currencyPurchase.GetViewModel;
}
public CurrencyPurchaseViewModel? GetElement(CurrencyPurchaseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new BankDatabase();
return context.CurrencyPurchases.Include(x => x.BankOperator).Include(x=>x.Currency)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public List<CurrencyPurchaseViewModel> GetFilteredList(CurrencyPurchaseSearchModel model)
{
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue && !model.BankOperatorId.HasValue)
{
return new();
}
if (model.DateFrom.HasValue && model.DateTo.HasValue)
{
using var context = new BankDatabase();
return context.CurrencyPurchases.Include(x => x.BankOperator).Include(x => x.Currency)
.Where(x => x.PurchaseDate >= model.DateFrom && x.PurchaseDate <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.BankOperatorId.HasValue)
{
using var context = new BankDatabase();
return context.CurrencyPurchases.Include(x => x.BankOperator).Include(x => x.Currency)
.Where(x => x.BankOperatorId == model.BankOperatorId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
using var context = new BankDatabase();
return context.CurrencyPurchases.Include(x => x.BankOperator).Include(x => x.Currency)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
}
public List<CurrencyPurchaseViewModel> GetFullList()
{
using var context = new BankDatabase();
return context.CurrencyPurchases.Include(x => x.BankOperator).Include(x=> x.Currency)
.Select(x => x.GetViewModel)
.ToList();
}
public CurrencyPurchaseViewModel? Insert(CurrencyPurchaseBindingModel model)
{
using var context = new BankDatabase();
var newCurrencyPurchase = CurrencyPurchase.Create(context, model);
if (newCurrencyPurchase == null)
{
return null;
}
context.CurrencyPurchases.Add(newCurrencyPurchase);
context.SaveChanges();
return newCurrencyPurchase.GetViewModel;
}
public CurrencyPurchaseViewModel? Update(CurrencyPurchaseBindingModel model)
{
using var context = new BankDatabase();
var currencyPurchase = context.CurrencyPurchases.FirstOrDefault(x => x.Id == model.Id);
if (currencyPurchase == null)
{
return null;
}
currencyPurchase.Update(model);
context.SaveChanges();
return currencyPurchase.GetViewModel;
}
}
}