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 DealStorage : IDealStorage
|
|
|
|
|
{
|
|
|
|
|
public DealViewModel? Delete(DealBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var element = context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Deals.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DealViewModel? GetElement(DealSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
return context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DealViewModel> GetFilteredList(DealSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.Date != null)
|
|
|
|
|
{
|
|
|
|
|
return context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Date == model.Date)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
if (model.UserId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.UserId == model.UserId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DealViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
return context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DealViewModel? Insert(DealBindingModel model)
|
|
|
|
|
{
|
2023-05-19 02:05:22 +04:00
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var newDeal = Deal.Create(context, model);
|
2023-04-08 15:14:22 +04:00
|
|
|
|
if (newDeal == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Deals.Add(newDeal);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return context.Deals
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newDeal.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DealViewModel? Update(DealBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
2023-04-08 22:40:14 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var deal = context.Deals
|
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 (deal == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
deal.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
deal.UpdateCases(context, model);
|
2023-05-19 02:05:22 +04:00
|
|
|
|
transaction.Commit();
|
2023-04-08 22:40:14 +04:00
|
|
|
|
return deal.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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|