103 lines
2.6 KiB
C#
103 lines
2.6 KiB
C#
using Contracts.BindingModel;
|
|
using Contracts.SearchModel;
|
|
using Contracts.StorageContracts;
|
|
using Contracts.ViewModel;
|
|
using DatabaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DatabaseImplement.Implements
|
|
{
|
|
public class LabWorkStorage : ILabWorkStorage
|
|
{
|
|
public List<LabWorkViewModel> GetFullList()
|
|
{
|
|
using var context = new ElegevContext();
|
|
|
|
return context.LabWorks
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<LabWorkViewModel> GetFilteredList(LabWorkSearchModel model)
|
|
{
|
|
using var context = new ElegevContext();
|
|
|
|
return context.LabWorks
|
|
.Where(x => x.Theme == model.Theme)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public LabWorkViewModel? GetElement(LabWorkSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new ElegevContext();
|
|
|
|
return context.LabWorks
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public LabWorkViewModel? Insert(LabWorkBindingModel model)
|
|
{
|
|
var newLabWork = LabWork.Create(model);
|
|
|
|
if (newLabWork == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new ElegevContext();
|
|
|
|
context.LabWorks.Add(newLabWork);
|
|
context.SaveChanges();
|
|
|
|
return newLabWork.GetViewModel;
|
|
}
|
|
|
|
public LabWorkViewModel? Update(LabWorkBindingModel model)
|
|
{
|
|
using var context = new ElegevContext();
|
|
|
|
var labWork = context.LabWorks
|
|
.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (labWork == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
labWork.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return labWork.GetViewModel;
|
|
}
|
|
|
|
public LabWorkViewModel? Delete(LabWorkBindingModel model)
|
|
{
|
|
using var context = new ElegevContext();
|
|
|
|
var element = context.LabWorks
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
if (element != null)
|
|
{
|
|
context.LabWorks.Remove(element);
|
|
context.SaveChanges();
|
|
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|