using BeautySalonContracts.BindingModels; using BeautySalonContracts.SearchModels; using BeautySalonContracts.StoragesContracts; using BeautySalonContracts.ViewModels; using BeautySalonDatabaseImplement.Models; namespace BeautySalonDatabaseImplement.Implements { public class ServiceStorage : IServiceStorage { public List GetFullList() { using var context = new BeautySalonDatabase(); return context.Services .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(ServiceSearchModel model) { if (string.IsNullOrEmpty(model.ServiceName)) { return new(); } using var context = new BeautySalonDatabase(); return context.Services .Where(x => x.ServiceName.Contains(model.ServiceName)) .Select(x => x.GetViewModel) .ToList(); } public ServiceViewModel? GetElement(ServiceSearchModel model) { if (string.IsNullOrEmpty(model.ServiceName) && !model.Id.HasValue) { return null; } using var context = new BeautySalonDatabase(); return context.Services .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ServiceName) && x.ServiceName == model.ServiceName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public ServiceViewModel? Insert(ServiceBindingModel model) { var newService = Service.Create(model); if (newService == null) { return null; } using var context = new BeautySalonDatabase(); context.Services.Add(newService); context.SaveChanges(); return newService.GetViewModel; } public ServiceViewModel? Update(ServiceBindingModel model) { using var context = new BeautySalonDatabase(); 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 BeautySalonDatabase(); var element = context.Services.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Services.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }