83 lines
2.5 KiB
C#
83 lines
2.5 KiB
C#
using ServiceStationContracts.BindingModels;
|
|
using ServiceStationContracts.SearchModels;
|
|
using ServiceStationContracts.ViewModels;
|
|
using ServiceStationsContracts.StorageContracts;
|
|
using ServiceStationsDataBaseImplement.Models;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace ServiceStationsDataBaseImplement.Implements
|
|
{
|
|
public class WorkStorage : IWorkStorage
|
|
{
|
|
public WorkViewModel? Delete(WorkBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var element = context.Works.FirstOrDefault(rec => rec.Id == model.ID);
|
|
if (element != null)
|
|
{
|
|
context.Works.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public WorkViewModel? GetElement(WorkSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
return context.Works.FirstOrDefault(x =>
|
|
(model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
}
|
|
|
|
public List<WorkViewModel> GetFilteredList(WorkSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new Database();
|
|
return context.Works.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<WorkViewModel> GetFullList()
|
|
{
|
|
using var context = new Database();
|
|
return context.Works.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public WorkViewModel? Insert(WorkBindingModel model)
|
|
{
|
|
var newComponent = Work.Create(model);
|
|
if (newComponent == null)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new Database();
|
|
context.Works.Add(newComponent);
|
|
context.SaveChanges();
|
|
return newComponent.GetViewModel;
|
|
}
|
|
|
|
public WorkViewModel? Update(WorkBindingModel model)
|
|
{
|
|
using var context = new Database();
|
|
var component = context.Works.FirstOrDefault(x => x.Id == model.ID);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
}
|
|
}
|