175 lines
5.7 KiB
C#
175 lines
5.7 KiB
C#
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)
|
|
{
|
|
var newCase = Case.Create(model);
|
|
if (newCase == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new CaseAccountingDatabase();
|
|
|
|
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();
|
|
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);
|
|
|
|
if (_case == null)
|
|
{
|
|
return null;
|
|
}
|
|
_case.Update(model);
|
|
context.SaveChanges();
|
|
_case.UpdateLawyers(context, model);
|
|
return _case.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|