2023-04-08 00:22:45 +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 CaseStorage : ICaseStorage
|
|
|
|
|
{
|
|
|
|
|
public CaseViewModel? Delete(CaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var element = context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Cases.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CaseViewModel? GetElement(CaseSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CaseViewModel> GetFilteredList(CaseSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.Name != null)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Name == model.Name)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.Applicant != null)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Applicant == model.Applicant)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.Defendant != null)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Defendant == model.Defendant)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.Date != null)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Date == model.Date)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.UserId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.UserId == model.UserId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.SpecializationId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.SpecializationId == model.SpecializationId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CaseViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CaseViewModel? Insert(CaseBindingModel model)
|
|
|
|
|
{
|
2023-05-19 02:05:22 +04:00
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var newCase = Case.Create(context, model);
|
2023-04-08 00:22:45 +04:00
|
|
|
|
if (newCase == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Cases.Add(newCase);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newCase.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CaseViewModel? Update(CaseBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
2023-04-08 22:40:14 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var _case = context.Cases
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
2023-04-08 00:22:45 +04:00
|
|
|
|
|
2023-04-08 22:40:14 +04:00
|
|
|
|
if (_case == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-20 03:13:10 +04:00
|
|
|
|
_case.Update(context, model);
|
2023-04-08 22:40:14 +04:00
|
|
|
|
context.SaveChanges();
|
|
|
|
|
_case.UpdateLawyers(context, model);
|
2023-05-19 02:05:22 +04:00
|
|
|
|
transaction.Commit();
|
2023-04-08 22:40:14 +04:00
|
|
|
|
return _case.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch
|
2023-04-08 00:22:45 +04:00
|
|
|
|
{
|
2023-04-08 22:40:14 +04:00
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
2023-04-08 00:22:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-05-20 01:21:33 +04:00
|
|
|
|
|
|
|
|
|
public List<HearingViewModel> GetCaseHearings(CaseSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var hearings = context.Hearings
|
|
|
|
|
.Where(x => x.CaseId == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
return hearings;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 00:22:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|