using RouteGuideContracts.BindingModels; using RouteGuideContracts.SearchModels; using RouteGuideContracts.StoragesContracts; using RouteGuideContracts.ViewModels; using RouteGuideDatabaseImplement.Models; using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using System.Text; using System.Threading.Tasks; namespace RouteGuideDatabaseImplement.Implements { /// /// Хранилище для сущности "Водитель" /// public class DriverStorage : IDriverStorage { /// /// Получение полного списка /// /// public List GetFullList() { using var context = new RouteGuideDatabase(); return context.Drivers .Select(x => x.GetViewModel) .ToList(); } /// /// Получение фильтрованного списка /// /// /// public List GetFilteredList(DriverSearchModel model) { if (string.IsNullOrEmpty(model.Phone)) { return new(); } using var context = new RouteGuideDatabase(); return context.Drivers .Where(x => x.Phone.Contains(model.Phone)) .Select(x => x.GetViewModel) .ToList(); } /// /// Получить список из заданного количества элементов /// /// /// public List GetList(int count) { if (count <= 0) { return new(); } using var context = new RouteGuideDatabase(); return context.Drivers .Take(count) .Select(x => x.GetViewModel) .ToList(); } /// /// Получение элемента /// /// /// public DriverViewModel? GetElement(DriverSearchModel model) { if (!model.Id.HasValue && string.IsNullOrEmpty(model.Phone)) { return null; } using var context = new RouteGuideDatabase(); return context.Drivers .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Phone) && x.Phone.Contains(model.Phone)) || (model.Id.HasValue && x.Id == model.Id)) ?.GetViewModel; } /// /// Добавление элемента /// /// /// public DriverViewModel? Insert(DriverBindingModel model) { var newDriver = Driver.Create(model); if (newDriver == null) { return null; } using var context = new RouteGuideDatabase(); context.Drivers.Add(newDriver); context.SaveChanges(); return newDriver.GetViewModel; } /// /// Редактирование элемента /// /// /// public DriverViewModel? Update(DriverBindingModel model) { using var context = new RouteGuideDatabase(); var driver = context.Drivers.FirstOrDefault(x => x.Id == model.Id); if (driver == null) { return null; } driver.Update(model); context.SaveChanges(); return driver.GetViewModel; } /// /// Удаление элемента /// /// /// public DriverViewModel? Delete(DriverBindingModel model) { using var context = new RouteGuideDatabase(); var driver = context.Drivers.FirstOrDefault(x => x.Id == model.Id); if (driver == null) { return null; } context.Drivers.Remove(driver); context.SaveChanges(); return driver.GetViewModel; } /// /// Удаление записи /// /// public DriverViewModel? Delete() { using var context = new RouteGuideDatabase(); if (!context.Drivers.Any()) { return null; } var element = context.Drivers.First(); context.Drivers.Remove(element); context.SaveChanges(); return element.GetViewModel; } /// /// Удаление всех элементов /// /// public int Clear() { using var context = new RouteGuideDatabase(); int count = context.Drivers.Count(); if (count <= 0) { return 0; } context.Drivers.RemoveRange(context.Drivers); context.SaveChanges(); return count; } } }