using BankYouBankruptContracts.BindingModels; using BankYouBankruptContracts.SearchModels; using BankYouBankruptContracts.StoragesContracts; using BankYouBankruptContracts.ViewModels; using BankYouBankruptDatabaseImplement.Models; using BankYouBankruptDataModels.Enums; 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 .Include(x => x.Card) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(DebitingSearchModel model) { using var context = new BankYouBancruptDatabase(); //для получения всех заявок на стнятие со статусом "Открыта" if(model.Status == StatusEnum.Открыта) { return context.Debitings .Include(x => x.Card) .Where(x => x.Status == StatusEnum.Открыта) .Select(x => x.GetViewModel) .ToList(); } return context.Debitings .Include(x => x.Card) .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 .Include(x => x.Card) .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 && debiting.Status == StatusEnum.Закрыта) { return null; } debiting.Update(model); context.SaveChanges(); transaction.Commit(); return context.Debitings .Include(x => x.Card) .FirstOrDefault(x => x.Id == model.Id) ?.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; } } }