80 lines
2.6 KiB
C#
Raw Normal View History

2024-11-13 15:21:19 +04:00
using StudentPerformanceContracts.BindingModels;
using StudentPerformanceContracts.SearchModels;
using StudentPerformanceContracts.StorageContracts;
using StudentPerformanceContracts.ViewModels;
using StudentPerformanceDatabaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace StudentPerformanceDatabaseImplement.Implements
{
public class FormatStorage : IFormatStorage
{
public List<FormatViewModel> GetFullList()
{
using var context = new StudentsDatabase();
return context.Formats
.Select(x => x.GetViewModel)
.ToList();
}
public List<FormatViewModel> GetFilteredList(FormatSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new StudentsDatabase();
return context.Formats
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public FormatViewModel? GetElement(FormatSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new StudentsDatabase();
return context.Formats
.FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id)
?.GetViewModel;
}
public FormatViewModel? Insert(FormatBindingModel model)
{
var newcity = Formats.Create(model);
if (newcity == null)
{
return null;
}
using var context = new StudentsDatabase();
context.Formats.Add(newcity);
context.SaveChanges();
return newcity.GetViewModel;
}
public FormatViewModel? Update(FormatBindingModel model)
{
using var context = new StudentsDatabase();
var component = context.Formats.FirstOrDefault(x => x.Id == model.Id);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
public FormatViewModel? Delete(FormatBindingModel model)
{
using var context = new StudentsDatabase();
var element = context.Formats.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Formats.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}