Case_accounting/CaseAccounting/CaseAccountingDataBaseImplement/Implements/ContractStorage.cs

132 lines
4.1 KiB
C#
Raw Normal View History

2023-04-08 15:14:22 +04:00
using CaseAccountingContracts.BindingModels;
using CaseAccountingContracts.SearchModels;
using CaseAccountingContracts.StoragesContracts;
using CaseAccountingContracts.ViewModels;
using CaseAccountingDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CaseAccountingDataBaseImplement.Implements
{
public class ContractStorage : IContractStorage
{
public ContractViewModel? Delete(ContractBindingModel model)
{
using var context = new CaseAccountingDatabase();
var element = context.Contracts
.Include(x => x.User)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element == null)
{
return null;
}
context.Contracts.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
public ContractViewModel? GetElement(ContractSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new CaseAccountingDatabase();
return context.Contracts
.Include(x => x.User)
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public List<ContractViewModel> GetFilteredList(ContractSearchModel model)
{
using var context = new CaseAccountingDatabase();
if (model.Id.HasValue)
{
return context.Contracts
.Include(x => x.User)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.Date != null)
{
return context.Contracts
.Include(x => x.User)
.Where(x => x.Date == model.Date)
.Select(x => x.GetViewModel)
.ToList();
}
if (model.UserId.HasValue)
{
return context.Contracts
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<ContractViewModel> GetFullList()
{
using var context = new CaseAccountingDatabase();
return context.Contracts
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public ContractViewModel? Insert(ContractBindingModel model)
{
var newContract = Contract.Create(model);
if (newContract == null)
{
return null;
}
using var context = new CaseAccountingDatabase();
context.Contracts.Add(newContract);
context.SaveChanges();
return context.Contracts
.Include(x => x.User)
.FirstOrDefault(x => x.Id == newContract.Id)
?.GetViewModel;
}
public ContractViewModel? Update(ContractBindingModel model)
{
using var context = new CaseAccountingDatabase();
2023-04-08 22:40:14 +04:00
using var transaction = context.Database.BeginTransaction();
try
{
var contract = context.Contracts
2023-04-08 15:14:22 +04:00
.Include(x => x.User)
.FirstOrDefault(x => x.Id == model.Id);
2023-04-08 22:40:14 +04:00
if (contract == null)
{
return null;
}
contract.Update(model);
context.SaveChanges();
contract.UpdateDeals(context, model);
return contract.GetViewModel;
}
catch
2023-04-08 15:14:22 +04:00
{
2023-04-08 22:40:14 +04:00
transaction.Rollback();
throw;
2023-04-08 15:14:22 +04:00
}
}
}
}