92 lines
2.4 KiB
C#
92 lines
2.4 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 SubjectStorage : ISubjectStorage
|
|
{
|
|
public List<SubjectViewModel> GetFullList()
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
return context.Subjects
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public List<SubjectViewModel> GetFilteredList(SubjectSearchModel
|
|
model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.SubjectName))
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new SchoolScheduleDataBase();
|
|
return context.Subjects
|
|
.Where(x => x.SubjectName.Contains(model.SubjectName))
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
public SubjectViewModel? GetElement(SubjectSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.SubjectName) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new SchoolScheduleDataBase();
|
|
return context.Subjects
|
|
.FirstOrDefault(x =>
|
|
(!string.IsNullOrEmpty(model.SubjectName) && x.SubjectName ==
|
|
model.SubjectName) ||
|
|
(model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
public SubjectViewModel? Insert(SubjectBindingModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
var newComponent = Subject.Create(context, model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Subjects.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
public SubjectViewModel? Update(SubjectBindingModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
var component = context.Subjects.FirstOrDefault(x => x.Id ==
|
|
model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(context, model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
public SubjectViewModel? Delete(SubjectBindingModel model)
|
|
{
|
|
using var context = new SchoolScheduleDataBase();
|
|
var element = context.Subjects.FirstOrDefault(rec => rec.Id ==
|
|
model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Subjects.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
}
|