From 196dbf8f6c0e82375249baa03ca8c6e6f08560cc Mon Sep 17 00:00:00 2001 From: goblinrf Date: Wed, 24 Apr 2024 14:00:53 +0400 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B5=D0=BA=D0=BE=D0=B5=20=D0=BD=D0=B5?= =?UTF-8?q?=D0=B7=D1=80=D0=B8=D0=BC=D0=BE=D0=B5=20=D1=81=D1=83=D1=89=D0=B5?= =?UTF-8?q?=D1=81=D1=82=D0=B2=D0=BE=20=D0=B8=D0=BB=D0=B8=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BA=D0=BE=D0=BD,=20=D0=BF=D0=BE=D0=B4=D0=BE=D0=B1=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=B4=D0=BB=D0=B0=D0=BD=D0=B8=20=D0=B3=D0=BE=D1=81?= =?UTF-8?q?=D0=BF=D0=BE=D0=B4=D0=BD=D0=B5=D0=B9,=20=D0=BF=D0=B0=D1=80?= =?UTF-8?q?=D1=8F=D1=89=D0=B5=D0=B9=20=D0=BD=D0=B0=D0=B4=20=D0=BC=D0=B8?= =?UTF-8?q?=D1=80=D0=BE=D0=BC=3F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Implements/AdminStorage.cs | 123 ----------------- .../Implements/AnimalStorage.cs | 129 ------------------ .../DinerDataBaseImplement/Models/Admin.cs | 76 ----------- .../DinerDataBaseImplement/Models/Visit.cs | 12 -- .../VetClinicDatabase.cs | 24 ---- VetClinic/VetClinic.sln | 12 +- .../BindingModels/AnimalBindingModel.cs | 2 +- .../SearchModels/AnimalSearchModel.cs | 2 +- .../ViewModels/AnimalViewModel.cs | 2 +- .../Implements/AdminStorage.cs | 123 +++++++++++++++++ .../Implements/AnimalStorage.cs | 110 +++++++++++++++ .../Models/Admin.cs | 76 +++++++++++ .../Models/Animal.cs | 59 +++++--- .../Models/Visit.cs | 20 +++ .../Models/VisitAnimal.cs | 2 +- .../VetClinicDataBaseImplement.csproj | 1 + .../VetClinicDatabase .cs | 24 ++++ .../Models/IAnimalModel.cs | 2 +- .../VetClinicDataModels/Models/IVisitModel.cs | 2 +- 19 files changed, 406 insertions(+), 395 deletions(-) delete mode 100644 VetClinic/DinerDataBaseImplement/Implements/AdminStorage.cs delete mode 100644 VetClinic/DinerDataBaseImplement/Implements/AnimalStorage.cs delete mode 100644 VetClinic/DinerDataBaseImplement/Models/Admin.cs delete mode 100644 VetClinic/DinerDataBaseImplement/Models/Visit.cs delete mode 100644 VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/AdminStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/Admin.cs rename VetClinic/{DinerDataBaseImplement => VetClinicDataBaseImplement}/Models/Animal.cs (57%) create mode 100644 VetClinic/VetClinicDataBaseImplement/Models/Visit.cs rename VetClinic/{DinerDataBaseImplement => VetClinicDataBaseImplement}/Models/VisitAnimal.cs (93%) rename VetClinic/{DinerDataBaseImplement => VetClinicDataBaseImplement}/VetClinicDataBaseImplement.csproj (90%) create mode 100644 VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs diff --git a/VetClinic/DinerDataBaseImplement/Implements/AdminStorage.cs b/VetClinic/DinerDataBaseImplement/Implements/AdminStorage.cs deleted file mode 100644 index 8c8bf16..0000000 --- a/VetClinic/DinerDataBaseImplement/Implements/AdminStorage.cs +++ /dev/null @@ -1,123 +0,0 @@ -using VetClinicContracts.BindingModels; -using VetClinicContracts.SearchModels; -using VetClinicContracts.StoragesContracts; -using VetClinicContracts.ViewModels; -using VetClinicDataBaseImplement.Models; -using Microsoft.EntityFrameworkCore; -using VetClinicDataBaseImplement; - -namespace VetClinicDataBaseImplement.Implements -{ - public class AdminStorage : IAdminStorage - { - public AdminViewModel? Delete(AdminBindingModel model) - { - using var context = new VetClinicDatabase(); - var element = context.Admins - .FirstOrDefault(rec => rec.Id == model.Id); - - if (element != null) - { - var deletedElement = context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - - context.Admins.Remove(element); - context.SaveChanges(); - - return deletedElement; - } - return null; - } - - public AdminViewModel? GetElement(AdminSearchModel model) - { - using var context = new VetClinicDatabase(); - if (model.Id.HasValue) - { - return context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) - ?.GetViewModel; - } - else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) - { - return context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password)) - ?.GetViewModel; - } - return new(); - } - - public List GetFilteredList(AdminSearchModel model) - { - if (string.IsNullOrEmpty(model.AdminFIO)) - { - return new(); - } - using var context = new VetClinicDatabase(); - - return context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .Where(x => x.AdminFIO.Contains(model.AdminFIO)) - .Select(x => x.GetViewModel) - .ToList(); - } - - public List GetFullList() - { - using var context = new VetClinicDatabase(); - - return context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .Select(x => x.GetViewModel) - .ToList(); - } - - public AdminViewModel? Insert(AdminBindingModel model) - { - var newAdmin = Admin.Create(model); - if (newAdmin == null) - { - return null; - } - using var context = new VetClinicDatabase(); - context.Admins.Add(newAdmin); - context.SaveChanges(); - - return context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .FirstOrDefault(x => x.Id == newAdmin.Id) - ?.GetViewModel; - } - - public AdminViewModel? Update(AdminBindingModel model) - { - using var context = new VetClinicDatabase(); - var order = context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .FirstOrDefault(x => x.Id == model.Id); - if (order == null) - { - return null; - } - order.Update(model); - context.SaveChanges(); - - return context.Admins - //.Include(x => x.Visits) - .Include(x => x.Animals) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - } - } -} diff --git a/VetClinic/DinerDataBaseImplement/Implements/AnimalStorage.cs b/VetClinic/DinerDataBaseImplement/Implements/AnimalStorage.cs deleted file mode 100644 index 4b52246..0000000 --- a/VetClinic/DinerDataBaseImplement/Implements/AnimalStorage.cs +++ /dev/null @@ -1,129 +0,0 @@ -using VetClinicContracts.BindingModels; -using VetClinicContracts.SearchModels; -using VetClinicContracts.StoragesContracts; -using VetClinicContracts.ViewModels; -using VetClinicDataBaseImplement.Models; -using Microsoft.EntityFrameworkCore; -using VetClinicDataBaseImplement; - -namespace DinerDataBaseImplement.Implements -{ - public class AnimalStorage - { - public List GetFullList() - { - using var context = new VetClinicDatabase(); - - return context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .Select(x => x.GetViewModel) - .ToList(); - } - public List GetFilteredList(AnimalSearchModel model) - { - using var context = new VetClinicDatabase(); - - - if (model.Id.HasValue) - { - return context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); - } - else if (model.AdminId.HasValue) - { - return context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .Where(x => x.AdminId == model.AdminId) - .Select(x => x.GetViewModel) - .ToList(); - } - else if (model.VisitId.HasValue) - { - return context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .Where(x => x.AdminId == model.AdminId) - .Select(x => x.GetViewModel) - .ToList(); - } - - return new(); - } - public AnimalViewModel? GetElement(AnimalSearchModel model) - { - if (!model.Id.HasValue) - { - return null; - } - using var context = new VetClinicDatabase(); - return context.Animals//.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)?.GetViewModel; - } - public AnimalViewModel? Insert(AnimalBindingModel model) - { - using var context = new VetClinicDatabase(); - var newAnimal = Animal.Create(model); - if (newAnimal == null) - { - return null; - } - context.Animals.Add(newAnimal); - context.SaveChanges(); - return context.Animals//.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .FirstOrDefault(x => x.Id == newAnimal.Id)?.GetViewModel; ; - } - public AnimalViewModel? Update(AnimalBindingModel model) - { - using var context = new VetClinicDatabase(); - var animal = context.Animals.FirstOrDefault(rec => rec.Id == model.Id); - if (animal == null) - { - return null; - } - animal.Update(model); - context.SaveChanges(); - - return context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .FirstOrDefault(x => x.Id == model.Id)? - .GetViewModel; - } - public AnimalViewModel? Delete(AnimalBindingModel model) - { - using var context = new VetClinicDatabase(); - var element = context.Animals - .FirstOrDefault(x => x.Id == model.Id); - - if (element != null) - { - var deletedElement = context.Animals - //.Include(x => x.Visit) - .Include(x => x.Admin) - //.Include(x => x.Vaccination) - .FirstOrDefault(x => x.Id == model.Id) - ?.GetViewModel; - - context.Animals.Remove(element); - context.SaveChanges(); - return deletedElement; - } - return null; - } - } -} diff --git a/VetClinic/DinerDataBaseImplement/Models/Admin.cs b/VetClinic/DinerDataBaseImplement/Models/Admin.cs deleted file mode 100644 index 82b8621..0000000 --- a/VetClinic/DinerDataBaseImplement/Models/Admin.cs +++ /dev/null @@ -1,76 +0,0 @@ -using VetClinicDataModels.Models; -using System; -using System.Collections.Generic; -using System.ComponentModel.DataAnnotations.Schema; -using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using VetClinicContracts.BindingModels; -using VetClinicContracts.ViewModels; - -namespace VetClinicDataBaseImplement.Models -{ - public class Admin : IAdminModel - { - public int Id { get; set; } - - [Required] - public string AdminFIO { get; set; } = string.Empty; - - [Required] - public string Email { get; set; } = string.Empty; - - [Required] - public string Password { get; set; } = string.Empty; - - // [ForeignKey("AdminId")] - //public virtual List Visits { get; set; } = new(); - [ForeignKey("AdminId")] - public virtual List Animals { get; set; } = new(); - public static Admin? Create(AdminBindingModel model) - { - if (model == null) - { - return null; - } - return new Admin() - { - Id = model.AdminId, - AdminFIO = model.AdminFIO, - Email = model.Email, - Password = model.Password - }; - } - - public static Admin Create(AdminViewModel model) - { - return new Admin - { - Id = model.Id, - AdminFIO = model.AdminFIO, - Email = model.Email, - Password = model.Password - }; - } - - public void Update(AdminBindingModel model) - { - if (model == null) - { - return; - } - AdminFIO = model.AdminFIO; - Email = model.Email; - Password = model.Password; - } - - public AdminViewModel GetViewModel => new() - { - Id = Id, - AdminFIO = AdminFIO, - Email = Email, - Password = Password - }; - } -} diff --git a/VetClinic/DinerDataBaseImplement/Models/Visit.cs b/VetClinic/DinerDataBaseImplement/Models/Visit.cs deleted file mode 100644 index 9cd620e..0000000 --- a/VetClinic/DinerDataBaseImplement/Models/Visit.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace VetClinicDataBaseImplement.Models -{ - public class Visit - { - } -} diff --git a/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs b/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs deleted file mode 100644 index 6fc17d9..0000000 --- a/VetClinic/DinerDataBaseImplement/VetClinicDatabase.cs +++ /dev/null @@ -1,24 +0,0 @@ -using VetClinicDataBaseImplement.Models; -using Microsoft.EntityFrameworkCore; - - - -namespace VetClinicDataBaseImplement -{ - public class VetClinicDatabase : DbContext - { - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (optionsBuilder.IsConfigured == false) - { - optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS02;Initial Catalog=DinerDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); - } - base.OnConfiguring(optionsBuilder); - } - public virtual DbSet Animals { set; get; } - public virtual DbSet Admins { set; get; } - public virtual DbSet VisitAnimals { set; get; } - public virtual DbSet Visits { set; get; } - // public virtual DbSet Vaccinations { set; get; } - } -} diff --git a/VetClinic/VetClinic.sln b/VetClinic/VetClinic.sln index fb5ab67..0e9dcb2 100644 --- a/VetClinic/VetClinic.sln +++ b/VetClinic/VetClinic.sln @@ -11,10 +11,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicRestApi", "VetClin EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicContracts", "VetClinicContracts\VetClinicContracts.csproj", "{F2944E52-DC59-42B0-98D2-AC93E22DAB8C}" EndProject -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicDataBaseImplement", "DinerDataBaseImplement\VetClinicDataBaseImplement.csproj", "{4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}" -EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "VetClinicAdminApp", "VetClinicAdminApi\VetClinicAdminApp.csproj", "{75280728-CC4C-4C43-8921-2DC4AD8A9192}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VetClinicDataBaseImplement", "VetClinicDataBaseImplement\VetClinicDataBaseImplement.csproj", "{55D8A594-2412-49E5-B306-F7D0385A870D}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -37,14 +37,14 @@ Global {F2944E52-DC59-42B0-98D2-AC93E22DAB8C}.Debug|Any CPU.Build.0 = Debug|Any CPU {F2944E52-DC59-42B0-98D2-AC93E22DAB8C}.Release|Any CPU.ActiveCfg = Release|Any CPU {F2944E52-DC59-42B0-98D2-AC93E22DAB8C}.Release|Any CPU.Build.0 = Release|Any CPU - {4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4BA6DC6C-8D75-4F4B-88E3-E634B360C14F}.Release|Any CPU.Build.0 = Release|Any CPU {75280728-CC4C-4C43-8921-2DC4AD8A9192}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {75280728-CC4C-4C43-8921-2DC4AD8A9192}.Debug|Any CPU.Build.0 = Debug|Any CPU {75280728-CC4C-4C43-8921-2DC4AD8A9192}.Release|Any CPU.ActiveCfg = Release|Any CPU {75280728-CC4C-4C43-8921-2DC4AD8A9192}.Release|Any CPU.Build.0 = Release|Any CPU + {55D8A594-2412-49E5-B306-F7D0385A870D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {55D8A594-2412-49E5-B306-F7D0385A870D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {55D8A594-2412-49E5-B306-F7D0385A870D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {55D8A594-2412-49E5-B306-F7D0385A870D}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs b/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs index 9ac53fe..856e1d3 100644 --- a/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs +++ b/VetClinic/VetClinicContracts/BindingModels/AnimalBindingModel.cs @@ -11,7 +11,7 @@ namespace VetClinicContracts.BindingModels { public int Id { get; set; } public int AdminId { get; set; } - public List VisitAnimals { get; set; } = new(); + public Dictionary VisitAnimals { get; set; } = new(); public string AnimalName { get; set; } = string.Empty; public string? Family { get; set; } = string.Empty; diff --git a/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs b/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs index 971c1b7..86a21fe 100644 --- a/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs +++ b/VetClinic/VetClinicContracts/SearchModels/AnimalSearchModel.cs @@ -12,7 +12,7 @@ namespace VetClinicContracts.SearchModels public int? Id { get; set; } public int? AdminId { get; set; } - public List Visits { get; set; } = new(); + public Dictionary Visits { get; set; } = new(); public string? AnimalName { get; set; } diff --git a/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs b/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs index b123df3..73a5ad4 100644 --- a/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs +++ b/VetClinic/VetClinicContracts/ViewModels/AnimalViewModel.cs @@ -13,7 +13,7 @@ namespace VetClinicContracts.ViewModels public int Id { get; set; } public int AdminId { get; set; } - public List VisitAnimals { get; set; } = new(); + public Dictionary VisitAnimals { get; set; } = new(); [DisplayName("Имя животного")] public string AnimalName { get; set; } = string.Empty; diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/AdminStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/AdminStorage.cs new file mode 100644 index 0000000..56d0cb9 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Implements/AdminStorage.cs @@ -0,0 +1,123 @@ +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; +using VetClinicDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using VetClinicDataBaseImplement; + +namespace VetClinicDataBaseImplement.Implements +{ + public class AdminStorage : IAdminStorage + { + public AdminViewModel? Delete(AdminBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Admins + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + var deletedElement = context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + + context.Admins.Remove(element); + context.SaveChanges(); + + return deletedElement; + } + return null; + } + + public AdminViewModel? GetElement(AdminSearchModel model) + { + using var context = new VetClinicDatabase(); + if (model.Id.HasValue) + { + return context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; + } + else if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password)) + { + return context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .FirstOrDefault(x => (x.Email == model.Email && x.Password == model.Password)) + ?.GetViewModel; + } + return new(); + } + + public List GetFilteredList(AdminSearchModel model) + { + if (string.IsNullOrEmpty(model.AdminFIO)) + { + return new(); + } + using var context = new VetClinicDatabase(); + + return context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .Where(x => x.AdminFIO.Contains(model.AdminFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new VetClinicDatabase(); + + return context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .Select(x => x.GetViewModel) + .ToList(); + } + + public AdminViewModel? Insert(AdminBindingModel model) + { + var newAdmin = Admin.Create(model); + if (newAdmin == null) + { + return null; + } + using var context = new VetClinicDatabase(); + context.Admins.Add(newAdmin); + context.SaveChanges(); + + return context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .FirstOrDefault(x => x.Id == newAdmin.Id) + ?.GetViewModel; + } + + public AdminViewModel? Update(AdminBindingModel model) + { + using var context = new VetClinicDatabase(); + var order = context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + + return context.Admins + //.Include(x => x.Visits) + .Include(x => x.Animals) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + } +} diff --git a/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs b/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs new file mode 100644 index 0000000..fe0f1c7 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs @@ -0,0 +1,110 @@ +using VetClinicContracts.BindingModels; +using VetClinicContracts.SearchModels; +using VetClinicContracts.StoragesContracts; +using VetClinicContracts.ViewModels; +using VetClinicDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using VetClinicDataBaseImplement; + +namespace DinerDataBaseImplement.Implements +{ + public class AnimalStorage + { + public List GetFullList() + { + using var context = new VetClinicDatabase(); + + return context.Animals + //.Include(x => x.Vaccination) + .Include(x => x.Admin) + .Include(x => x.Visits) + .ThenInclude(x => x.Visit) + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(AnimalSearchModel model) + { + + if (string.IsNullOrEmpty(model.AnimalName)) + { + return new(); + } + using var context = new VetClinicDatabase(); + return context.Animals + .Include(x => x.Admin) + .Include(x => x.Visits) + .ThenInclude(x => x.Visit) + .Where(x => x.AnimalName.Contains(model.AnimalName)) + .ToList() + .Select(x => x.GetViewModel).ToList(); + } + public AnimalViewModel? GetElement(AnimalSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new VetClinicDatabase(); + return context.Animals + .Include(x => x.Admin) + .Include(x => x.Visits) + .ThenInclude(x => x.Visit) + //.Include(x => x.Vaccination) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.AnimalName) && x.AnimalName == model.AnimalName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public AnimalViewModel? Insert(AnimalBindingModel model) + { + using var context = new VetClinicDatabase(); + var newAnimal = Animal.Create(context,model); + if (newAnimal == null) + { + return null; + } + context.Animals.Add(newAnimal); + context.SaveChanges(); + return newAnimal.GetViewModel; + } + public AnimalViewModel? Update(AnimalBindingModel model) + { + using var context = new VetClinicDatabase(); + using var transaction = context.Database.BeginTransaction(); + + try + { + var animal = context.Animals.FirstOrDefault(rec => rec.Id == model.Id); + if (animal == null) + { + return null; + } + animal.Update(model); + context.SaveChanges(); + animal.UpdateVisits(context, model); + transaction.Commit(); + return animal.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public AnimalViewModel? Delete(AnimalBindingModel model) + { + using var context = new VetClinicDatabase(); + var element = context.Animals + //.Include(x => x.Visit) + .Include(x => x.Admin) + //.Include(x => x.Vaccination) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + + context.Animals.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Admin.cs b/VetClinic/VetClinicDataBaseImplement/Models/Admin.cs new file mode 100644 index 0000000..ee0a3aa --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Models/Admin.cs @@ -0,0 +1,76 @@ +using VetClinicDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; + +namespace VetClinicDataBaseImplement.Models +{ + public class Admin : IAdminModel + { + public int Id { get; set; } + + [Required] + public string AdminFIO { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + // [ForeignKey("AdminId")] + //public virtual List Visits { get; set; } = new(); + [ForeignKey("AdminId")] + public virtual List Animals { get; set; } = new(); + public static Admin? Create(AdminBindingModel model) + { + if (model == null) + { + return null; + } + return new Admin() + { + Id = model.AdminId, + AdminFIO = model.AdminFIO, + Email = model.Email, + Password = model.Password + }; + } + + public static Admin Create(AdminViewModel model) + { + return new Admin + { + Id = model.Id, + AdminFIO = model.AdminFIO, + Email = model.Email, + Password = model.Password + }; + } + + public void Update(AdminBindingModel model) + { + if (model == null) + { + return; + } + AdminFIO = model.AdminFIO; + Email = model.Email; + Password = model.Password; + } + + public AdminViewModel GetViewModel => new() + { + Id = Id, + AdminFIO = AdminFIO, + Email = Email, + Password = Password + }; + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/Animal.cs b/VetClinic/VetClinicDataBaseImplement/Models/Animal.cs similarity index 57% rename from VetClinic/DinerDataBaseImplement/Models/Animal.cs rename to VetClinic/VetClinicDataBaseImplement/Models/Animal.cs index 194ed37..0189201 100644 --- a/VetClinic/DinerDataBaseImplement/Models/Animal.cs +++ b/VetClinic/VetClinicDataBaseImplement/Models/Animal.cs @@ -9,39 +9,38 @@ using System.Threading.Tasks; using VetClinicContracts.BindingModels; using VetClinicContracts.ViewModels; using System.Collections; +using System.ComponentModel; namespace VetClinicDataBaseImplement.Models { - public class Animal:IAnimalModel + public class Animal : IAnimalModel { public int Id { get; set; } [Required] public int AdminId { get; set; } [Required] - public List? _visitAnimals = null; + public Dictionary? _visitAnimals = null; [Required] - public string AnimalName { get; set; }= string.Empty; + public string AnimalName { get; set; } = string.Empty; [Required] - public string Family { get; set;} = string.Empty; - //public virtual Visit Visit { get; set; } - public virtual Admin Admin { get; set; } - - public virtual Visit Visit { get; set; } - - //public virtual Vaccination Vaccination { get; set; } + public string Family { get; set; } = string.Empty; [ForeignKey("AnimalId")] public virtual List Visits { get; set; } = new(); //[ForeignKey("AnimalId")] //public virtual List Vaccinations { get; set; } = new(); - public List VisitAnimals + + public virtual Admin Admin { get; set; } + [NotMapped] + public Dictionary VisitAnimals { get { if (_visitAnimals == null) { - _visitAnimals = Visits.Select(recPC => recPC.Visit as IVisitModel).ToList(); + _visitAnimals = Visits.ToDictionary(recPC => recPC.VisitId, recPC => + recPC.Animal as IVisitModel); } return _visitAnimals; } @@ -55,15 +54,16 @@ namespace VetClinicDataBaseImplement.Models return new Animal() { Id = model.Id, - Visits = model.VisitAnimals.Select(x => new VisitAnimal - { - Id = x.Id - }).ToList(), AdminId = model.AdminId, //VaccinationId = model.VaccinationId AnimalName = model.AnimalName, - Family = model.Family - + Family = model.Family, + Visits = model.VisitAnimals.Select(x => new VisitAnimal + { + Visit = context.Visits.First(y => y.Id == x.Key) + }).ToList(), + + }; } public void Update(AnimalBindingModel? model) @@ -85,6 +85,27 @@ namespace VetClinicDataBaseImplement.Models Family = Family }; - + public void UpdateVisits(VetClinicDatabase context, AnimalBindingModel model) + { + var visitAnimals = context.VisitAnimals.Where(rec => rec.AnimalId == model.Id).ToList(); + if (visitAnimals != null) + { // удалили те, которых нет в модели + context.VisitAnimals.RemoveRange(visitAnimals.Where(rec => !model.VisitAnimals.ContainsKey(rec.VisitId))); + context.SaveChanges(); + + } + var animal = context.Animals.First(x => x.Id == Id); + foreach (var pc in model.VisitAnimals) + { + context.VisitAnimals.Add(new VisitAnimal + { + Animal = animal, + Visit = context.Visits.First(x => x.Id == pc.Key), + + }); + context.SaveChanges(); + } + _visitAnimals = null; + } } } diff --git a/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs b/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs new file mode 100644 index 0000000..ea254d5 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/Models/Visit.cs @@ -0,0 +1,20 @@ +using VetClinicDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using VetClinicContracts.BindingModels; +using VetClinicContracts.ViewModels; +using System.Collections; +using System.ComponentModel; + +namespace VetClinicDataBaseImplement.Models +{ + public class Visit : IVisitModel + { + public int Id { get; private set; } + } +} diff --git a/VetClinic/DinerDataBaseImplement/Models/VisitAnimal.cs b/VetClinic/VetClinicDataBaseImplement/Models/VisitAnimal.cs similarity index 93% rename from VetClinic/DinerDataBaseImplement/Models/VisitAnimal.cs rename to VetClinic/VetClinicDataBaseImplement/Models/VisitAnimal.cs index 0093b3a..d16f2e3 100644 --- a/VetClinic/DinerDataBaseImplement/Models/VisitAnimal.cs +++ b/VetClinic/VetClinicDataBaseImplement/Models/VisitAnimal.cs @@ -16,7 +16,7 @@ namespace VetClinicDataBaseImplement.Models [Required] public int AnimalId { get; set; } [Required] - public int VisittId { get; set; } + public int VisitId { get; set; } public virtual Animal Animal { get; set; } = new(); public virtual Visit Visit { get; set; } = new(); diff --git a/VetClinic/DinerDataBaseImplement/VetClinicDataBaseImplement.csproj b/VetClinic/VetClinicDataBaseImplement/VetClinicDataBaseImplement.csproj similarity index 90% rename from VetClinic/DinerDataBaseImplement/VetClinicDataBaseImplement.csproj rename to VetClinic/VetClinicDataBaseImplement/VetClinicDataBaseImplement.csproj index 290ccda..27347b9 100644 --- a/VetClinic/DinerDataBaseImplement/VetClinicDataBaseImplement.csproj +++ b/VetClinic/VetClinicDataBaseImplement/VetClinicDataBaseImplement.csproj @@ -8,6 +8,7 @@ + all diff --git a/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs b/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs new file mode 100644 index 0000000..2266641 --- /dev/null +++ b/VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs @@ -0,0 +1,24 @@ +using VetClinicDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + + + +namespace VetClinicDataBaseImplement +{ + public class VetClinicDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS02;Initial Catalog=DinerDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Animals { set; get; } + public virtual DbSet Admins { set; get; } + public virtual DbSet VisitAnimals { set; get; } + public virtual DbSet Visits { set; get; } + // public virtual DbSet Vaccinations { set; get; } + } +} diff --git a/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs b/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs index d3e0bb0..0867340 100644 --- a/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs +++ b/VetClinic/VetClinicDataModels/Models/IAnimalModel.cs @@ -9,7 +9,7 @@ namespace VetClinicDataModels.Models public interface IAnimalModel : IId { int AdminId { get; } - List VisitAnimals { get; } + Dictionary VisitAnimals { get; } string AnimalName { get; } string? Family { get; } } diff --git a/VetClinic/VetClinicDataModels/Models/IVisitModel.cs b/VetClinic/VetClinicDataModels/Models/IVisitModel.cs index b82bf57..c0c70a6 100644 --- a/VetClinic/VetClinicDataModels/Models/IVisitModel.cs +++ b/VetClinic/VetClinicDataModels/Models/IVisitModel.cs @@ -13,6 +13,6 @@ namespace VetClinicDataModels.Models int AdminId { get; } string NameVisit { get;} DateTime DateVisit { get; } - List VisitAnimals { get; } + Dictionary VisitAnimals { get; } } }