2024-04-30 20:55:37 +04:00
|
|
|
|
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();
|
2024-08-03 19:12:18 +04:00
|
|
|
|
var element = context.Works.FirstOrDefault(rec => rec.Id == model.ID);
|
2024-04-30 20:55:37 +04:00
|
|
|
|
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();
|
2024-08-03 19:12:18 +04:00
|
|
|
|
var component = context.Works.FirstOrDefault(x => x.Id == model.ID);
|
2024-04-30 20:55:37 +04:00
|
|
|
|
if (component == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
component.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return component.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|