Некое незримое существо или закон, подобно длани господней, парящей над миром?
This commit is contained in:
parent
e89d8a07d3
commit
196dbf8f6c
@ -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<AdminViewModel> 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<AdminViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<AnimalViewModel> 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<AnimalViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<Visit> Visits { get; set; } = new();
|
||||
[ForeignKey("AdminId")]
|
||||
public virtual List<Animal> 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
|
||||
};
|
||||
}
|
||||
}
|
@ -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
|
||||
{
|
||||
}
|
||||
}
|
@ -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<Animal> Animals { set; get; }
|
||||
public virtual DbSet<Admin> Admins { set; get; }
|
||||
public virtual DbSet<VisitAnimal> VisitAnimals { set; get; }
|
||||
public virtual DbSet<Visit> Visits { set; get; }
|
||||
// public virtual DbSet<Vaccination> Vaccinations { set; get; }
|
||||
}
|
||||
}
|
@ -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
|
||||
|
@ -11,7 +11,7 @@ namespace VetClinicContracts.BindingModels
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int AdminId { get; set; }
|
||||
public List<IVisitModel> VisitAnimals { get; set; } = new();
|
||||
public Dictionary<int, IVisitModel> VisitAnimals { get; set; } = new();
|
||||
public string AnimalName { get; set; } = string.Empty;
|
||||
|
||||
public string? Family { get; set; } = string.Empty;
|
||||
|
@ -12,7 +12,7 @@ namespace VetClinicContracts.SearchModels
|
||||
public int? Id { get; set; }
|
||||
public int? AdminId { get; set; }
|
||||
|
||||
public List<IVisitModel> Visits { get; set; } = new();
|
||||
public Dictionary<int, IVisitModel> Visits { get; set; } = new();
|
||||
|
||||
public string? AnimalName { get; set; }
|
||||
|
||||
|
@ -13,7 +13,7 @@ namespace VetClinicContracts.ViewModels
|
||||
public int Id { get; set; }
|
||||
public int AdminId { get; set; }
|
||||
|
||||
public List<IVisitModel> VisitAnimals { get; set; } = new();
|
||||
public Dictionary<int, IVisitModel> VisitAnimals { get; set; } = new();
|
||||
[DisplayName("Имя животного")]
|
||||
public string AnimalName { get; set; } = string.Empty;
|
||||
|
||||
|
123
VetClinic/VetClinicDataBaseImplement/Implements/AdminStorage.cs
Normal file
123
VetClinic/VetClinicDataBaseImplement/Implements/AdminStorage.cs
Normal file
@ -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<AdminViewModel> 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<AdminViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
110
VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs
Normal file
110
VetClinic/VetClinicDataBaseImplement/Implements/AnimalStorage.cs
Normal file
@ -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<AnimalViewModel> 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<AnimalViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
76
VetClinic/VetClinicDataBaseImplement/Models/Admin.cs
Normal file
76
VetClinic/VetClinicDataBaseImplement/Models/Admin.cs
Normal file
@ -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<Visit> Visits { get; set; } = new();
|
||||
[ForeignKey("AdminId")]
|
||||
public virtual List<Animal> 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
|
||||
};
|
||||
}
|
||||
}
|
@ -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<IVisitModel>? _visitAnimals = null;
|
||||
public Dictionary<int, IVisitModel>? _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<VisitAnimal> Visits { get; set; } = new();
|
||||
//[ForeignKey("AnimalId")]
|
||||
//public virtual List<Vaccination> Vaccinations { get; set; } = new();
|
||||
public List<IVisitModel> VisitAnimals
|
||||
|
||||
public virtual Admin Admin { get; set; }
|
||||
[NotMapped]
|
||||
public Dictionary<int, IVisitModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
20
VetClinic/VetClinicDataBaseImplement/Models/Visit.cs
Normal file
20
VetClinic/VetClinicDataBaseImplement/Models/Visit.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
@ -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();
|
@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
24
VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs
Normal file
24
VetClinic/VetClinicDataBaseImplement/VetClinicDatabase .cs
Normal file
@ -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<Animal> Animals { set; get; }
|
||||
public virtual DbSet<Admin> Admins { set; get; }
|
||||
public virtual DbSet<VisitAnimal> VisitAnimals { set; get; }
|
||||
public virtual DbSet<Visit> Visits { set; get; }
|
||||
// public virtual DbSet<Vaccination> Vaccinations { set; get; }
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ namespace VetClinicDataModels.Models
|
||||
public interface IAnimalModel : IId
|
||||
{
|
||||
int AdminId { get; }
|
||||
List <IVisitModel> VisitAnimals { get; }
|
||||
Dictionary<int, IVisitModel> VisitAnimals { get; }
|
||||
string AnimalName { get; }
|
||||
string? Family { get; }
|
||||
}
|
||||
|
@ -13,6 +13,6 @@ namespace VetClinicDataModels.Models
|
||||
int AdminId { get; }
|
||||
string NameVisit { get;}
|
||||
DateTime DateVisit { get; }
|
||||
List<IAnimalModel> VisitAnimals { get; }
|
||||
Dictionary<int,IAnimalModel> VisitAnimals { get; }
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user