87 lines
2.7 KiB
C#
87 lines
2.7 KiB
C#
using STOContracts.BindingModels;
|
|
using STOContracts.SearchModels;
|
|
using STOContracts.StorageContracts;
|
|
using STOContracts.ViewModels;
|
|
using STODatabaseImplement;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace DatabaseImplement
|
|
{
|
|
public class ServiceStorage : IServiceStorage
|
|
{
|
|
public List<ServiceViewModel> GetFullList()
|
|
{
|
|
using var context = new STODatabase();
|
|
return context.Services.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new STODatabase();
|
|
return context.Services.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public ServiceViewModel? GetElement(ServiceSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new STODatabase();
|
|
return context.Services.FirstOrDefault(x =>
|
|
(x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
public ServiceViewModel? Insert(ServiceBindingModel model)
|
|
{
|
|
using var context = new STODatabase();
|
|
if (model == null)
|
|
return null;
|
|
var newService = Service.Create(context, model);
|
|
if (newService == null)
|
|
{
|
|
return null;
|
|
}
|
|
context.Services.Add(newService);
|
|
context.SaveChanges();
|
|
return newService.GetViewModel;
|
|
}
|
|
|
|
public ServiceViewModel? Update(ServiceBindingModel model)
|
|
{
|
|
using var context = new STODatabase();
|
|
var component = context.Services.FirstOrDefault(x => x.Id == model.Id);
|
|
if (component == null)
|
|
{
|
|
return null;
|
|
}
|
|
component.Update(model);
|
|
context.SaveChanges();
|
|
return component.GetViewModel;
|
|
}
|
|
|
|
public ServiceViewModel? Delete(ServiceBindingModel model)
|
|
{
|
|
using var context = new STODatabase();
|
|
var element = context.Services.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Services.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
}
|
|
|
|
}
|