diff --git a/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs b/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs index 19dec6b..2e05a74 100644 --- a/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs +++ b/Hospital/HospitalDataBaseImplement/Implementss/MedicineStorage.cs @@ -1,5 +1,7 @@ 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; @@ -7,12 +9,99 @@ using HospitalContracts.BindingModels; using HospitalContracts.SearchModels; using HospitalContracts.StoragesContracts; using HospitalContracts.ViewModels; +using HospitalDatabaseImplement.Models; +using HospitalDataModels.Models; +using HospitalDatabaseImplement.Modelss; namespace HospitalDatabaseImplement.Implementss { public class MedicineStorage { - + + public List GetFullList() + { + using var context = new HospitalDatabase(); + return context.Medicines + .Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(MedicineSearchModel model) + { + using var context = new HospitalDatabase(); + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return context.Medicines + .Where(x => x.Name.Contains(model.Name)).ToList() + .Select(x => x.GetViewModel).ToList(); + } + else + { + return new(); + } + } + + public MedicineViewModel? GetElement(MedicineSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new HospitalDatabase(); + return context.Medicines + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public MedicineViewModel? Insert(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + var newMedicine = Medicine.Create(model); + if (newMedicine == null) + { + return null; + } + context.Medicines.Add(newMedicine); + context.SaveChanges(); + return newMedicine.GetViewModel; + } + + public MedicineViewModel? Update(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var medicine = context.Medicines + .FirstOrDefault(rec => rec.Id == model.Id); + if (medicine == null) + { + return null; + } + medicine.Update(model); + context.SaveChanges(); + transaction.Commit(); + return medicine.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public MedicineViewModel? Delete(MedicineBindingModel model) + { + using var context = new HospitalDatabase(); + var element = context.Medicines + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Medicines.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } } -} + diff --git a/Hospital/HospitalDatabaseImplement/Migrations/20240429103652_CreateMigration.Designer.cs b/Hospital/HospitalDatabaseImplement/Migrations/20240429120033_CreateMigration.Designer.cs similarity index 99% rename from Hospital/HospitalDatabaseImplement/Migrations/20240429103652_CreateMigration.Designer.cs rename to Hospital/HospitalDatabaseImplement/Migrations/20240429120033_CreateMigration.Designer.cs index 7efd139..1a61b6e 100644 --- a/Hospital/HospitalDatabaseImplement/Migrations/20240429103652_CreateMigration.Designer.cs +++ b/Hospital/HospitalDatabaseImplement/Migrations/20240429120033_CreateMigration.Designer.cs @@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion; namespace HospitalDatabaseImplement.Migrations { [DbContext(typeof(HospitalDatabase))] - [Migration("20240429103652_CreateMigration")] + [Migration("20240429120033_CreateMigration")] partial class CreateMigration { /// diff --git a/Hospital/HospitalDatabaseImplement/Migrations/20240429103652_CreateMigration.cs b/Hospital/HospitalDatabaseImplement/Migrations/20240429120033_CreateMigration.cs similarity index 100% rename from Hospital/HospitalDatabaseImplement/Migrations/20240429103652_CreateMigration.cs rename to Hospital/HospitalDatabaseImplement/Migrations/20240429120033_CreateMigration.cs