SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonDatabaseImplement/Implements/ServiceStorage.cs

87 lines
2.7 KiB
C#
Raw Normal View History

2023-03-28 21:24:52 +04:00
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonDatabaseImplement.Implements
{
public class ServiceStorage : IServiceStorage
{
public ServiceViewModel? Delete(ServiceBindingModel model)
{
using var context = new NewdbContext();
var element = context.Services
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Services.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ServiceViewModel? GetElement(ServiceSearchModel model)
{
using var context = new NewdbContext();
if (model.Id.HasValue)
return context.Services
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.Name))
return context.Services
.FirstOrDefault(x => x.Name.Equals(model.Name))?
.GetViewModel;
return null;
}
public List<ServiceViewModel> GetFilteredList(ServiceSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new NewdbContext();
return context.Services
.Where(x => x.Name.Contains(model.Name))
.Select(x => x.GetViewModel)
.ToList();
}
public List<ServiceViewModel> GetFullList()
{
using var context = new NewdbContext();
return context.Services
.Select(x => x.GetViewModel)
.ToList();
}
public ServiceViewModel? Insert(ServiceBindingModel model)
{
var newService = Service.Create(model);
if (newService == null)
{
return null;
}
using var context = new NewdbContext();
context.Services.Add(newService);
context.SaveChanges();
return newService.GetViewModel;
}
public ServiceViewModel? Update(ServiceBindingModel model)
{
using var context = new NewdbContext();
var service = context.Services
.FirstOrDefault(x => x.Id == model.Id);
if (service == null)
{
return null;
}
service.Update(model);
context.SaveChanges();
return service.GetViewModel;
}
}
}