сторадж медицине
This commit is contained in:
parent
9a10ad410d
commit
0b87bc8287
@ -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<MedicineViewModel> GetFullList()
|
||||
{
|
||||
using var context = new HospitalDatabase();
|
||||
return context.Medicines
|
||||
.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<MedicineViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -12,7 +12,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
namespace HospitalDatabaseImplement.Migrations
|
||||
{
|
||||
[DbContext(typeof(HospitalDatabase))]
|
||||
[Migration("20240429103652_CreateMigration")]
|
||||
[Migration("20240429120033_CreateMigration")]
|
||||
partial class CreateMigration
|
||||
{
|
||||
/// <inheritdoc />
|
Loading…
Reference in New Issue
Block a user