Исправление: поле treatmentId у пациента теперь nullable

This commit is contained in:
prodigygirl 2023-04-07 14:57:17 +04:00
parent 54c3ec41b0
commit 6520eab6a2
9 changed files with 546 additions and 18 deletions

View File

@ -3,6 +3,7 @@ using HospitalContracts.BusinessLogicContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StorageContracts;
using HospitalContracts.ViewModels;
using System.Linq;
namespace HospitalBusinessLogic
{
@ -44,7 +45,7 @@ namespace HospitalBusinessLogic
var treatments = _treatmentStorage.GetFullList().Where(x => (x.TreatmentProcedures.Keys.Intersect(procedures).Any() || treatmentsInRecipes.Contains(x.Id))).Select(x => x.Id);
// получаем список пациентов, которые связаны с найденными лечениями
var patients = _patientStorage.GetFullList().Where(x => treatments.Contains(x.TreatmentId));
var patients = _patientStorage.GetFullList().Where(x => x.TreatmentId.HasValue).Where(x => treatments.Contains(x.TreatmentId!.Value));
var record = new ReportPatientsMedicinesViewModel
{

View File

@ -14,6 +14,6 @@ namespace HospitalContracts.BindingModels
public DateTime BirthDate { get; set; }
public int TreatmentId { get; set; }
public int? TreatmentId { get; set; }
}
}

View File

@ -15,11 +15,11 @@ namespace HospitalContracts.ViewModels
public string Name { get; set; } = string.Empty;
public string Patronymic { get; set; } = string.Empty;
public string? Patronymic { get; set; } = string.Empty;
public DateTime BirthDate { get; set; }
public int TreatmentId { get; set; }
public string TreatmentName { get; set; } = string.Empty;
public int? TreatmentId { get; set; }
public string? TreatmentName { get; set; } = string.Empty;
}
}

View File

@ -6,6 +6,6 @@
string Name { get; }
string? Patronymic { get; }
DateTime BirthDate { get; }
int TreatmentId { get; }
int? TreatmentId { get; }
}
}

View File

@ -39,6 +39,12 @@ namespace HospitalDatabaseImplement
.Entity<Prescription>().HasOne(e => e.Apothecary)
.WithMany(e => e.Prescriptions)
.OnDelete(DeleteBehavior.Restrict);
// при удалении лечения зануляем данное поле у пациентов
// можно было ввести статус записи лечения - активный/неактивный, но по заданию функционала удаления лечений нет
modelBuilder
.Entity<Patient>().HasOne(e => e.Treatment)
.WithMany(e => e.Patients)
.OnDelete(DeleteBehavior.SetNull);
}
public virtual DbSet<Apothecary> Apothecaries { set; get; }
public virtual DbSet<Recipe> Recipes { set; get; }

View File

@ -0,0 +1,467 @@
// <auto-generated />
using System;
using HospitalDatabaseImplement;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace HospitalDatabaseImplement.Migrations
{
[DbContext(typeof(HospitalDatabase))]
[Migration("20230407105259_FixPatient")]
partial class FixPatient
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("HospitalDatabaseImplement.Models.Apothecary", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Login")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.Property<string>("Password")
.IsRequired()
.HasMaxLength(55)
.HasColumnType("nvarchar(55)");
b.HasKey("Id");
b.ToTable("Apothecaries");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ApothecaryId")
.HasColumnType("int");
b.Property<double>("Cost")
.HasColumnType("float");
b.Property<string>("Dose")
.IsRequired()
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(60)
.HasColumnType("nvarchar(60)");
b.HasKey("Id");
b.HasIndex("ApothecaryId");
b.ToTable("Medicines");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Patient", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<DateTime>("BirthDate")
.HasColumnType("datetime2");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<string>("Patronymic")
.HasColumnType("nvarchar(max)");
b.Property<string>("Surname")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int?>("TreatmentId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("TreatmentId");
b.ToTable("Patients");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Prescription", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ApothecaryId")
.HasColumnType("int");
b.Property<DateTime>("Date")
.HasColumnType("datetime2");
b.Property<int>("MedicineId")
.HasColumnType("int");
b.Property<int>("Number")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ApothecaryId");
b.HasIndex("MedicineId");
b.ToTable("Prescriptions");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Procedure", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.HasKey("Id");
b.ToTable("Procedures");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.ProcedureMedicine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("MedicineId")
.HasColumnType("int");
b.Property<int>("ProcedureId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("MedicineId");
b.HasIndex("ProcedureId");
b.ToTable("ProcedureMedicines");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Recipe", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ApothecaryId")
.HasColumnType("int");
b.Property<DateTime>("Date")
.HasColumnType("datetime2");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("nvarchar(50)");
b.HasKey("Id");
b.HasIndex("ApothecaryId");
b.ToTable("Recipes");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.RecipeMedicine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("MedicineId")
.HasColumnType("int");
b.Property<int>("RecipeId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("MedicineId");
b.HasIndex("RecipeId");
b.ToTable("RecipeMedicines");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.RecipeTreatment", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("RecipeId")
.HasColumnType("int");
b.Property<int>("TreatmentId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("RecipeId");
b.HasIndex("TreatmentId");
b.ToTable("RecipeTreatments");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Treatment", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(55)
.HasColumnType("nvarchar(55)");
b.HasKey("Id");
b.ToTable("Treatments");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.TreatmentProcedure", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("ProcedureId")
.HasColumnType("int");
b.Property<int>("TreatmentId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("ProcedureId");
b.HasIndex("TreatmentId");
b.ToTable("TreatmentProcedures");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
.WithMany("Medicines")
.HasForeignKey("ApothecaryId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Apothecary");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Patient", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Treatment", "Treatment")
.WithMany("Patients")
.HasForeignKey("TreatmentId")
.OnDelete(DeleteBehavior.SetNull);
b.Navigation("Treatment");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Prescription", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
.WithMany("Prescriptions")
.HasForeignKey("ApothecaryId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.HasOne("HospitalDatabaseImplement.Models.Medicine", "Medicine")
.WithMany("Prescriptions")
.HasForeignKey("MedicineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Apothecary");
b.Navigation("Medicine");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.ProcedureMedicine", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Medicine", "Medicine")
.WithMany("Procedures")
.HasForeignKey("MedicineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("HospitalDatabaseImplement.Models.Procedure", "Procedure")
.WithMany("Medicines")
.HasForeignKey("ProcedureId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Medicine");
b.Navigation("Procedure");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Recipe", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Apothecary", "Apothecary")
.WithMany("Recipes")
.HasForeignKey("ApothecaryId")
.OnDelete(DeleteBehavior.Restrict)
.IsRequired();
b.Navigation("Apothecary");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.RecipeMedicine", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Medicine", "Medicine")
.WithMany("Recipes")
.HasForeignKey("MedicineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("HospitalDatabaseImplement.Models.Recipe", "Recipe")
.WithMany("Medicines")
.HasForeignKey("RecipeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Medicine");
b.Navigation("Recipe");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.RecipeTreatment", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Recipe", "Recipe")
.WithMany("Treatments")
.HasForeignKey("RecipeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("HospitalDatabaseImplement.Models.Treatment", "Treatment")
.WithMany("Recipes")
.HasForeignKey("TreatmentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Recipe");
b.Navigation("Treatment");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.TreatmentProcedure", b =>
{
b.HasOne("HospitalDatabaseImplement.Models.Procedure", "Procedure")
.WithMany("Treatments")
.HasForeignKey("ProcedureId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("HospitalDatabaseImplement.Models.Treatment", "Treatment")
.WithMany("Procedures")
.HasForeignKey("TreatmentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Procedure");
b.Navigation("Treatment");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Apothecary", b =>
{
b.Navigation("Medicines");
b.Navigation("Prescriptions");
b.Navigation("Recipes");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Medicine", b =>
{
b.Navigation("Prescriptions");
b.Navigation("Procedures");
b.Navigation("Recipes");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Procedure", b =>
{
b.Navigation("Medicines");
b.Navigation("Treatments");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Recipe", b =>
{
b.Navigation("Medicines");
b.Navigation("Treatments");
});
modelBuilder.Entity("HospitalDatabaseImplement.Models.Treatment", b =>
{
b.Navigation("Patients");
b.Navigation("Procedures");
b.Navigation("Recipes");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,60 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace HospitalDatabaseImplement.Migrations
{
/// <inheritdoc />
public partial class FixPatient : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Patients_Treatments_TreatmentId",
table: "Patients");
migrationBuilder.AlterColumn<int>(
name: "TreatmentId",
table: "Patients",
type: "int",
nullable: true,
oldClrType: typeof(int),
oldType: "int");
migrationBuilder.AddForeignKey(
name: "FK_Patients_Treatments_TreatmentId",
table: "Patients",
column: "TreatmentId",
principalTable: "Treatments",
principalColumn: "Id",
onDelete: ReferentialAction.SetNull);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Patients_Treatments_TreatmentId",
table: "Patients");
migrationBuilder.AlterColumn<int>(
name: "TreatmentId",
table: "Patients",
type: "int",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "int",
oldNullable: true);
migrationBuilder.AddForeignKey(
name: "FK_Patients_Treatments_TreatmentId",
table: "Patients",
column: "TreatmentId",
principalTable: "Treatments",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
}
}

View File

@ -98,7 +98,7 @@ namespace HospitalDatabaseImplement.Migrations
.IsRequired()
.HasColumnType("nvarchar(max)");
b.Property<int>("TreatmentId")
b.Property<int?>("TreatmentId")
.HasColumnType("int");
b.HasKey("Id");
@ -307,8 +307,7 @@ namespace HospitalDatabaseImplement.Migrations
b.HasOne("HospitalDatabaseImplement.Models.Treatment", "Treatment")
.WithMany("Patients")
.HasForeignKey("TreatmentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.OnDelete(DeleteBehavior.SetNull);
b.Navigation("Treatment");
});

View File

@ -1,12 +1,7 @@
using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels;
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace HospitalDatabaseImplement.Models
@ -23,8 +18,8 @@ namespace HospitalDatabaseImplement.Models
[Required]
public DateTime BirthDate { get; private set; }
public int TreatmentId { get; private set; }
public virtual Treatment Treatment { get; set; }
public int? TreatmentId { get; private set; }
public virtual Treatment? Treatment { get; set; }
public static Patient? Create(PatientBindingModel model)
{
@ -55,7 +50,7 @@ namespace HospitalDatabaseImplement.Models
Name = element.Element("Name")!.Value,
Patronymic = element.Element("Patronymic")?.Value,
BirthDate = DateTime.ParseExact(element.Element("BirthDate")!.Value, "dd.mm.yyyy", null),
TreatmentId = Convert.ToInt32(element.Element("TreatmentId")!.Value)
TreatmentId = Convert.ToInt32(element.Element("TreatmentId")?.Value)
};
}
@ -80,7 +75,7 @@ namespace HospitalDatabaseImplement.Models
Patronymic = Patronymic,
BirthDate = BirthDate,
TreatmentId = TreatmentId,
TreatmentName = Treatment.Name
TreatmentName = Treatment?.Name
};
}
}