PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideDatabaseImplement/Implements/DriverStorage.cs

182 lines
5.5 KiB
C#
Raw Permalink Normal View History

2024-04-02 02:15:17 +04:00
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.SearchModels;
using RouteGuideContracts.StoragesContracts;
using RouteGuideContracts.ViewModels;
using RouteGuideDatabaseImplement.Models;
using System;
using System.Collections.Generic;
2024-04-09 13:48:30 +04:00
using System.Diagnostics.Contracts;
2024-04-02 02:15:17 +04:00
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideDatabaseImplement.Implements
{
/// <summary>
/// Хранилище для сущности "Водитель"
/// </summary>
public class DriverStorage : IDriverStorage
{
/// <summary>
/// Получение полного списка
/// </summary>
/// <returns></returns>
public List<DriverViewModel> GetFullList()
{
using var context = new RouteGuideDatabase();
return context.Drivers
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<DriverViewModel> GetFilteredList(DriverSearchModel model)
{
2024-04-02 18:46:47 +04:00
if (string.IsNullOrEmpty(model.Phone))
2024-04-02 02:15:17 +04:00
{
return new();
}
using var context = new RouteGuideDatabase();
return context.Drivers
2024-04-02 18:46:47 +04:00
.Where(x => x.Phone.Contains(model.Phone))
2024-04-02 02:15:17 +04:00
.Select(x => x.GetViewModel)
.ToList();
}
2024-04-09 13:48:30 +04:00
/// <summary>
/// Получить список из заданного количества элементов
/// </summary>
/// <param name="count"></param>
/// <returns></returns>
public List<DriverViewModel> GetList(int count)
{
if (count <= 0)
{
return new();
}
using var context = new RouteGuideDatabase();
return context.Drivers
.Take(count)
.Select(x => x.GetViewModel)
.ToList();
}
2024-04-02 02:15:17 +04:00
/// <summary>
/// Получение элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public DriverViewModel? GetElement(DriverSearchModel model)
{
2024-04-02 18:46:47 +04:00
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Phone))
2024-04-02 02:15:17 +04:00
{
return null;
}
using var context = new RouteGuideDatabase();
return context.Drivers
2024-04-02 18:46:47 +04:00
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Phone) && x.Phone.Contains(model.Phone)) || (model.Id.HasValue && x.Id == model.Id))
2024-04-02 02:15:17 +04:00
?.GetViewModel;
}
/// <summary>
/// Добавление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
2024-04-09 13:48:30 +04:00
/// <summary>
/// Удаление записи
/// </summary>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление всех элементов
/// </summary>
/// <returns></returns>
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;
}
2024-04-02 02:15:17 +04:00
}
}