using BeautySaloonContracts.BindingModels; using BeautySaloonContracts.SearchModels; using BeautySaloonContracts.StoragesContracts; using BeautySaloonContracts.ViewModels; namespace BeautySaloonDatabaseImplement.Implements { public class PositionStorage : IPositionStorage { public PositionViewModel? Delete(PositionBindingModel model) { using var context = new NewdbContext(); var element = context.Positions .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Positions.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public PositionViewModel? GetElement(PositionSearchModel model) { using var context = new NewdbContext(); if (model.Id.HasValue) return context.Positions .FirstOrDefault(x => x.Id == model.Id)? .GetViewModel; if (!string.IsNullOrEmpty(model.Name)) return context.Positions .FirstOrDefault(x => x.Name.Equals(model.Name))? .GetViewModel; return null; } public List GetFilteredList(PositionSearchModel model) { if (string.IsNullOrEmpty(model.Name)) { return new(); } using var context = new NewdbContext(); return context.Positions .Where(x => x.Name.Contains(model.Name)) .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new NewdbContext(); return context.Positions .Select(x => x.GetViewModel) .ToList(); } public PositionViewModel? Insert(PositionBindingModel model) { var newPosition = Position.Create(model); if (newPosition == null) { return null; } using var context = new NewdbContext(); context.Positions.Add(newPosition); context.SaveChanges(); return newPosition.GetViewModel; } public PositionViewModel? Update(PositionBindingModel model) { using var context = new NewdbContext(); var element = context.Positions .FirstOrDefault(x => x.Id == model.Id); if (element == null) { return null; } element.Update(model); context.SaveChanges(); return element.GetViewModel; } } }