Частичная реализация HospitalBusinessLogic и HospitalDatabaseImplement.

This commit is contained in:
Anastasia 2023-04-07 09:43:04 +04:00
parent 63588ee557
commit 7e123f347a
26 changed files with 1629 additions and 8 deletions

View File

@ -7,7 +7,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hospital", "Hospital\Hospit
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalDataModels", "HospitalDataModels\HospitalDataModels.csproj", "{84B572A0-F8F5-40D2-9D9E-ACAEC7A91859}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalContracts", "HospitalContracts\HospitalContracts.csproj", "{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "HospitalBusinessLogic", "HospitalBusinessLogic\HospitalBusinessLogic.csproj", "{861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HospitalDatabaseImplement", "HospitalDatabaseImplement\HospitalDatabaseImplement.csproj", "{2C40C8A1-137C-4B36-BA44-888CD7230E50}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -27,6 +31,14 @@ Global
{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A3FDB59-29E9-459A-8D17-62CA88D1C56E}.Release|Any CPU.Build.0 = Release|Any CPU
{861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{861E0654-2ECE-4A7D-8A82-2E6DB5AEBDCE}.Release|Any CPU.Build.0 = Release|Any CPU
{2C40C8A1-137C-4B36-BA44-888CD7230E50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2C40C8A1-137C-4B36-BA44-888CD7230E50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2C40C8A1-137C-4B36-BA44-888CD7230E50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2C40C8A1-137C-4B36-BA44-888CD7230E50}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,116 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class DiseaseLogic : IDiseaseLogic
{
private readonly ILogger _logger;
private readonly IDiseaseStorage _diseaseStorage;
public DiseaseLogic(ILogger<DiseaseLogic> logger, IDiseaseStorage diseaseStorage)
{
_logger = logger;
_diseaseStorage = diseaseStorage;
}
public bool Create(DiseaseBindingModel model)
{
CheckModel(model);
if (_diseaseStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DiseaseBindingModel model)
{
CheckModel(model);
if (_diseaseStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DiseaseBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_diseaseStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public DiseaseViewModel? ReadElement(DiseaseSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id);
var element = _diseaseStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<DiseaseViewModel>? ReadList(DiseaseSearchModel? model)
{
_logger.LogInformation("ReadList. DiseaseId:{Id}", model?.Id);
var list = model == null ? _diseaseStorage.GetFullList() : _diseaseStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(DiseaseBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия болезни", nameof(model.Name));
}
_logger.LogInformation("Disease. Name:{Name}. Symptoms:{Symptoms}. DoctorId:{DoctorId}. Id:{ Id}", model.Name, model.Symptoms, model.DoctorId, model.Id);
var element = _diseaseStorage.GetElement(new DiseaseSearchModel
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Болезнь с такими данными уже есть");
}
}
}
}

View File

@ -0,0 +1,124 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class DoctorLogic : IDoctorLogic
{
private readonly ILogger _logger;
private readonly IDoctorStorage _doctorStorage;
public DoctorLogic(ILogger<DoctorLogic> logger, IDoctorStorage doctorStorage)
{
_logger = logger;
_doctorStorage = doctorStorage;
}
public bool Create(DoctorBindingModel model)
{
CheckModel(model);
if (_doctorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(DoctorBindingModel model)
{
CheckModel(model);
if (_doctorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(DoctorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_doctorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public DoctorViewModel? ReadElement(DoctorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Login:{Login}. PhoneNumber:{PhoneNumber}. Id:{ Id}", model.Login, model.PhoneNumber, model.Id);
var element = _doctorStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<DoctorViewModel>? ReadList(DoctorSearchModel? model)
{
_logger.LogInformation("ReadList. DoctorId:{Id}", model?.Id);
var list = model == null ? _doctorStorage.GetFullList() : _doctorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(DoctorBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Login))
{
throw new ArgumentNullException("Нет логина врача", nameof(model.Login));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("Нет номера телефона врача", nameof(model.PhoneNumber));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля врача", nameof(model.Password));
}
_logger.LogInformation("Doctor. Login:{Login}. PhoneNumber:{PhoneNumber}. Password:{Password}. Id:{ Id}", model.Login, model.PhoneNumber, model.Password, model.Id);
var element = _doctorStorage.GetElement(new DoctorSearchModel
{
Login = model.Login,
PhoneNumber = model.PhoneNumber,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Врач с таким именем уже есть");
}
}
}
}

View File

@ -0,0 +1,115 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class MedicineLogic : IMedicineLogic
{
private readonly ILogger _logger;
private readonly IMedicineStorage _medicineStorage;
public MedicineLogic(ILogger<MedicineLogic> logger, IMedicineStorage medicineStorage)
{
_logger = logger;
_medicineStorage = medicineStorage;
}
public bool Create(MedicineBindingModel model)
{
CheckModel(model);
if (_medicineStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(MedicineBindingModel model)
{
CheckModel(model);
if (_medicineStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(MedicineBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_medicineStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public MedicineViewModel? ReadElement(MedicineSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id);
var element = _medicineStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<MedicineViewModel>? ReadList(MedicineSearchModel? model)
{
_logger.LogInformation("ReadList. MedicineId:{Id}", model?.Id);
var list = model == null ? _medicineStorage.GetFullList() : _medicineStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(MedicineBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет навзания лекарства", nameof(model.Name));
}
_logger.LogInformation("Medicine. Name:{Name}. Id:{ Id}", model.Name, model.Id);
var element = _medicineStorage.GetElement(new MedicineSearchModel
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Лекарство с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,129 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class PatientLogic : IPatientLogic
{
private readonly ILogger _logger;
private readonly IPatientStorage _patientStorage;
public PatientLogic(ILogger<PatientLogic> logger, IPatientStorage patientStorage)
{
_logger = logger;
_patientStorage = patientStorage;
}
public bool Create(PatientBindingModel model)
{
CheckModel(model);
if (_patientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PatientBindingModel model)
{
CheckModel(model);
if (_patientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PatientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_patientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public PatientViewModel? ReadElement(PatientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Surname:{Surname}. Name:{Name}. Patronymic:{Patronymic}. Id:{ Id}", model.Surname, model.Name, model.Patronymic, model.Id);
var element = _patientStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<PatientViewModel>? ReadList(PatientSearchModel? model)
{
_logger.LogInformation("ReadList. PatientId:{Id}", model?.Id);
var list = model == null ? _patientStorage.GetFullList() : _patientStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(PatientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Surname))
{
throw new ArgumentNullException("Нет фамилии пациента", nameof(model.Surname));
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени пациента", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Patronymic))
{
throw new ArgumentNullException("Нет отчества пациента", nameof(model.Patronymic));
}
if (model.Age < 0)
{
throw new ArgumentNullException("Возраст пациента должен быть больше 0", nameof(model.Patronymic));
}
_logger.LogInformation("Patient. Login:{Login}. PhoneNumber:{PhoneNumber}. Password:{Password}. Age:{Age}. DoctorId:{DoctorId}. Id:{ Id}", model.Surname, model.Name, model.Patronymic, model.Age, model.DoctorId, model.Id);
var element = _patientStorage.GetElement(new PatientSearchModel
{
Surname = model.Surname,
Name = model.Name,
Patronymic = model.Patronymic,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Пациент с такими данными уже есть");
}
}
}
}

View File

@ -0,0 +1,115 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class ProcedureLogic : IProcedureLogic
{
private readonly ILogger _logger;
private readonly IProcedureStorage _procedureStorage;
public ProcedureLogic(ILogger<ProcedureLogic> logger, IProcedureStorage procedureStorage)
{
_logger = logger;
_procedureStorage = procedureStorage;
}
public bool Create(ProcedureBindingModel model)
{
CheckModel(model);
if (_procedureStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ProcedureBindingModel model)
{
CheckModel(model);
if (_procedureStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ProcedureBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_procedureStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ProcedureViewModel? ReadElement(ProcedureSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}. Id:{ Id}", model.Name, model.Id);
var element = _procedureStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<ProcedureViewModel>? ReadList(ProcedureSearchModel? model)
{
_logger.LogInformation("ReadList. ProcedureId:{Id}", model?.Id);
var list = model == null ? _procedureStorage.GetFullList() : _procedureStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(ProcedureBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия процедуры", nameof(model.Name));
}
_logger.LogInformation("Procedure. Name:{Name}. Id:{ Id}", model.Name, model.Id);
var element = _procedureStorage.GetElement(new ProcedureSearchModel
{
Name = model.Name,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Процедура с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,115 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class RecipeLogic : IRecipeLogic
{
private readonly ILogger _logger;
private readonly IRecipeStorage _recipeStorage;
public RecipeLogic(ILogger<RecipeLogic> logger, IRecipeStorage recipeStorage)
{
_logger = logger;
_recipeStorage = recipeStorage;
}
public bool Create(RecipeBindingModel model)
{
CheckModel(model);
if (_recipeStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(RecipeBindingModel model)
{
CheckModel(model);
if (_recipeStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(RecipeBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_recipeStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public RecipeViewModel? ReadElement(RecipeSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
var element = _recipeStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public List<RecipeViewModel>? ReadList(RecipeSearchModel? model)
{
_logger.LogInformation("ReadList. RecipeId:{Id}", model?.Id);
var list = model == null ? _recipeStorage.GetFullList() : _recipeStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
private void CheckModel(RecipeBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.RecipeMedicines == null || model.RecipeMedicines.Count == 0)
{
throw new ArgumentNullException("Нет лекарств в рецепте", nameof(model.RecipeMedicines));
}
_logger.LogInformation("Recipe. Id:{ Id}. DiseaseId:{DiseaseId}. DoctorId:{DoctorId}", model.Id, model.DiseaseId, model.DoctorId);
var element = _recipeStorage.GetElement(new RecipeSearchModel
{
Id = model.Id,
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Пациент с такими данными уже есть");
}
}
}
}

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HospitalContracts\HospitalContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -12,7 +12,5 @@ namespace HospitalContracts.BindingModels
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string Form { get; set; } = string.Empty;
}
}

View File

@ -14,8 +14,5 @@ namespace HospitalContracts.ViewModels
[DisplayName("Название лекарства")]
public string Name { get; set; } = string.Empty;
[DisplayName("Форма выпуска лекарства")]
public string Form { get; set; } = string.Empty;
}
}

View File

@ -9,7 +9,5 @@ namespace HospitalDataModels.Models
public interface IMedicineModel : IId
{
string Name { get; }
string Form { get; }
}
}

View File

@ -0,0 +1,12 @@
{
"profiles": {
"HospitalDataModels": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:65343;http://localhost:65344"
}
}
}

View File

@ -0,0 +1,42 @@
using HospitalDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement
{
public class HospitalDatabase : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=HospitalDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Doctor> Doctors { set; get; }
public virtual DbSet<Patient> Patients { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; }
public virtual DbSet<Disease> Diseases { set; get; }
public virtual DbSet<Medicine> Medicines { set; get; }
public virtual DbSet<Procedure> Procedures { set; get; }
public virtual DbSet<PatientRecipe> PatientRecipes { set; get; }
public virtual DbSet<PatientProcedure> PatientProcedures { set; get; }
public virtual DbSet<RecipeMedicine> RecipeMedicines { set; get; }
public virtual DbSet<ProcedureMedicine> ProcedureMedicines { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HospitalContracts\HospitalContracts.csproj" />
<ProjectReference Include="..\HospitalDataModels\HospitalDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,85 @@
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Implements
{
public class DoctorStorage : IDoctorStorage
{
public DoctorViewModel? GetElement(DoctorSearchModel model)
{
if (string.IsNullOrEmpty(model.Login) && string.IsNullOrEmpty(model.PhoneNumber) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Doctors.FirstOrDefault(x =>(!string.IsNullOrEmpty(model.Login) && x.Login == model.Login &&
!string.IsNullOrEmpty(model.PhoneNumber) && x.PhoneNumber == model.PhoneNumber &&
!string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<DoctorViewModel> GetFilteredList(DoctorSearchModel model)
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new HospitalDatabase();
return context.Doctors.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList();
}
public List<DoctorViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Doctors.Select(x => x.GetViewModel).ToList();
}
public DoctorViewModel? Insert(DoctorBindingModel model)
{
var newDoctor = Doctor.Create(model);
if (newDoctor == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Doctors.Add(newDoctor);
context.SaveChanges();
return newDoctor.GetViewModel;
}
public DoctorViewModel? Update(DoctorBindingModel model)
{
using var context = new HospitalDatabase();
var doctor = context.Doctors.FirstOrDefault(x => x.Id == model.Id);
if (doctor == null)
{
return null;
}
doctor.Update(model);
context.SaveChanges();
return doctor.GetViewModel;
}
public DoctorViewModel? Delete(DoctorBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Doctors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,84 @@
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Implements
{
public class PatientStorage : IPatientStorage
{
public PatientViewModel? GetElement(PatientSearchModel model)
{
if (string.IsNullOrEmpty(model.Surname) && string.IsNullOrEmpty(model.Name) && string.IsNullOrEmpty(model.Patronymic) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Doctors.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) && x.Login == model.Login &&
!string.IsNullOrEmpty(model.PhoneNumber) && x.PhoneNumber == model.PhoneNumber &&
!string.IsNullOrEmpty(model.Password) && x.Password == model.Password) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<PatientViewModel> GetFilteredList(PatientSearchModel model)
{
if (string.IsNullOrEmpty(model.Login))
{
return new();
}
using var context = new HospitalDatabase();
return context.Doctors.Where(x => x.Login.Contains(model.Login)).Select(x => x.GetViewModel).ToList();
}
public List<PatientViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Doctors.Select(x => x.GetViewModel).ToList();
}
public PatientViewModel? Insert(PatientBindingModel model)
{
var newDoctor = Doctor.Create(model);
if (newDoctor == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Doctors.Add(newDoctor);
context.SaveChanges();
return newDoctor.GetViewModel;
}
public PatientViewModel? Update(PatientBindingModel model)
{
using var context = new HospitalDatabase();
var doctor = context.Doctors.FirstOrDefault(x => x.Id == model.Id);
if (doctor == null)
{
return null;
}
doctor.Update(model);
context.SaveChanges();
return doctor.GetViewModel;
}
public PatientViewModel? Delete(PatientBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Doctors.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Doctors.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,75 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.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;
namespace HospitalDatabaseImplement.Models
{
public class Disease : IDiseaseModel
{
public int Id { get; set; }
public int DoctorId { get; private set; }
public virtual Doctor Doctor { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; } = string.Empty;
[Required]
[MaxLength(150)]
public string Symptoms { get; set; } = string.Empty;
[ForeignKey("DiseaseId")]
public virtual List<Recipe> Recipes { get; set; } = new();
public static Disease? Create(DiseaseBindingModel model)
{
if (model == null)
{
return null;
}
return new Disease()
{
Id = model.Id,
Name = model.Name,
Symptoms = model.Symptoms,
DoctorId = model.DoctorId
};
}
public static Disease Create(DiseaseViewModel model)
{
return new Disease
{
Id = model.Id,
Name = model.Name,
Symptoms = model.Symptoms
};
}
public void Update(DiseaseBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
Symptoms = model.Symptoms;
}
public DiseaseViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Symptoms = Symptoms
};
}
}

View File

@ -0,0 +1,83 @@
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
namespace HospitalDatabaseImplement.Models
{
public class Doctor : IDoctorModel
{
public int Id { get; set; }
[Required]
[MaxLength(25)]
public string Login { get; set; } = string.Empty;
[Required]
[MaxLength(11)]
public string PhoneNumber { get; set; } = string.Empty;
[Required]
[MaxLength(30)]
public string Password { get; set; } = string.Empty;
[ForeignKey("DoctorId")]
public virtual List<Patient> Patients { get; set; } = new();
[ForeignKey("DoctorId")]
public virtual List<Recipe> Recipes { get; set; } = new();
[ForeignKey("DoctorId")]
public virtual List<Disease> Diseases { get; set; } = new();
public static Doctor? Create(DoctorBindingModel model)
{
if (model == null)
{
return null;
}
return new Doctor()
{
Id = model.Id,
Login = model.Login,
PhoneNumber = model.PhoneNumber,
Password = model.Password
};
}
public static Doctor Create(DoctorViewModel model)
{
return new Doctor
{
Id = model.Id,
Login = model.Login,
PhoneNumber = model.PhoneNumber,
Password = model.Password
};
}
public void Update(DoctorBindingModel model)
{
if (model == null)
{
return;
}
Login = model.Login;
PhoneNumber = model.PhoneNumber;
Password = model.Password;
}
public DoctorViewModel GetViewModel => new()
{
Id = Id,
Login = Login,
PhoneNumber = PhoneNumber,
Password = Password
};
}
}

View File

@ -0,0 +1,60 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.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 System.ComponentModel;
namespace HospitalDatabaseImplement.Models
{
public class Medicine : IMedicineModel
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; } = string.Empty;
[ForeignKey("MedicineId")]
public virtual List<RecipeMedicine> RecipeMedicines { get; set; } = new();
public static Medicine? Create(MedicineBindingModel model)
{
if (model == null)
{
return null;
}
return new Medicine()
{
Id = model.Id,
Name = model.Name
};
}
public static Medicine Create(MedicineViewModel model)
{
return new Medicine
{
Id = model.Id,
Name = model.Name
};
}
public void Update(MedicineBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
}
public MedicineViewModel GetViewModel => new()
{
Id = Id,
Name = Name
};
}
}

View File

@ -0,0 +1,136 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.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 System.Diagnostics;
namespace HospitalDatabaseImplement.Models
{
public class Patient : IPatientModel
{
public int Id { get; set; }
public int DoctorId { get; private set; }
public virtual Doctor Doctor { get; set; }
[Required]
[MaxLength(50)]
public string Surname { get; set; } = string.Empty;
[Required]
[MaxLength(50)]
public string Name { get; set; } = string.Empty;
[Required]
[MaxLength(50)]
public string Patronymic { get; set; } = string.Empty;
[Required]
public int Age { get; set; }
private Dictionary<int, IRecipeModel>? _patientRecipes = null;
[NotMapped]
public Dictionary<int, IRecipeModel> PatientRecipes
{
get
{
if (_patientRecipes == null)
{
_patientRecipes = Recipes.ToDictionary(recPR => recPR.RecipeId, recPR => (recPR.Recipe as IRecipeModel));
}
return _patientRecipes;
}
}
[ForeignKey("PatientId")]
public virtual List<PatientRecipe> Recipes { get; set; } = new();
private Dictionary<int, IProcedureModel>? _patientProcedures = null;
[NotMapped]
public Dictionary<int, IProcedureModel> PatientProcedures
{
get
{
if (_patientProcedures == null)
{
_patientProcedures = Procedures.ToDictionary(recPP => recPP.ProcedureId, recPP => (recPP.Procedure as IProcedureModel));
}
return _patientProcedures;
}
}
[ForeignKey("PatientId")]
public virtual List<PatientProcedure> Procedures { get; set; } = new();
public static Patient Create(HospitalDatabase context, PatientBindingModel model)
{
return new Patient()
{
Id = model.Id,
Surname = model.Surname,
Name = model.Name,
Patronymic = model.Patronymic,
Age = model.Age,
DoctorId = model.DoctorId,
Doctor = context.Doctors.First(x => x.Id == model.DoctorId),
Recipes = model.PatientRecipes.Select(x => new PatientRecipe
{
Recipe = context.Recipes.First(y => y.Id == x.Key),
}).ToList(),
Procedures = model.PatientProcedures.Select(x => new PatientProcedure
{
Procedure = context.Procedures.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(PatientBindingModel model)
{
Surname = model.Surname;
Name = model.Name;
Patronymic = model.Patronymic;
Age = model.Age;
}
public PatientViewModel GetViewModel => new()
{
Surname = Surname,
Name = Name,
Patronymic = Patronymic,
Age = Age,
PatientRecipes = PatientRecipes,
PatientProcedures = PatientProcedures
};
public void UpdateRecipes(HospitalDatabase context, PatientBindingModel model)
{
var patientRecipes = context.PatientRecipes.Where(rec => rec.PatientId == model.Id).ToList();
if (patientRecipes != null)
{ // удалили те, которых нет в модели
context.PatientRecipes.RemoveRange(patientRecipes.Where(rec => !model.PatientRecipes.ContainsKey(rec.RecipeId)));
context.SaveChanges();
}
var patient = context.Patients.First(x => x.Id == Id);
foreach (var pr in model.PatientRecipes)
{
context.PatientRecipes.Add(new PatientRecipe
{
Patient = patient,
Recipe = context.Recipes.First(x => x.Id == pr.Key),
});
context.SaveChanges();
}
_patientRecipes = null;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Models
{
public class PatientProcedure
{
public int Id { get; set; }
[Required]
public int PatientId { get; set; }
[Required]
public int ProcedureId { get; set; }
public virtual Patient Patient { get; set; } = new();
public virtual Procedure Procedure { get; set; } = new();
}
}

View File

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Models
{
public class PatientRecipe
{
public int Id { get; set; }
[Required]
public int PatientId { get; set; }
[Required]
public int RecipeId { get; set; }
public virtual Patient Patient { get; set; } = new();
public virtual Recipe Recipe { get; set; } = new();
}
}

View File

@ -0,0 +1,91 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.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 System.Diagnostics;
namespace HospitalDatabaseImplement.Models
{
public class Procedure : IProcedureModel
{
public int Id { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; } = string.Empty;
[ForeignKey("ProcedureId")]
public virtual List<PatientProcedure> PatientProcedures { get; set; } = new();
private Dictionary<int, IMedicineModel>? _procedureMedicines = null;
[NotMapped]
public Dictionary<int, IMedicineModel> ProcedureMedicines
{
get
{
if (_procedureMedicines == null)
{
_procedureMedicines = Medicines.ToDictionary(recPM => recPM.MedicineId, recPM =>(recPM.Medicine as IMedicineModel));
}
return _procedureMedicines;
}
}
[ForeignKey("ProcedureId")]
public virtual List<ProcedureMedicine> Medicines { get; set; } = new();
public static Procedure Create(HospitalDatabase context, ProcedureBindingModel model)
{
return new Procedure()
{
Id = model.Id,
Name = model.Name,
Medicines = model.ProcedureMedicines.Select(x => new ProcedureMedicine
{
Medicine = context.Medicines.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(ProcedureBindingModel model)
{
Name = model.Name;
}
public ProcedureViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
ProcedureMedicines = ProcedureMedicines
};
public void UpdateMedicines(HospitalDatabase context, ProcedureBindingModel model)
{
var procedureMedicines = context.ProcedureMedicines.Where(rec => rec.ProcedureId == model.Id).ToList();
if (procedureMedicines != null)
{ // удалили те, которых нет в модели
context.ProcedureMedicines.RemoveRange(procedureMedicines.Where(rec => !model.ProcedureMedicines.ContainsKey(rec.MedicineId)));
context.SaveChanges();
}
var procedure = context.Procedures.First(x => x.Id == Id);
foreach (var pm in model.ProcedureMedicines)
{
context.ProcedureMedicines.Add(new ProcedureMedicine
{
Procedure = procedure,
Medicine = context.Medicines.First(x => x.Id == pm.Key)
});
context.SaveChanges();
}
_procedureMedicines = null;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Models
{
public class ProcedureMedicine
{
public int Id { get; set; }
[Required]
public int ProcedureId { get; set; }
[Required]
public int MedicineId { get; set; }
public virtual Procedure Procedure { get; set; } = new();
public virtual Medicine Medicine { get; set; } = new();
}
}

View File

@ -0,0 +1,96 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.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;
namespace HospitalDatabaseImplement.Models
{
public class Recipe : IRecipeModel
{
public int Id { get; set; }
public int DoctorId { get; private set; }
public virtual Doctor Doctor { get; set; }
public int DiseaseId { get; private set; }
public virtual Disease Disease { get; set; }
[Required]
public DateTime IssueDate { get; private set; } = DateTime.Now;
private Dictionary<int, IMedicineModel>? _recipeMedicines = null;
[NotMapped]
public Dictionary<int, IMedicineModel> RecipeMedicines
{
get
{
if (_recipeMedicines == null)
{
_recipeMedicines = Medicines.ToDictionary(recRM => recRM.MedicineId, recRM => (recRM.Medicine as IMedicineModel));
}
return _recipeMedicines;
}
}
[ForeignKey("RecipeId")]
public virtual List<RecipeMedicine> Medicines { get; set; } = new();
public static Recipe Create(HospitalDatabase context, RecipeBindingModel model)
{
return new Recipe()
{
Id = model.Id,
IssueDate = model.IssueDate,
DiseaseId = model.DiseaseId,
DoctorId = model.DoctorId,
Doctor = context.Doctors.First(x => x.Id == model.DoctorId),
Medicines = model.RecipeMedicines.Select(x => new RecipeMedicine
{
Medicine = context.Medicines.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(RecipeBindingModel model)
{
IssueDate = model.IssueDate;
}
public RecipeViewModel GetViewModel => new()
{
Id = Id,
IssueDate = IssueDate,
RecipeMedicines = RecipeMedicines
};
public void UpdateMedicines(HospitalDatabase context, RecipeBindingModel model)
{
var recipeMedicines = context.RecipeMedicines.Where(rec => rec.RecipeId == model.Id).ToList();
if (recipeMedicines != null)
{ // удалили те, которых нет в модели
context.RecipeMedicines.RemoveRange(recipeMedicines.Where(rec => !model.RecipeMedicines.ContainsKey(rec.MedicineId)));
context.SaveChanges();
}
var recipe = context.Recipes.First(x => x.Id == Id);
foreach (var pr in model.RecipeMedicines)
{
context.RecipeMedicines.Add(new RecipeMedicine
{
Recipe = recipe,
Medicine = context.Medicines.First(x => x.Id == pr.Key),
});
context.SaveChanges();
}
_recipeMedicines = null;
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDatabaseImplement.Models
{
public class RecipeMedicine
{
public int Id { get; set; }
[Required]
public int RecipeId { get; set; }
[Required]
public int MedicineId { get; set; }
public virtual Recipe Recipe { get; set; } = new();
public virtual Medicine Medicine { get; set; } = new();
}
}