2023-04-01 19:08:55 +04:00
|
|
|
|
using BankYouBankruptContracts.BindingModels;
|
|
|
|
|
using BankYouBankruptContracts.SearchModels;
|
2023-04-01 23:47:54 +04:00
|
|
|
|
using BankYouBankruptContracts.StoragesContracts;
|
2023-04-01 19:08:55 +04:00
|
|
|
|
using BankYouBankruptContracts.ViewModels;
|
|
|
|
|
using BankYouBankruptDatabaseImplement.Models;
|
2023-05-14 14:33:49 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-01 19:08:55 +04:00
|
|
|
|
using System;
|
2023-04-01 21:03:14 +04:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BankYouBankruptDatabaseImplement.Implements
|
|
|
|
|
{
|
2023-04-01 23:47:54 +04:00
|
|
|
|
public class CardStorage : ICardStorage
|
2023-04-01 21:03:14 +04:00
|
|
|
|
{
|
2023-04-01 19:08:55 +04:00
|
|
|
|
public CardViewModel? Delete(CardBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
var element = context.Cards.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Cards.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CardViewModel? GetElement(CardSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.Number) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Cards
|
2023-05-14 15:28:22 +04:00
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Number) && x.Number == model.Number) ||
|
2023-04-01 19:08:55 +04:00
|
|
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CardViewModel> GetFilteredList(CardSearchModel model)
|
|
|
|
|
{
|
2023-05-14 14:33:49 +04:00
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(model.Number))
|
2023-04-01 19:08:55 +04:00
|
|
|
|
{
|
2023-05-14 14:33:49 +04:00
|
|
|
|
return context.Cards
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.Number.Contains(model.Number))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-01 19:08:55 +04:00
|
|
|
|
|
2023-05-14 14:33:49 +04:00
|
|
|
|
if (model.ClientID.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Cards
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Where(x => x.ClientID == model.ClientID)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
2023-04-01 19:08:55 +04:00
|
|
|
|
|
2023-05-14 14:33:49 +04:00
|
|
|
|
return context.Cards
|
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
2023-04-01 19:08:55 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CardViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
return context.Cards
|
2023-05-14 15:28:22 +04:00
|
|
|
|
.Include(x => x.Client)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
2023-04-01 19:08:55 +04:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CardViewModel? Insert(CardBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
|
|
|
|
|
var newCard = Card.Create(context, model);
|
|
|
|
|
|
|
|
|
|
if (newCard == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Cards.Add(newCard);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return newCard.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CardViewModel? Update(CardBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new BankYouBancruptDatabase();
|
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var card = context.Cards.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (card == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
card.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
|
|
|
|
|
return card.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-01 21:03:14 +04:00
|
|
|
|
}
|