using TransportCompanyContracts.BindingModels; using TransportCompanyContracts.SearchModels; using TransportCompanyContracts.StoragesContracts; using TransportCompanyContracts.ViewModels; using TransportCompanyDatabaseImplement.Models; namespace TransportCompanyDatabaseImplement.Implements { public class PointStorage : IPointStorage { public List GetFullList() { using var context = new TransportCompanyDatabase(); return context.Points.Select(x => x.GetViewModel).ToList(); } public List GetFilteredList(PointSearchModel model) { if (string.IsNullOrEmpty(model.PointName)) { return new(); } using var context = new TransportCompanyDatabase(); return context.Points.Where(x => x.PointName.Contains(model.PointName)).Select(x => x.GetViewModel).ToList(); } public PointViewModel? GetElement(PointSearchModel model) { if (string.IsNullOrEmpty(model.PointName) && !model.Id.HasValue) { return null; } using var context = new TransportCompanyDatabase(); return context.Points.FirstOrDefault(x => (!string.IsNullOrEmpty(model.PointName) && x.PointName == model.PointName) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } public PointViewModel? Insert(PointBindingModel model) { var newComponent = Point.Create(model); if (newComponent == null) { return null; } using var context = new TransportCompanyDatabase(); context.Points.Add(newComponent); context.SaveChanges(); return newComponent.GetViewModel; } public PointViewModel? Update(PointBindingModel model) { using var context = new TransportCompanyDatabase(); var component = context.Points.FirstOrDefault(x => x.Id == model.Id); if (component == null) { return null; } component.Update(model); context.SaveChanges(); return component.GetViewModel; } public PointViewModel? Delete(PointBindingModel model) { using var context = new TransportCompanyDatabase(); var element = context.Points.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Points.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }