diff --git a/LawCompany/LawCompanyContracts/SearchModels/VisitSearchModel.cs b/LawCompany/LawCompanyContracts/SearchModels/VisitSearchModel.cs index 4efefe9..7e4909b 100644 --- a/LawCompany/LawCompanyContracts/SearchModels/VisitSearchModel.cs +++ b/LawCompany/LawCompanyContracts/SearchModels/VisitSearchModel.cs @@ -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; } } } diff --git a/LawCompany/LawCompanyDatabaseImplement/Implements/ClientStorage.cs b/LawCompany/LawCompanyDatabaseImplement/Implements/ClientStorage.cs new file mode 100644 index 0000000..779c6ad --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Implements/ClientStorage.cs @@ -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 GetFullList() + { + using var context = new LawCompanyDatabase(); + return context.Clients + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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 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 GetClientVisitList(VisitSearchModel model) + { + using var context = new LawCompanyDatabase(); + return context.VisitClients.Where(x => x.VisitId == model.Id).Select(x => x.Client.GetViewModel).ToList(); + } + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Implements/ExecutorStorage.cs b/LawCompany/LawCompanyDatabaseImplement/Implements/ExecutorStorage.cs new file mode 100644 index 0000000..130f2ed --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Implements/ExecutorStorage.cs @@ -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 GetFullList() + { + using var context = new LawCompanyDatabase(); + return context.Executors + .Select(x => x.GetViewModel) + .ToList(); + } + + public List 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; + } + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Implements/VisitStorage.cs b/LawCompany/LawCompanyDatabaseImplement/Implements/VisitStorage.cs new file mode 100644 index 0000000..8bb9715 --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Implements/VisitStorage.cs @@ -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 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 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 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; + } + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabase.cs b/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabase.cs new file mode 100644 index 0000000..5b9e973 --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabase.cs @@ -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 Cases { set; get; } + public virtual DbSet CaseClients { set; get; } + public virtual DbSet Clients { set; get; } + public virtual DbSet Hearings { set; get; } + public virtual DbSet HearingLawyers { set; get; } + public virtual DbSet Lawyers { set; get; } + public virtual DbSet Visits { set; get; } + public virtual DbSet VisitClients { set; get; } + public virtual DbSet Consultations { set; get; } + public virtual DbSet ConsultationLawyers { set; get; } + public virtual DbSet Guarantors { set; get; } + public virtual DbSet Executors { set; get; } + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabaseImplement.csproj b/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabaseImplement.csproj index e4c7e22..4d1ebec 100644 --- a/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabaseImplement.csproj +++ b/LawCompany/LawCompanyDatabaseImplement/LawCompanyDatabaseImplement.csproj @@ -1,14 +1,18 @@ - - net6.0 - enable - enable - + + net6.0 + enable + enable + - - - - + + + + + + + + diff --git a/LawCompany/LawCompanyDatabaseImplement/LawFirmDatabase.cs b/LawCompany/LawCompanyDatabaseImplement/LawFirmDatabase.cs deleted file mode 100644 index f0efe40..0000000 --- a/LawCompany/LawCompanyDatabaseImplement/LawFirmDatabase.cs +++ /dev/null @@ -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 - { - } -} diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/CaseClient.cs b/LawCompany/LawCompanyDatabaseImplement/Models/CaseClient.cs new file mode 100644 index 0000000..13843b5 --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Models/CaseClient.cs @@ -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(); + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Client.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Client.cs new file mode 100644 index 0000000..da01051 --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Client.cs @@ -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 CaseClients { get; set; } = new(); + [ForeignKey("ClientId")] + public virtual List 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, + }; + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Executor.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Executor.cs new file mode 100644 index 0000000..4e57f07 --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Executor.cs @@ -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, + }; + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/Visit.cs b/LawCompany/LawCompanyDatabaseImplement/Models/Visit.cs new file mode 100644 index 0000000..7ee523a --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Models/Visit.cs @@ -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? _visitClients = null; + [NotMapped] + public Dictionary 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 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; + } + } +} diff --git a/LawCompany/LawCompanyDatabaseImplement/Models/VisitClient.cs b/LawCompany/LawCompanyDatabaseImplement/Models/VisitClient.cs new file mode 100644 index 0000000..1ccff0d --- /dev/null +++ b/LawCompany/LawCompanyDatabaseImplement/Models/VisitClient.cs @@ -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(); + + } +}