116 lines
3.8 KiB
C#
116 lines
3.8 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VeterinaryClinicContracts.BindingModels;
|
|
using VeterinaryClinicContracts.SearchModels;
|
|
using VeterinaryClinicContracts.StorageContracts;
|
|
using VeterinaryClinicContracts.ViewModels;
|
|
using VeterinaryClinicDatabaseImplement.Models;
|
|
|
|
namespace VeterinaryClinicDatabaseImplement.Implements
|
|
{
|
|
public class MedicineStorage : IMedicineStorage
|
|
{
|
|
public MedicineViewModel? Delete(MedicineBindingModel model)
|
|
{
|
|
using var context = new VeterinaryClinicDatabase();
|
|
var element = context.Medicines
|
|
.Include(x => x.Medications)
|
|
.Include(x => x.Orders)
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Medicines.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
|
|
}
|
|
|
|
public MedicineViewModel? GetElement(MedicineSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.MedicineName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VeterinaryClinicDatabase();
|
|
return context.Medicines
|
|
.Include(x => x.Medications)
|
|
.ThenInclude(x => x.Medication)
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.MedicineName) &&
|
|
x.MedicineName == model.MedicineName) || (model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<MedicineViewModel> GetFilteredList(MedicineSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.MedicineName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new VeterinaryClinicDatabase();
|
|
return context.Medicines
|
|
.Include(x => x.Medications)
|
|
.ThenInclude(x => x.Medication)
|
|
.Where(x => x.MedicineName.Contains(model.MedicineName))
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<MedicineViewModel> GetFullList()
|
|
{
|
|
using var context = new VeterinaryClinicDatabase();
|
|
return context.Medicines
|
|
.Include(x => x.Medications)
|
|
.ThenInclude(x => x.Medication)
|
|
.ToList()
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public MedicineViewModel? Insert(MedicineBindingModel model)
|
|
{
|
|
using var context = new VeterinaryClinicDatabase();
|
|
var newMedicine = Medicine.Create(context, model);
|
|
if (newMedicine == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Medicines.Add(newMedicine);
|
|
context.SaveChanges();
|
|
return newMedicine.GetViewModel;
|
|
}
|
|
|
|
public MedicineViewModel? Update(MedicineBindingModel model)
|
|
{
|
|
using var context = new VeterinaryClinicDatabase();
|
|
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();
|
|
Medicine.UpdateMedications(context, model);
|
|
transaction.Commit();
|
|
return Medicine.GetViewModel;
|
|
}
|
|
catch
|
|
{
|
|
transaction.Rollback();
|
|
throw;
|
|
}
|
|
}
|
|
}
|
|
}
|