базы данных
This commit is contained in:
parent
9b2ab07b65
commit
e757e92dfe
@ -10,6 +10,8 @@ namespace LawCompanyContracts.SearchModels
|
||||
{
|
||||
public int? Id { get; set; }
|
||||
public DateTime? VisitDate { get; set; }
|
||||
public DateTime? DateFrom { get; set; }
|
||||
public DateTime? DateTo { get; set; }
|
||||
public int? HearingId { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,106 @@
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.SearchModels;
|
||||
using LawCompanyContracts.StoragesContracts;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using LawCompanyDatabaseImplement.Models;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class ClientStorage : IClientStorage
|
||||
{
|
||||
public List<ClientViewModel> GetFullList()
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Clients
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.FIO)
|
||||
&& string.IsNullOrEmpty(model.Phone))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
if (!string.IsNullOrEmpty(model.Email))
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Clients
|
||||
.Where(x => x.Email.Equals(model.Email))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Clients
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
public ClientViewModel? GetElement(ClientSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.FIO)
|
||||
&& string.IsNullOrEmpty(model.Phone))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LawCompanyDatabase();
|
||||
|
||||
return context.Clients
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email)
|
||||
&& x.Email == model.Email) || (model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public ClientViewModel? Insert(ClientBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var newClient = Client.Create(model);
|
||||
if (newClient == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Clients.Add(newClient);
|
||||
context.SaveChanges();
|
||||
return newClient.GetViewModel;
|
||||
}
|
||||
public ClientViewModel? Update(ClientBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (client == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
client.Update(model);
|
||||
context.SaveChanges();
|
||||
return client.GetViewModel;
|
||||
}
|
||||
public ClientViewModel? Delete(ClientBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Clients.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<ClientViewModel> GetClientCaseList(CaseSearchModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.CaseClients.Where(x => x.CaseId == model.Id).Select(x => x.Client.GetViewModel).ToList();
|
||||
|
||||
}
|
||||
|
||||
public List<ClientViewModel> GetClientVisitList(VisitSearchModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.VisitClients.Where(x => x.VisitId == model.Id).Select(x => x.Client.GetViewModel).ToList();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.SearchModels;
|
||||
using LawCompanyContracts.StoragesContracts;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using LawCompanyDatabaseImplement.Models;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class ExecutorStorage : IExecutorStorage
|
||||
{
|
||||
public List<ExecutorViewModel> GetFullList()
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Executors
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ExecutorViewModel> GetFilteredList(ExecutorSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.FIO)
|
||||
&& string.IsNullOrEmpty(model.Password))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
if (!string.IsNullOrEmpty(model.Email))
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Executors
|
||||
.Where(x => x.Email.Equals(model.Email))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Executors
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public ExecutorViewModel? GetElement(ExecutorSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue && string.IsNullOrEmpty(model.FIO)
|
||||
&& string.IsNullOrEmpty(model.Password))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new LawCompanyDatabase();
|
||||
|
||||
return context.Executors
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Email)
|
||||
&& x.Email == model.Email) || (model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public ExecutorViewModel? Insert(ExecutorBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var newExecutor = Executor.Create(model);
|
||||
if (newExecutor == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Executors.Add(newExecutor);
|
||||
context.SaveChanges();
|
||||
return newExecutor.GetViewModel;
|
||||
}
|
||||
|
||||
public ExecutorViewModel? Update(ExecutorBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var executor = context.Executors.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (executor == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
executor.Update(model);
|
||||
context.SaveChanges();
|
||||
return executor.GetViewModel;
|
||||
}
|
||||
|
||||
public ExecutorViewModel? Delete(ExecutorBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var element = context.Executors.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Executors.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,137 @@
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.SearchModels;
|
||||
using LawCompanyContracts.StoragesContracts;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using LawCompanyDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class VisitStorage : IVisitStorage
|
||||
{
|
||||
public List<VisitViewModel> GetFullList()
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Visits
|
||||
.Include(x => x.Clients)
|
||||
.ThenInclude(x => x.Client)
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<VisitViewModel> GetFilteredList(VisitSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue
|
||||
&& !model.VisitDate.HasValue && !model.HearingId.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Visits
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Visits
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.Where(x => x.VisitDate >= model.DateFrom && x.VisitDate <= model.DateTo)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
public List<VisitViewModel> GetFilteredDateList(VisitSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue && !model.DateFrom.HasValue && !model.DateTo.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
if (!model.DateFrom.HasValue || !model.DateTo.HasValue)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Visits
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.Where(x => x.Id == model.Id)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
else
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Visits
|
||||
.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.Where(x => x.VisitDate >= model.DateFrom && x.VisitDate <= model.DateTo)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
public VisitViewModel? GetElement(VisitSearchModel model)
|
||||
{
|
||||
if (!model.Id.HasValue && !model.VisitDate.HasValue && !model.HearingId.HasValue)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new LawCompanyDatabase();
|
||||
return context.Visits.Include(x => x.Clients).ThenInclude(x => x.Client)
|
||||
.FirstOrDefault(x => (model.HearingId.HasValue && x.Hearing == model.HearingId)
|
||||
|| (model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public VisitViewModel? Insert(VisitBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var newVisit = Visit.Create(context, model);
|
||||
if (newVisit == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Visits.Add(newVisit);
|
||||
context.SaveChanges();
|
||||
return newVisit.GetViewModel;
|
||||
}
|
||||
public VisitViewModel? Update(VisitBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var visit = context.Visits.FirstOrDefault(rec =>
|
||||
rec.Id == model.Id);
|
||||
if (visit == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
visit.Update(model);
|
||||
context.SaveChanges();
|
||||
visit.UpdateClients(context, model);
|
||||
transaction.Commit();
|
||||
return visit.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
public VisitViewModel? Delete(VisitBindingModel model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
var element = context.Visits
|
||||
.Include(x => x.Clients)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Visits.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
32
LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabase.cs
Normal file
32
LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabase.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LawCompanyDatabaseImplement
|
||||
{
|
||||
public class LawCompanyDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder
|
||||
optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
// optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-6GNIALH9\SQLEXPRESS;Initial Catalog=LawCompanyDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Case> Cases { set; get; }
|
||||
public virtual DbSet<CaseClient> CaseClients { set; get; }
|
||||
public virtual DbSet<Client> Clients { set; get; }
|
||||
public virtual DbSet<Hearing> Hearings { set; get; }
|
||||
public virtual DbSet<HearingLawyer> HearingLawyers { set; get; }
|
||||
public virtual DbSet<Lawyer> Lawyers { set; get; }
|
||||
public virtual DbSet<Visit> Visits { set; get; }
|
||||
public virtual DbSet<VisitClient> VisitClients { set; get; }
|
||||
public virtual DbSet<Consultation> Consultations { set; get; }
|
||||
public virtual DbSet<ConsultationLawyer> ConsultationLawyers { set; get; }
|
||||
public virtual DbSet<Guarantor> Guarantors { set; get; }
|
||||
public virtual DbSet<Executor> Executors { set; get; }
|
||||
}
|
||||
}
|
@ -7,8 +7,12 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LawFimDataModels\LawFimDataModels.csproj" />
|
||||
<ProjectReference Include="..\LawFirmContracts\LawFirmContracts.csproj" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.18" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\LawCompanyDataModels\LawCompanyDataModels.csproj" />
|
||||
<ProjectReference Include="..\LawCompanyContracts\LawCompanyContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,12 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace LawCompanyDatabaseImplement
|
||||
{
|
||||
internal class LawFirmDatabase
|
||||
{
|
||||
}
|
||||
}
|
15
LawCompany/LawCompanyDatabaseImplement/Models/CaseClient.cs
Normal file
15
LawCompany/LawCompanyDatabaseImplement/Models/CaseClient.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class CaseClient
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int CaseId { get; set; }
|
||||
[Required]
|
||||
public int ClientId { get; set; }
|
||||
public virtual Case Case { get; set; } = new();
|
||||
public virtual Client Client { get; set; } = new();
|
||||
}
|
||||
}
|
65
LawCompany/LawCompanyDatabaseImplement/Models/Client.cs
Normal file
65
LawCompany/LawCompanyDatabaseImplement/Models/Client.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using LawCompanyDataModels.Models;
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class Client : IClientModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string FIO { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Email { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Phone { get; private set; } = string.Empty;
|
||||
[ForeignKey("ClientId")]
|
||||
public virtual List<CaseClient> CaseClients { get; set; } = new();
|
||||
[ForeignKey("ClientId")]
|
||||
public virtual List<VisitClient> ClientVisits { get; set; } = new();
|
||||
|
||||
public static Client? Create(ClientBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Client()
|
||||
{
|
||||
Id = model.Id,
|
||||
FIO = model.FIO,
|
||||
Email = model.Email,
|
||||
Phone = model.Phone,
|
||||
};
|
||||
}
|
||||
public static Client Create(ClientViewModel model)
|
||||
{
|
||||
return new Client
|
||||
{
|
||||
Id = model.Id,
|
||||
FIO = model.FIO,
|
||||
Email = model.Email,
|
||||
Phone = model.Phone,
|
||||
};
|
||||
}
|
||||
public void Update(ClientBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FIO = model.FIO;
|
||||
Email = model.Email;
|
||||
Phone = model.Phone;
|
||||
}
|
||||
public ClientViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
FIO = FIO,
|
||||
Email = Email,
|
||||
Phone = Phone,
|
||||
};
|
||||
}
|
||||
}
|
63
LawCompany/LawCompanyDatabaseImplement/Models/Executor.cs
Normal file
63
LawCompany/LawCompanyDatabaseImplement/Models/Executor.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using LawCompanyDataModels.Models;
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class Executor : IExecutorModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public string FIO { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Email { get; private set; } = string.Empty;
|
||||
[Required]
|
||||
public string Password { get; private set; } = string.Empty;
|
||||
|
||||
public static Executor? Create(ExecutorBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Executor()
|
||||
{
|
||||
Id = model.Id,
|
||||
FIO = model.FIO,
|
||||
Email = model.Email,
|
||||
Password = model.Password,
|
||||
};
|
||||
}
|
||||
|
||||
public static Executor Create(ExecutorViewModel model)
|
||||
{
|
||||
return new Executor
|
||||
{
|
||||
Id = model.Id,
|
||||
FIO = model.FIO,
|
||||
Email = model.Email,
|
||||
Password = model.Password,
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(ExecutorBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
FIO = model.FIO;
|
||||
Email = model.Email;
|
||||
Password = model.Password;
|
||||
}
|
||||
|
||||
public ExecutorViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
FIO = FIO,
|
||||
Email = Email,
|
||||
Password = Password,
|
||||
};
|
||||
}
|
||||
}
|
110
LawCompany/LawCompanyDatabaseImplement/Models/Visit.cs
Normal file
110
LawCompany/LawCompanyDatabaseImplement/Models/Visit.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using LawCompanyDataModels.Models;
|
||||
using LawCompanyContracts.BindingModels;
|
||||
using LawCompanyContracts.ViewModels;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class Visit : IVisitModel
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
[Required]
|
||||
public DateTime VisitDate { get; private set; }
|
||||
|
||||
//связь один-к-одному
|
||||
public int HearingId { get; private set; }
|
||||
public Hearing Hearing { get; set; } = null!;
|
||||
//---
|
||||
public int? CompanyId { get; set; }
|
||||
|
||||
private Dictionary<int, IClientModel>? _visitClients = null;
|
||||
[NotMapped]
|
||||
public Dictionary<int, IClientModel> VisitClients
|
||||
{
|
||||
get
|
||||
{
|
||||
|
||||
if (_visitClients == null)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
_visitClients = Clients
|
||||
.ToDictionary(x => x.ClientId, x => (context.Clients
|
||||
.FirstOrDefault(y => y.Id == x.ClientId) as IClientModel));
|
||||
}
|
||||
return _visitClients;
|
||||
}
|
||||
}
|
||||
[ForeignKey("VisitId")]
|
||||
public virtual List<VisitClient> Clients { get; set; } = new();
|
||||
public static Visit? Create(LawCompanyDatabase context,
|
||||
VisitBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
var visits = context.Visits.Where(x => x.HearingId == model.HearingId).ToList();
|
||||
if (visits.Count > 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Visit()
|
||||
{
|
||||
Id = model.Id,
|
||||
VisitDate = model.VisitDate,
|
||||
HearingId = model.HearingId,
|
||||
Hearing = context.Hearings.First(x => x.Id == model.HearingId),
|
||||
Clients = model.VisitClients.Select(x => new VisitClient
|
||||
{
|
||||
Client = context.Clients.First(y => y.Id == x.Key)
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
public void Update(VisitBindingModel? model)
|
||||
{
|
||||
using var context = new LawCompanyDatabase();
|
||||
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
VisitDate = model.VisitDate;
|
||||
HearingId = model.HearingId;
|
||||
}
|
||||
public VisitViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
VisitDate = VisitDate,
|
||||
HearingId = HearingId,
|
||||
VisitClients = VisitClients
|
||||
};
|
||||
public void UpdateClients(LawCompanyDatabase context,
|
||||
VisitBindingModel model)
|
||||
{
|
||||
var visitClients = context.VisitClients.Where(rec =>
|
||||
rec.VisitId == model.Id).ToList();
|
||||
if (visitClients != null && visitClients.Count > 0)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.VisitClients.RemoveRange(visitClients.Where(rec
|
||||
=> !model.VisitClients.ContainsKey(rec.ClientId)));
|
||||
context.SaveChanges();
|
||||
}
|
||||
var _visit = context.Visits.First(x => x.Id == Id);
|
||||
foreach (var pc in model.VisitClients)
|
||||
{
|
||||
if (!VisitClients.ContainsKey(pc.Key))
|
||||
{
|
||||
context.VisitClients.Add(new VisitClient
|
||||
{
|
||||
Visit = _visit,
|
||||
Client = context.Clients.First(x => x.Id == pc.Key),
|
||||
});
|
||||
}
|
||||
context.SaveChanges();
|
||||
}
|
||||
_visitClients = null;
|
||||
}
|
||||
}
|
||||
}
|
16
LawCompany/LawCompanyDatabaseImplement/Models/VisitClient.cs
Normal file
16
LawCompany/LawCompanyDatabaseImplement/Models/VisitClient.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace LawCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class VisitClient
|
||||
{
|
||||
public int Id { get; set; }
|
||||
[Required]
|
||||
public int ClientId { get; set; }
|
||||
[Required]
|
||||
public int VisitId { get; set; }
|
||||
public virtual Client Client { get; set; } = new();
|
||||
public virtual Visit Visit { get; set; } = new();
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user