diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs new file mode 100644 index 0000000..e06c7f4 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/CreditingStorage.cs @@ -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 GetFullList() + { + using var context = new BankYouBancruptDatabase(); + + return context.Creditings + .Select(x => x.GetViewModel) + .ToList(); + } + + public List 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; + } + } +} diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/DebitingStorage.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/DebitingStorage.cs new file mode 100644 index 0000000..8c71c56 --- /dev/null +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/DebitingStorage.cs @@ -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 GetFullList() + { + using var context = new BankYouBancruptDatabase(); + + return context.Debitings + .Select(x => x.GetViewModel) + .ToList(); + } + + public List 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; + } + } +} diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs index 956fe24..89b4858 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Crediting.cs @@ -47,5 +47,10 @@ namespace BankYouBankruptDatabaseImplement.Models Date = model.Date }; } + + public void Update(CreditingBindingModel model) + { + Date = model.Date; + } } } diff --git a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Debiting.cs b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Debiting.cs index 42ccc62..4d45307 100644 --- a/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Debiting.cs +++ b/BankYouBankrupt/BankYouBankruptDatabaseImplement/Models/Debiting.cs @@ -45,5 +45,10 @@ namespace BankYouBankruptDatabaseImplement.Models Date = model.Date }; } + + public void Update(DebitingBindingModel model) + { + Date = model.Date; + } } }