работает ворд и эксель

This commit is contained in:
Екатерина Рогашова 2023-06-20 18:43:55 +04:00
parent d6d13df913
commit c07422e040
17 changed files with 266 additions and 131 deletions

View File

@ -38,9 +38,9 @@ namespace HospitalBusinessLogic.BusinessLogics
} }
var kurses = _kurseStorage.GetFullList(); var kurses = _kurseStorage.GetFullList();
List<MedicinesViewModel> medicines = new List<MedicinesViewModel>(); List<MedicinesViewModel> medicines = new List<MedicinesViewModel>();
foreach (var memId in Ids) foreach (var mId in Ids)
{ {
var res = _medicineStorage.GetElement(new MedicinesSearchModel { Id = memId }); var res = _medicineStorage.GetElement(new MedicinesSearchModel { Id = mId });
if (res != null) if (res != null)
{ {
medicines.Add(res); medicines.Add(res);
@ -56,7 +56,7 @@ namespace HospitalBusinessLogic.BusinessLogics
}; };
foreach (var kurse in kurses) foreach (var kurse in kurses)
{ {
if (kurse.KurseMedicine.ContainsKey(medicine.Id)) if (kurse.KurseMedicines.ContainsKey(medicine.Id))
{ {
record.Kurses.Add(new Tuple<int, string>(kurse.Id, kurse.Duration)); record.Kurses.Add(new Tuple<int, string>(kurse.Id, kurse.Duration));
} }
@ -111,7 +111,7 @@ namespace HospitalBusinessLogic.BusinessLogics
// return listAll; // return listAll;
//} //}
public List<ReportRecipeMedicineViewModel> GetRecipeMedicine(ReportBindingModel model) public List<ReportMedicinesViewModel> GetRecipeMedicine(ReportBindingModel model)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
@ -131,7 +131,7 @@ namespace HospitalBusinessLogic.BusinessLogics
_saveToWord.CreateDoc(new WordInfo _saveToWord.CreateDoc(new WordInfo
{ {
FileName = model.FileName, FileName = model.FileName,
Title = "Список конференций", Title = "Список курсов",
KurseMedicines = GetKurseMedicines(model.Ids) KurseMedicines = GetKurseMedicines(model.Ids)
}); });
} }

View File

@ -49,17 +49,15 @@ namespace HospitalBusinessLogic.OfficePackage
Text = kurse.Item1.ToString(), Text = kurse.Item1.ToString(),
StyleInfo = ExcelStyleInfoType.TextWithBroder StyleInfo = ExcelStyleInfoType.TextWithBroder
}); });
rowIndex++;
InsertCellInWorksheet(new ExcelCellParameters InsertCellInWorksheet(new ExcelCellParameters
{ {
ColumnName = "B", ColumnName = "C",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = kurse.Item2, Text = kurse.Item2,
StyleInfo = ExcelStyleInfoType.TextWithBroder StyleInfo = ExcelStyleInfoType.TextWithBroder
}); });
rowIndex++; rowIndex++;
} }
rowIndex++; rowIndex++;
} }
SaveExcel(info); SaveExcel(info);

View File

@ -48,8 +48,9 @@ namespace HospitalBusinessLogic.OfficePackage
} }
}); });
} }
SaveWord(info);
} }
SaveWord(info);
} }
/// <summary> /// <summary>
/// Создание doc-файла /// Создание doc-файла

View File

@ -13,6 +13,6 @@ namespace HospitalBusinessLogic.OfficePackage.HelperModels
public string Title { get; set; } = string.Empty; public string Title { get; set; } = string.Empty;
public DateTime DateFrom { get; set; } public DateTime DateFrom { get; set; }
public DateTime DateTo { get; set; } public DateTime DateTo { get; set; }
public List<ReportRecipeMedicineViewModel> RecipeMedicine { get; set; } = new(); public List<ReportMedicinesViewModel> RecipeMedicine { get; set; } = new();
} }
} }

View File

@ -1,4 +1,6 @@
using HospitalBusinessLogic.BusinessLogics; using HospitalBusinessLogic.BusinessLogics;
using HospitalBusinessLogic.OfficePackage;
using HospitalBusinessLogic.OfficePackage.Implements;
using HospitalContracts.BusinessLogicsContracts; using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.StoragesContracts; using HospitalContracts.StoragesContracts;
using HospitalDataBaseImplements; using HospitalDataBaseImplements;
@ -27,6 +29,11 @@ builder.Services.AddTransient<IMedicinesLogic, MedicinesLogic>();
builder.Services.AddTransient<IProceduresLogic, ProceduresLogic>(); builder.Services.AddTransient<IProceduresLogic, ProceduresLogic>();
builder.Services.AddTransient<IKurseLogic, KurseLogic>(); builder.Services.AddTransient<IKurseLogic, KurseLogic>();
builder.Services.AddTransient<IRecipesLogic, RecipesLogic>(); builder.Services.AddTransient<IRecipesLogic, RecipesLogic>();
builder.Services.AddTransient<IReportLogic, ReportLogic>();
builder.Services.AddTransient<AbstractSaveToExcel, SaveToExcel>();
builder.Services.AddTransient<AbstractSaveToWord, SaveToWord>();
builder.Services.AddTransient<AbstractSaveToPdf, SaveToPdf>();
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
@ -44,6 +51,7 @@ builder.Services.AddSwaggerGen(c =>
var app = builder.Build(); var app = builder.Build();
LoaderFromXML.LoadSymptoms(); LoaderFromXML.LoadSymptoms();
LoaderFromXML.LoadKurses();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) if (app.Environment.IsDevelopment())

View File

@ -12,7 +12,7 @@ namespace HospitalContracts.BindingModels
public int Id { get; set; } public int Id { get; set; }
public string Duration { get; set; } = string.Empty; public string Duration { get; set; } = string.Empty;
public int CountInDay { get; set; } public int CountInDay { get; set; }
public Dictionary<int, IMedicinesModel> KurseMedicine public Dictionary<int, IMedicinesModel> KurseMedicines
{ {
get; get;
set; set;

View File

@ -20,7 +20,7 @@ namespace HospitalContracts.BusinessLogicsContracts
/// </summary> /// </summary>
/// <param name="model"></param> /// <param name="model"></param>
/// <returns></returns> /// <returns></returns>
List<ReportRecipeMedicineViewModel> GetRecipeMedicine(ReportBindingModel model); List<ReportMedicinesViewModel> GetRecipeMedicine(ReportBindingModel model);
/// <summary> /// <summary>
/// Сохранение компонент в файл-Word /// Сохранение компонент в файл-Word
/// </summary> /// </summary>

View File

@ -16,7 +16,7 @@ namespace HospitalContracts.ViewModels
public string Duration { get; set; } = string.Empty; public string Duration { get; set; } = string.Empty;
[DisplayName("Срок приема")] [DisplayName("Срок приема")]
public int CountInDay { get; set; } public int CountInDay { get; set; }
public Dictionary<int, IMedicinesModel> KurseMedicine public Dictionary<int, IMedicinesModel> KurseMedicines
{ {
get; get;
set; set;
@ -24,9 +24,9 @@ namespace HospitalContracts.ViewModels
public KurseViewModel() { } public KurseViewModel() { }
[JsonConstructor] [JsonConstructor]
public KurseViewModel(Dictionary<int, MedicinesViewModel> KurseMedicine) public KurseViewModel(Dictionary<int, MedicinesViewModel> KurseMedicines)
{ {
this.KurseMedicine = KurseMedicine.ToDictionary(x => x.Key, x => x.Value as IMedicinesModel); this.KurseMedicines = KurseMedicines.ToDictionary(x => x.Key, x => x.Value as IMedicinesModel);
} }
} }
} }

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace HospitalContracts.ViewModels namespace HospitalContracts.ViewModels
{ {
public class ReportRecipeMedicineViewModel public class ReportMedicinesViewModel
{ {
public int Id { get; set; } public int Id { get; set; }
public DateTime DateCreate { get; set; } public DateTime DateCreate { get; set; }

View File

@ -60,12 +60,13 @@ namespace HospitalDataBaseImplements.Implements
public KurseViewModel? Insert(KurseBindingModel model) public KurseViewModel? Insert(KurseBindingModel model)
{ {
var newKurse = Kurses.Create(model); using var context = new HospitalDatabase();
var newKurse = Kurses.Create(context, model);
if (newKurse == null) if (newKurse == null)
{ {
return null; return null;
} }
using var context = new HospitalDatabase();
context.Kurse.Add(newKurse); context.Kurse.Add(newKurse);
context.SaveChanges(); context.SaveChanges();
return context.Kurse return context.Kurse
@ -77,6 +78,9 @@ namespace HospitalDataBaseImplements.Implements
public KurseViewModel? Update(KurseBindingModel model) public KurseViewModel? Update(KurseBindingModel model)
{ {
using var context = new HospitalDatabase(); using var context = new HospitalDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var kurse = context.Kurse.FirstOrDefault(x => x.Id == model.Id); var kurse = context.Kurse.FirstOrDefault(x => x.Id == model.Id);
if (kurse == null) if (kurse == null)
{ {
@ -84,7 +88,18 @@ namespace HospitalDataBaseImplements.Implements
} }
kurse.Update(model); kurse.Update(model);
context.SaveChanges(); context.SaveChanges();
if (model.KurseMedicines != null)
{
kurse.UpdateMedicines(context, model);
}
transaction.Commit();
return kurse.GetViewModel; return kurse.GetViewModel;
} }
catch
{
transaction.Rollback();
throw;
}
}
} }
} }

View File

@ -12,7 +12,7 @@ namespace HospitalDataBaseImplements
{ {
// private static readonly string IllnessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Illness.xml"); // private static readonly string IllnessFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Illness.xml");
private static readonly string SymptomsFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Symptoms.xml"); private static readonly string SymptomsFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Symptoms.xml");
// private static readonly string KursesFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Kurses.xml"); private static readonly string KursesFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLData\\Kurses.xml");
private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction) private static List<T>? LoadData<T>(string filename, string xmlNodeName, Func<XElement, T> selectFunction)
{ {
@ -40,23 +40,23 @@ namespace HospitalDataBaseImplements
//} //}
/// <summary> /// <summary>
/// Чтение лечений из XML-файла /// Чтение курсов из XML-файла
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
//public static void LoadKurses() // (запуск после загрузки симптомов) public static void LoadKurses() // (запуск после загрузки симптомов)
//{ {
// using var context = new HospitalDatabase(); using var context = new HospitalDatabase();
// if (context.Kurse.ToList().Count > 0) if (context.Kurse.ToList().Count > 0)
// return; return;
// //var list = LoadData(KursesFileName, "Kurses", x => Kurses.Create(context, x)!)!; var list = LoadData(KursesFileName, "Kurses", x => Kurses.Create(context, x)!)!;
// //list.ForEach(x => list.ForEach(x =>
// //{ {
// // context.Kurse.Add(x); context.Kurse.Add(x);
// //}); });
// //context.SaveChanges(); context.SaveChanges();
//} }
/// <summary> /// <summary>
/// Чтение поцедур из XML-файла /// Чтение симптомов из XML-файла
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static void LoadSymptoms()// (запуск после старта программы) public static void LoadSymptoms()// (запуск после старта программы)

View File

@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace HospitalDataBaseImplements.Migrations namespace HospitalDataBaseImplements.Migrations
{ {
[DbContext(typeof(HospitalDatabase))] [DbContext(typeof(HospitalDatabase))]
[Migration("20230525103233_InitialCreate")] [Migration("20230620143135_InitialCreate")]
partial class InitialCreate partial class InitialCreate
{ {
/// <inheritdoc /> /// <inheritdoc />
@ -117,6 +117,29 @@ namespace HospitalDataBaseImplements.Migrations
b.ToTable("IllnessSymptomses"); b.ToTable("IllnessSymptomses");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.KurseMedicines", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("KurseId")
.HasColumnType("int");
b.Property<int>("MedicineId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("KurseId");
b.HasIndex("MedicineId");
b.ToTable("KurseMedicines");
});
modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -132,17 +155,8 @@ namespace HospitalDataBaseImplements.Migrations
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<int>("MedicinesId")
.HasColumnType("int");
b.Property<string>("MedicinesName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("MedicinesId");
b.ToTable("Kurse"); b.ToTable("Kurse");
}); });
@ -336,15 +350,23 @@ namespace HospitalDataBaseImplements.Migrations
b.Navigation("Symptoms"); b.Navigation("Symptoms");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.KurseMedicines", b =>
{ {
b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicines") b.HasOne("HospitalDataBaseImplements.Models.Kurses", "Kurse")
.WithMany() .WithMany("Medicines")
.HasForeignKey("MedicinesId") .HasForeignKey("KurseId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.Navigation("Medicines"); b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicine")
.WithMany("KurseMedicines")
.HasForeignKey("MedicineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Kurse");
b.Navigation("Medicine");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b =>
@ -436,10 +458,14 @@ namespace HospitalDataBaseImplements.Migrations
modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b =>
{ {
b.Navigation("IllnessKurses"); b.Navigation("IllnessKurses");
b.Navigation("Medicines");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b =>
{ {
b.Navigation("KurseMedicines");
b.Navigation("Procedures"); b.Navigation("Procedures");
}); });

View File

@ -40,6 +40,20 @@ namespace HospitalDataBaseImplements.Migrations
table.PrimaryKey("PK_Illnesses", x => x.Id); table.PrimaryKey("PK_Illnesses", x => x.Id);
}); });
migrationBuilder.CreateTable(
name: "Kurse",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
Duration = table.Column<string>(type: "nvarchar(max)", nullable: false),
CountInDay = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Kurse", x => x.Id);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Symptomses", name: "Symptomses",
columns: table => new columns: table => new
@ -96,6 +110,32 @@ namespace HospitalDataBaseImplements.Migrations
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable(
name: "IllnessKurse",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IllnessId = table.Column<int>(type: "int", nullable: false),
KurseId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IllnessKurse", x => x.Id);
table.ForeignKey(
name: "FK_IllnessKurse_Illnesses_IllnessId",
column: x => x.IllnessId,
principalTable: "Illnesses",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_IllnessKurse_Kurse_KurseId",
column: x => x.KurseId,
principalTable: "Kurse",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "IllnessSymptomses", name: "IllnessSymptomses",
columns: table => new columns: table => new
@ -152,22 +192,26 @@ namespace HospitalDataBaseImplements.Migrations
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "Kurse", name: "KurseMedicines",
columns: table => new columns: table => new
{ {
Id = table.Column<int>(type: "int", nullable: false) Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"), .Annotation("SqlServer:Identity", "1, 1"),
MedicinesId = table.Column<int>(type: "int", nullable: false), MedicineId = table.Column<int>(type: "int", nullable: false),
MedicinesName = table.Column<string>(type: "nvarchar(max)", nullable: false), KurseId = table.Column<int>(type: "int", nullable: false)
Duration = table.Column<string>(type: "nvarchar(max)", nullable: false),
CountInDay = table.Column<int>(type: "int", nullable: false)
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("PK_Kurse", x => x.Id); table.PrimaryKey("PK_KurseMedicines", x => x.Id);
table.ForeignKey( table.ForeignKey(
name: "FK_Kurse_Medicines_MedicinesId", name: "FK_KurseMedicines_Kurse_KurseId",
column: x => x.MedicinesId, column: x => x.KurseId,
principalTable: "Kurse",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_KurseMedicines_Medicines_MedicineId",
column: x => x.MedicineId,
principalTable: "Medicines", principalTable: "Medicines",
principalColumn: "Id", principalColumn: "Id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
@ -225,32 +269,6 @@ namespace HospitalDataBaseImplements.Migrations
onDelete: ReferentialAction.Restrict); onDelete: ReferentialAction.Restrict);
}); });
migrationBuilder.CreateTable(
name: "IllnessKurse",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:Identity", "1, 1"),
IllnessId = table.Column<int>(type: "int", nullable: false),
KurseId = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_IllnessKurse", x => x.Id);
table.ForeignKey(
name: "FK_IllnessKurse_Illnesses_IllnessId",
column: x => x.IllnessId,
principalTable: "Illnesses",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_IllnessKurse_Kurse_KurseId",
column: x => x.KurseId,
principalTable: "Kurse",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_IllnessKurse_IllnessId", name: "IX_IllnessKurse_IllnessId",
table: "IllnessKurse", table: "IllnessKurse",
@ -272,9 +290,14 @@ namespace HospitalDataBaseImplements.Migrations
column: "SymptomsId"); column: "SymptomsId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Kurse_MedicinesId", name: "IX_KurseMedicines_KurseId",
table: "Kurse", table: "KurseMedicines",
column: "MedicinesId"); column: "KurseId");
migrationBuilder.CreateIndex(
name: "IX_KurseMedicines_MedicineId",
table: "KurseMedicines",
column: "MedicineId");
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "IX_Medicines_ClientId", name: "IX_Medicines_ClientId",
@ -326,17 +349,23 @@ namespace HospitalDataBaseImplements.Migrations
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "IllnessSymptomses"); name: "IllnessSymptomses");
migrationBuilder.DropTable(
name: "KurseMedicines");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "ProcedureMedicine"); name: "ProcedureMedicine");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "RecipesProcedures"); name: "RecipesProcedures");
migrationBuilder.DropTable(
name: "Illnesses");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Kurse"); name: "Kurse");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Illnesses"); name: "Medicines");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Procedures"); name: "Procedures");
@ -345,13 +374,10 @@ namespace HospitalDataBaseImplements.Migrations
name: "Recipes"); name: "Recipes");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Medicines"); name: "Clients");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "Symptomses"); name: "Symptomses");
migrationBuilder.DropTable(
name: "Clients");
} }
} }
} }

View File

@ -114,6 +114,29 @@ namespace HospitalDataBaseImplements.Migrations
b.ToTable("IllnessSymptomses"); b.ToTable("IllnessSymptomses");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.KurseMedicines", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
b.Property<int>("KurseId")
.HasColumnType("int");
b.Property<int>("MedicineId")
.HasColumnType("int");
b.HasKey("Id");
b.HasIndex("KurseId");
b.HasIndex("MedicineId");
b.ToTable("KurseMedicines");
});
modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
@ -129,17 +152,8 @@ namespace HospitalDataBaseImplements.Migrations
.IsRequired() .IsRequired()
.HasColumnType("nvarchar(max)"); .HasColumnType("nvarchar(max)");
b.Property<int>("MedicinesId")
.HasColumnType("int");
b.Property<string>("MedicinesName")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.HasKey("Id"); b.HasKey("Id");
b.HasIndex("MedicinesId");
b.ToTable("Kurse"); b.ToTable("Kurse");
}); });
@ -333,15 +347,23 @@ namespace HospitalDataBaseImplements.Migrations
b.Navigation("Symptoms"); b.Navigation("Symptoms");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.KurseMedicines", b =>
{ {
b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicines") b.HasOne("HospitalDataBaseImplements.Models.Kurses", "Kurse")
.WithMany() .WithMany("Medicines")
.HasForeignKey("MedicinesId") .HasForeignKey("KurseId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired(); .IsRequired();
b.Navigation("Medicines"); b.HasOne("HospitalDataBaseImplements.Models.Medicines", "Medicine")
.WithMany("KurseMedicines")
.HasForeignKey("MedicineId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("Kurse");
b.Navigation("Medicine");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b =>
@ -433,10 +455,14 @@ namespace HospitalDataBaseImplements.Migrations
modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Kurses", b =>
{ {
b.Navigation("IllnessKurses"); b.Navigation("IllnessKurses");
b.Navigation("Medicines");
}); });
modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b => modelBuilder.Entity("HospitalDataBaseImplements.Models.Medicines", b =>
{ {
b.Navigation("KurseMedicines");
b.Navigation("Procedures"); b.Navigation("Procedures");
}); });

View File

@ -8,6 +8,7 @@ using System.Threading.Tasks;
using HospitalDataModels.Models; using HospitalDataModels.Models;
using HospitalContracts.BindingModels; using HospitalContracts.BindingModels;
using HospitalContracts.ViewModels; using HospitalContracts.ViewModels;
using System.Xml.Linq;
namespace HospitalDataBaseImplements.Models namespace HospitalDataBaseImplements.Models
{ {
@ -25,41 +26,49 @@ namespace HospitalDataBaseImplements.Models
private Dictionary<int, IMedicinesModel>? _kurseMedicine = null; private Dictionary<int, IMedicinesModel>? _kurseMedicine = null;
[NotMapped] [NotMapped]
public Dictionary<int, IMedicinesModel> KurseMedicine public Dictionary<int, IMedicinesModel> KurseMedicines
{ {
get get
{ {
using var context = new HospitalDatabase();
if (_kurseMedicine == null) if (_kurseMedicine == null)
{ {
_kurseMedicine = Medicines _kurseMedicine = Medicines
.ToDictionary(rec => rec.MedicineId, rec => .ToDictionary(x => x.MedicineId, x =>
rec.Medicine as IMedicinesModel); (context.Medicines.FirstOrDefault(y => y.Id == x.MedicineId)! as IMedicinesModel));
} }
return _kurseMedicine; return _kurseMedicine;
} }
} }
[ForeignKey("KurseId")] [ForeignKey("KurseId")]
public virtual List<KurseMedicines> Medicines { get; set; } = new(); public virtual List<KurseMedicines> Medicines { get; set; } = new();
public static Kurses? Create(KurseBindingModel model) public static Kurses? Create(HospitalDatabase context, XElement element)
{ {
if (model == null) if (element == null)
{ {
return null; return null;
} }
return new Kurses() return new Kurses()
{ {
Id = model.Id, Duration = element.Element("Duration")!.Value,
Duration = model.Duration, CountInDay = Convert.ToInt32(element.Element("CountInDay")!.Value),
CountInDay = model.CountInDay Medicines = element.Element("Medicines")!.Elements("MedicineId").Select(x => new KurseMedicines
{
Medicine = context.Medicines.First(y => y.Id == Convert.ToInt32(x.Value))
}).ToList()
}; };
} }
public static Kurses Create(KurseViewModel model) public static Kurses Create(HospitalDatabase context, KurseBindingModel model)
{ {
return new Kurses return new Kurses
{ {
Id = model.Id, Id = model.Id,
Duration = model.Duration, Duration = model.Duration,
CountInDay = model.CountInDay CountInDay = model.CountInDay,
Medicines = model.KurseMedicines.Select(x => new KurseMedicines
{
Medicine = context.Medicines.First(y => y.Id == x.Key)
}).ToList()
}; };
} }
public void Update(KurseBindingModel model) public void Update(KurseBindingModel model)
@ -75,7 +84,30 @@ namespace HospitalDataBaseImplements.Models
{ {
Id = Id, Id = Id,
Duration = Duration, Duration = Duration,
CountInDay = CountInDay CountInDay = CountInDay,
KurseMedicines = KurseMedicines
}; };
public void UpdateMedicines(HospitalDatabase context, KurseBindingModel model)
{
var kurseMedicine = context.KurseMedicines.Where(rec => rec.KurseId == model.Id).ToList();
if (kurseMedicine != null && kurseMedicine.Count > 0)
{
context.KurseMedicines.RemoveRange(kurseMedicine.Where(rec
=> !model.KurseMedicines.ContainsKey(rec.MedicineId)));
context.SaveChanges();
}
var kurse = context.Kurse.First(x => x.Id == Id);
var existingMedicineIds = kurseMedicine?.Select(x => x.MedicineId).ToList();
foreach (var rec in model.KurseMedicines)
{
context.KurseMedicines.Add(new KurseMedicines
{
Kurse = kurse,
Medicine = context.Medicines.First(x => x.Id == rec.Key),
});
}
context.SaveChanges();
_kurseMedicine = null;
}
} }
} }

View File

@ -23,6 +23,9 @@ namespace HospitalDataBaseImplements.Models
public virtual Client Client { get; set; } public virtual Client Client { get; set; }
[ForeignKey("MedicineId")] [ForeignKey("MedicineId")]
public virtual List<ProcedureMedicine> Procedures { get; set; } = new(); public virtual List<ProcedureMedicine> Procedures { get; set; } = new();
[ForeignKey("MedicineId")]
public virtual List<KurseMedicines> KurseMedicines { get; set; } = new();
public static Medicines? Create(MedicinesBindingModel model) public static Medicines? Create(MedicinesBindingModel model)
{ {
if (model == null) if (model == null)

View File

@ -10,6 +10,6 @@ namespace HospitalDataModels.Models
{ {
string Duration { get; } string Duration { get; }
int CountInDay { get; } int CountInDay { get; }
Dictionary<int, IMedicinesModel> KurseMedicine { get; } Dictionary<int, IMedicinesModel> KurseMedicines { get; }
} }
} }