This commit is contained in:
Programmist73 2023-04-01 23:48:06 +04:00
commit d8ab257d98
4 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,119 @@
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
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
.Select(x => x.GetViewModel)
.ToList();
}
public List<CreditingViewModel> GetFilteredList(CreditingSearchModel model)
{
if (model.CardId < 0)
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.Creditings
.Where(x => x.CardId == model.CardId)
.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
.FirstOrDefault(x => (!(model.CardId < 0) && x.CardId == model.CardId) ||
(model.Id.HasValue && 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 crediting.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;
}
}
}

View File

@ -0,0 +1,119 @@
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
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 DebitingStorage : IDebitingStorage
{
public List<DebitingViewModel> GetFullList()
{
using var context = new BankYouBancruptDatabase();
return context.Debitings
.Select(x => x.GetViewModel)
.ToList();
}
public List<DebitingViewModel> GetFilteredList(DebitingSearchModel model)
{
if (model.CardId < 0)
{
return new();
}
using var context = new BankYouBancruptDatabase();
return context.Debitings
.Where(x => x.CardId == model.CardId)
.Select(x => x.GetViewModel)
.ToList();
}
public DebitingViewModel? GetElement(DebitingSearchModel model)
{
if (model.CardId < 0 && !model.Id.HasValue)
{
return null;
}
using var context = new BankYouBancruptDatabase();
return context.Debitings
.FirstOrDefault(x => (!(model.CardId < 0) && x.CardId == model.CardId) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public DebitingViewModel? Insert(DebitingBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var newDebiting = Debiting.Create(context, model);
if (newDebiting == null)
{
return null;
}
context.Debitings.Add(newDebiting);
context.SaveChanges();
return newDebiting.GetViewModel;
}
public DebitingViewModel? Update(DebitingBindingModel model)
{
using var context = new BankYouBancruptDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var debiting = context.Debitings.FirstOrDefault(rec => rec.Id == model.Id);
if (debiting == null)
{
return null;
}
debiting.Update(model);
context.SaveChanges();
transaction.Commit();
return debiting.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DebitingViewModel? Delete(DebitingBindingModel model)
{
using var context = new BankYouBancruptDatabase();
var element = context.Debitings
.Include(x => x.CardId)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Debitings.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -47,5 +47,10 @@ namespace BankYouBankruptDatabaseImplement.Models
Date = model.Date
};
}
public void Update(CreditingBindingModel model)
{
Date = model.Date;
}
}
}

View File

@ -45,5 +45,10 @@ namespace BankYouBankruptDatabaseImplement.Models
Date = model.Date
};
}
public void Update(DebitingBindingModel model)
{
Date = model.Date;
}
}
}