Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceStationsDataBaseImplement/Implements/ClientWorkStorage.cs

83 lines
2.7 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 ClientWorkStorage : IClientWorkStorage
{
public ClientWorkViewModel? Delete(ClientWorkBindingModel model)
{
using var context = new Database();
var element = context.ClientsWorks.FirstOrDefault(rec => rec.ClientId == model.ClientId);
if (element != null)
{
context.ClientsWorks.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ClientWorkViewModel? GetElement(ClientWorkSearchModel model)
{
if (!model.ClientId.HasValue)
{
return null;
}
using var context = new Database();
return context.ClientsWorks.FirstOrDefault(x =>
(model.ClientId.HasValue && x.ClientId == model.ClientId))?.GetViewModel;
}
public List<ClientWorkViewModel> GetFilteredList(ClientWorkSearchModel model)
{
if (!model.ClientId.HasValue)
{
return new();
}
using var context = new Database();
return context.ClientsWorks.Where(x => x.ClientId == model.ClientId).Select(x => x.GetViewModel).ToList();
}
public List<ClientWorkViewModel> GetFullList()
{
using var context = new Database();
return context.ClientsWorks.Select(x => x.GetViewModel).ToList();
}
public ClientWorkViewModel? Insert(ClientWorkBindingModel model)
{
var newComponent = ClientWork.Create(model);
if (newComponent == null)
{
return null;
}
using var context = new Database();
context.ClientsWorks.Add(newComponent);
context.SaveChanges();
return newComponent.GetViewModel;
}
public ClientWorkViewModel? Update(ClientWorkBindingModel model)
{
using var context = new Database();
var component = context.ClientsWorks.FirstOrDefault(x => x.ClientId == model.ClientId);
if (component == null)
{
return null;
}
component.Update(model);
context.SaveChanges();
return component.GetViewModel;
}
}
}