CourseWork_BankYouBankrupt/BankYouBankrupt/BankYouBankruptDatabaseImplement/Implements/DebitingStorage.cs

131 lines
3.8 KiB
C#
Raw Normal View History

2023-04-01 19:28:58 +04:00
using BankYouBankruptContracts.BindingModels;
using BankYouBankruptContracts.SearchModels;
using BankYouBankruptContracts.StoragesContracts;
using BankYouBankruptContracts.ViewModels;
using BankYouBankruptDatabaseImplement.Models;
using BankYouBankruptDataModels.Enums;
2023-04-01 19:28:58 +04:00
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
2023-05-14 18:27:41 +04:00
.Include(x => x.Card)
2023-04-01 19:28:58 +04:00
.Select(x => x.GetViewModel)
.ToList();
}
public List<DebitingViewModel> GetFilteredList(DebitingSearchModel model)
{
using var context = new BankYouBancruptDatabase();
//для получения всех заявок на стнятие со статусом "Открыта"
2023-05-14 19:07:43 +04:00
if(model.Status == StatusEnum.Открыта)
{
return context.Debitings
2023-05-14 18:27:41 +04:00
.Include(x => x.Card)
.Where(x => x.Status == StatusEnum.Открыта)
.Select(x => x.GetViewModel)
.ToList();
}
2023-04-01 19:28:58 +04:00
return context.Debitings
2023-05-14 18:27:41 +04:00
.Include(x => x.Card)
2023-04-01 19:28:58 +04:00
.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
2023-05-14 18:27:41 +04:00
.Include(x => x.Card)
.FirstOrDefault(x => (!(model.CardId < 0) && x.CardId == model.CardId) ||
2023-04-01 19:28:58 +04:00
(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);
2023-05-14 20:22:27 +04:00
if (debiting == null && debiting.Status == StatusEnum.Закрыта)
2023-04-01 19:28:58 +04:00
{
return null;
}
debiting.Update(model);
context.SaveChanges();
transaction.Commit();
2023-05-14 20:22:27 +04:00
return context.Debitings
.Include(x => x.Card)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
2023-04-01 19:28:58 +04:00
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;
}
}
}