пару классов в датабаз
This commit is contained in:
parent
2b16a3c314
commit
9b2ab07b65
110
LawCompany/LawCompanyDatabaseImplement/Implements/CaseStorage.cs
Normal file
110
LawCompany/LawCompanyDatabaseImplement/Implements/CaseStorage.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.SearchModels;
|
||||
using LawCompanyContracts.StoragesContracts;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using LawCompanyDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class CaseStorage : ICaseStorage
|
||||
{
|
||||
public List<CaseViewModel> GetFullList()
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Cases.Include(x => x.CaseClients)
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<CaseViewModel> GetFilteredList(CaseSearchModel model)
|
||||
{
|
||||
if (!model.DateFrom.HasValue && !model.DateTo.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Cases.Include(x => x.CaseClients)
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel).ToList()
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Cases
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.Where(x => x.DateCreate >= model.DateFrom && x.DateCreate <= model.DateTo)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
public CaseViewModel? GetElement(CaseSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Cases.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
|
||||
?.GetViewModel; //имя дела не должно повторяться, уникальное должно быть
|
||||
|
||||
}
|
||||
public CaseViewModel? Insert(CaseBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var newCase = Case.Create(context, model);
|
||||
if (newCase == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Cases.Add(newCase);
|
||||
context.SaveChanges();
|
||||
return newCase.GetViewModel;
|
||||
}
|
||||
public CaseViewModel? Update(CaseBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var _case = context.Cases.FirstOrDefault(rec =>
|
||||
rec.Id == model.Id);
|
||||
if (_case == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
_case.Update(model);
|
||||
context.SaveChanges();
|
||||
_case.UpdateClients(context, model);
|
||||
|
||||
transaction.Commit();
|
||||
return _case.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public CaseViewModel? Delete(CaseBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var element = context.Cases
|
||||
.Include(x => x.Clients)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Cases.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,13 +6,6 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Implements\" />
|
||||
<Folder Include="Models\" />
|
||||
<Folder Include="Implements\" />
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LawFimDataModels\LawFimDataModels.csproj" />
|
||||
<ProjectReference Include="..\LawFirmContracts\LawFirmContracts.csproj" />
|
||||
|
115
LawCompany/LawCompanyDatabaseImplement/Models/Case.cs
Normal file
115
LawCompany/LawCompanyDatabaseImplement/Models/Case.cs
Normal file
@ -0,0 +1,115 @@
|
||||
using LawCompanyDataModels.Enums;
|
||||
using LawCompanyDataModels.Models;
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class Case : ICaseModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string Name { get; private set; } = String.Empty;
|
||||
[Required]
|
||||
public string CaseType { get; private set; } = String.Empty;
|
||||
[Required]
|
||||
public DateTime DateCreate { get; private set; }
|
||||
public DateTime? DateImplement { get; private set; }
|
||||
[Required]
|
||||
public CaseStatus Status { get; private set; }
|
||||
private Dictionary<int, IClientModel>? _caseClients = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, IClientModel> CaseClients
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_caseClients == null)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
_caseClients = Clients
|
||||
.ToDictionary(x => x.ClientId, x => (context.Clients.FirstOrDefault(y => y.Id == x.ClientId) as IClientModel));
|
||||
}
|
||||
|
||||
return _caseClients;
|
||||
}
|
||||
}
|
||||
[ForeignKey("CaseId")]
|
||||
public virtual List<CaseClient> Clients { get; set; } = new();
|
||||
[ForeignKey("CaseId")]
|
||||
public virtual List<Hearing> Hearings { get; set; } = new();
|
||||
public static Case Create(LawCompanyDatabase context, CaseBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Case()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
DateCreate = model.DateCreate,
|
||||
DateImplement = model.DateImplement,
|
||||
Status = model.Status,
|
||||
Clients = model.CaseClients.Select(x => new CaseClient
|
||||
{
|
||||
Client = context.Clients.First(y => y.Id == x.Key)
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(CaseBindingModel? model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (model.Status == CaseStatus.ЗакрытиеДела) DateImplement = DateTime.Now;
|
||||
Name = model.Name;
|
||||
Status = model.Status;
|
||||
|
||||
|
||||
if (model.DateImplement.HasValue) DateImplement = model.DateImplement;
|
||||
if (!model.CompanyId.HasValue) CompanyId = model.CompanyId;
|
||||
}
|
||||
public CaseViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
CaseType = CaseType,
|
||||
DateCreate = DateCreate,
|
||||
DateImplement = DateImplement,
|
||||
Status = Status,
|
||||
CaseClients = CaseClients,
|
||||
};
|
||||
|
||||
|
||||
public void UpdateClients(LawCompanyDatabase context, CaseBindingModel model)
|
||||
{
|
||||
var caseClients = context.CaseClients.Where(rec =>
|
||||
rec.CaseId == model.Id).ToList();
|
||||
if (caseClients != null && caseClients.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.CaseClients.RemoveRange(caseClients.Where(rec
|
||||
=> !model.CaseClients.ContainsKey(rec.ClientId)));
|
||||
context.SaveChanges();
|
||||
}
|
||||
var _case = context.Cases.First(x => x.Id == Id);
|
||||
foreach (var pc in model.CaseClients)
|
||||
{
|
||||
if (!CaseClients.ContainsKey(pc.Key))
|
||||
{
|
||||
context.CaseClients.Add(new CaseClient
|
||||
{
|
||||
Case = _case,
|
||||
Client = context.Clients.First(x => x.Id == pc.Key),
|
||||
});
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
_caseClients = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user