2024-04-29 21:47:21 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using PolyclinicContracts.BindingModels;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
using PolyclinicContracts.SearchModels;
|
|
|
|
|
using PolyclinicContracts.StoragesContracts;
|
|
|
|
|
using PolyclinicContracts.ViewModels;
|
2024-04-29 21:47:21 +04:00
|
|
|
|
using PolyclinicDatabaseImplement.Models;
|
|
|
|
|
using SecuritySystemDatabaseImplement;
|
2024-04-28 20:10:45 +04:00
|
|
|
|
|
|
|
|
|
namespace PolyclinicDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
public class CourseStorage : ICourseStorage
|
|
|
|
|
{
|
2024-04-29 20:23:37 +04:00
|
|
|
|
public CourseViewModel? Delete(CourseBindingModel model)
|
|
|
|
|
{
|
2024-04-29 21:47:21 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
var element = context.Courses.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Courses.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CourseViewModel? GetElement(CourseSearchModel model)
|
|
|
|
|
{
|
2024-04-29 21:47:21 +04:00
|
|
|
|
return GetFilteredList(model).FirstOrDefault();
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CourseViewModel> GetFilteredList(CourseSearchModel model)
|
|
|
|
|
{
|
2024-04-29 21:47:21 +04:00
|
|
|
|
var elements = GetFullList();
|
|
|
|
|
foreach (var prop in model.GetType().GetProperties())
|
|
|
|
|
{
|
|
|
|
|
if (model.GetType().GetProperty(prop.Name)?.GetValue(model, null) != null)
|
|
|
|
|
{
|
|
|
|
|
elements = elements.Where(x => x.GetType().GetProperty(prop.Name)?.GetValue(x, null) == model.GetType().GetProperty(prop.Name)?.GetValue(model, null)).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return elements;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<CourseViewModel> GetFullList()
|
|
|
|
|
{
|
2024-04-29 21:47:21 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
return context.Courses
|
|
|
|
|
.Include(x => x.Diagnoses)
|
|
|
|
|
.ThenInclude(x => x.Diagnose)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CourseViewModel? Insert(CourseBindingModel model)
|
|
|
|
|
{
|
2024-04-29 21:47:21 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
var element = Course.Create(context, model);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
context.Courses.Add(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public CourseViewModel? Update(CourseBindingModel model)
|
|
|
|
|
{
|
2024-04-29 21:47:21 +04:00
|
|
|
|
using var context = new PolyclinicDatabase();
|
|
|
|
|
var element = context.Courses.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
element.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
2024-04-29 20:23:37 +04:00
|
|
|
|
}
|
2024-04-28 20:10:45 +04:00
|
|
|
|
}
|
|
|
|
|
}
|