CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs
2023-05-18 19:48:45 +04:00

132 lines
3.9 KiB
C#

using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels.Client.Default;
using BankYouBankruptDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankYouBankruptDatabaseImplement.Implements
{
public class CreditingStorage : ICreditingStorage
{
public List<CreditingViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.Creditings
.Include(x => x.Card)
.Select(x => x.GetViewModel)
.ToList();
}
public List<CreditingViewModel> GetFilteredList(CreditingSearchModel model)
{
using var context = new BankYouBancruptDatabase();
var result = context.Creditings.Include(x => x.Card).ToList();
if (model.Status.HasValue) result = result.Where(x => x.Status == model.Status).ToList();
if (model.CardId.HasValue) result = result.Where(x => x.CardId == model.CardId).ToList();
if (model.UserId.HasValue)
{
List<int> cards = context.Cards.Where(x => x.ClientID == model.UserId).Select(x => x.Id).ToList();
result = result.Where(x => cards.Contains(x.CardId)).ToList();
}
if (model.DateFrom.HasValue && model.DateTo.HasValue)
{
result = result.Where(x => x.DateOpen >= model.DateFrom && x.DateClose <= model.DateTo).ToList();
}
return result.Select(x => x.GetViewModel).ToList();
}
public CreditingViewModel? GetElement(CreditingSearchModel model)
{
if (model.CardId < 0 && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.Creditings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public CreditingViewModel? Insert(CreditingBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var newCrediting = Crediting.Create(context, model);
if (newCrediting == null)
{
return null;
}
context.Creditings.Add(newCrediting);
context.SaveChanges();
return newCrediting.GetViewModel;
}
public CreditingViewModel? Update(CreditingBindingModel model)
{
using var context = new BankYouBancruptDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var crediting = context.Creditings.FirstOrDefault(rec => rec.Id == model.Id);
if (crediting == null)
{
return null;
}
crediting.Update(model);
context.SaveChanges();
transaction.Commit();
return context.Creditings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public CreditingViewModel? Delete(CreditingBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var element = context.Creditings
.Include(x => x.CardId)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Creditings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}