Rogashova_E.A._CourseWork_H.../HospitalDataBaseImplements/Implements/KurseStorage.cs

85 lines
2.6 KiB
C#

using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class KurseStorage : IKurseStorage
{
public KurseViewModel? Delete(KurseBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Kurse.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Kurse.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public KurseViewModel? GetElement(KurseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Kurse.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<KurseViewModel> GetFilteredList(KurseSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new HospitalDatabase();
return context.Kurse
.Where(x => x.MedicinesName.Contains(model.MedicinesName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<KurseViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Kurse.Select(x => x.GetViewModel).ToList();
}
public KurseViewModel? Insert(KurseBindingModel model)
{
var newKurse = Kurses.Create(model);
if (newKurse == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Kurse.Add(newKurse);
context.SaveChanges();
return newKurse.GetViewModel;
}
public KurseViewModel? Update(KurseBindingModel model)
{
using var context = new HospitalDatabase();
var kurse = context.Kurse.FirstOrDefault(x => x.Id == model.Id);
if (kurse == null)
{
return null;
}
kurse.Update(model);
context.SaveChanges();
return kurse.GetViewModel;
}
}
}