SUBD_SchoolSchedule/SchoolSchedule/SchoolScheduleDataBaseImplement/Implements/SchedulePlaceStorage.cs
2024-04-08 22:02:19 +04:00

87 lines
2.3 KiB
C#

using SchoolScheduleContracts.BindingModels;
using SchoolScheduleContracts.SearchModels;
using SchoolScheduleContracts.StoragesContracts;
using SchoolScheduleContracts.ViewModels;
using SchoolScheduleDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolScheduleDataBaseImplement.Implements
{
public class SchedulePlaceStorage : ISchedulePlaceStorage
{
public List<SchedulePlaceViewModel> GetFullList()
{
using var context = new SchoolScheduleDataBase();
return context.SchedulePlaces
.Select(x => x.GetViewModel)
.ToList();
}
public List<SchedulePlaceViewModel> GetFilteredList(SchedulePlaceSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new SchoolScheduleDataBase();
return context.SchedulePlaces
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public SchedulePlaceViewModel? GetElement(SchedulePlaceSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new SchoolScheduleDataBase();
return context.SchedulePlaces
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public SchedulePlaceViewModel? Insert(SchedulePlaceBindingModel model)
{
var newComponent = SchedulePlace.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new SchoolScheduleDataBase();
context.SchedulePlaces.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public SchedulePlaceViewModel? Update(SchedulePlaceBindingModel model)
{
using var context = new SchoolScheduleDataBase();
var component = context.SchedulePlaces.FirstOrDefault(x => x.Id ==
model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public SchedulePlaceViewModel? Delete(SchedulePlaceBindingModel model)
{
using var context = new SchoolScheduleDataBase();
var element = context.SchedulePlaces.FirstOrDefault(rec => rec.Id ==
model.Id);
if (element != null)
{
context.SchedulePlaces.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}