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 LawyerStorage : ILawyerStorage
|
|
|
|
|
{
|
|
|
|
|
public LawyerViewModel? Delete(LawyerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var element = context.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Lawyers.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LawyerViewModel? GetElement(LawyerSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
return context.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<LawyerViewModel> GetFilteredList(LawyerSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.Id == model.Id)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.UserId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Lawyers
|
|
|
|
|
.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.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(x => x.SpecializationId == model.SpecializationId)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<LawyerViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
return context.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-20 03:13:10 +04:00
|
|
|
|
public List<LawyerViewModel> GetLawyerMTM(int caseId)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var lawyersId = context.CaseLawyers
|
|
|
|
|
.Where(x => x.CaseId == caseId)
|
|
|
|
|
.Select(x => x.LawyerId).ToList();
|
|
|
|
|
return context.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.Where(X => lawyersId.Contains(X.Id))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-08 00:22:45 +04:00
|
|
|
|
public LawyerViewModel? Insert(LawyerBindingModel model)
|
|
|
|
|
{
|
2023-05-19 13:20:14 +04:00
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var newLawyer = Lawyer.Create(context, model);
|
2023-04-08 00:22:45 +04:00
|
|
|
|
if (newLawyer == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
context.Lawyers.Add(newLawyer);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return context.Lawyers
|
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newLawyer.Id)
|
|
|
|
|
?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LawyerViewModel? Update(LawyerBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
2023-04-08 22:40:14 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var lawyer = context.Lawyers
|
2023-04-08 00:22:45 +04:00
|
|
|
|
.Include(x => x.Specialization)
|
|
|
|
|
.Include(x => x.User)
|
|
|
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
|
2023-04-08 22:40:14 +04:00
|
|
|
|
if (lawyer == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-19 21:27:33 +04:00
|
|
|
|
lawyer.Update(context, model);
|
2023-04-08 22:40:14 +04:00
|
|
|
|
context.SaveChanges();
|
2023-05-19 21:27:33 +04:00
|
|
|
|
if (model.Cases.Count > 0)
|
|
|
|
|
{
|
|
|
|
|
lawyer.UpdateContracts(context, model);
|
|
|
|
|
}
|
2023-05-19 02:05:22 +04:00
|
|
|
|
transaction.Commit();
|
2023-04-08 22:40:14 +04:00
|
|
|
|
return lawyer.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<CaseViewModel> GetLawyerCases(LawyerSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
|
var cases = context.CaseLawyers
|
|
|
|
|
.Where(x => x.LawyerId == model.Id)
|
|
|
|
|
.Select(x => x.Case.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
return cases;
|
|
|
|
|
}
|
2023-04-08 00:22:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|