93 lines
3.1 KiB
C#
93 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.MedicationModel;
|
|
using System.Linq;
|
|
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 MedicationStorage : IMedicationStorage
|
|
{
|
|
public MedicationViewModel? Delete(MedicationBindingModel model)
|
|
{
|
|
using var context = new VeterinaryClinicDatabase();
|
|
var element = context.Medications.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Medications.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public MedicationViewModel? GetElement(MedicationSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.MedicationName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VeterinaryClinicDatabase();
|
|
return context.Medications
|
|
.FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.MedicationName) && x.MedicationName == model.MedicationName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public List<MedicationViewModel> GetFilteredList(MedicationSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.MedicationName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new VeterinaryClinicDatabase();
|
|
return context.Medications
|
|
.Where(x => x.MedicationName.Contains(model.MedicationName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<MedicationViewModel> GetFullList()
|
|
{
|
|
using var context = new VeterinaryClinicDatabase();
|
|
return context.Medications
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public MedicationViewModel? Insert(MedicationBindingModel model)
|
|
{
|
|
var newMedication = Medication.Create(model);
|
|
if (newMedication == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new VeterinaryClinicDatabase();
|
|
context.Medications.Add(newMedication);
|
|
context.SaveChanges();
|
|
return newMedication.GetViewModel;
|
|
}
|
|
|
|
public MedicationViewModel? Update(MedicationBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var Medication = context.Medications.FirstOrDefault(x => x.Id ==
|
|
model.Id);
|
|
if (Medication == null)
|
|
{
|
|
return null;
|
|
}
|
|
Medication.Update(model);
|
|
context.SaveChanges();
|
|
return Medication.GetViewModel;
|
|
}
|
|
}
|
|
}
|