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;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RouteGuideDatabaseImplement.Implements
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранилище для сущности "Остановка"
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class StopStorage : IStopStorage
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение полного списка
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<StopViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
return context.Stops
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение фильтрованного списка
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<StopViewModel> GetFilteredList(StopSearchModel model)
|
|
|
|
|
{
|
2024-04-02 18:46:47 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.Name))
|
2024-04-02 02:15:17 +04:00
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
return context.Stops
|
2024-04-02 18:46:47 +04:00
|
|
|
|
.Where(x => x.Name.Contains(model.Name))
|
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<StopViewModel> GetList(int count)
|
|
|
|
|
{
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
return context.Stops
|
|
|
|
|
.Take(count)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 02:15:17 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public StopViewModel? GetElement(StopSearchModel model)
|
|
|
|
|
{
|
2024-04-02 18:46:47 +04:00
|
|
|
|
if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name))
|
2024-04-02 02:15:17 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
return context.Stops
|
2024-04-02 18:46:47 +04:00
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (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 StopViewModel? Insert(StopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var newStop = Stop.Create(model);
|
|
|
|
|
if (newStop == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
context.Stops.Add(newStop);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return newStop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Редактирование элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public StopViewModel? Update(StopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
var stop = context.Stops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (stop == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
stop.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return stop.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление элемента
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public StopViewModel? Delete(StopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
var stop = context.Stops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (stop == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Stops.Remove(stop);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return stop.GetViewModel;
|
|
|
|
|
}
|
2024-04-09 13:48:30 +04:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public StopViewModel? Delete()
|
|
|
|
|
{
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
if (!context.Stops.Any())
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var element = context.Stops.First();
|
|
|
|
|
context.Stops.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление всех элементов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Clear()
|
|
|
|
|
{
|
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
|
|
|
int count = context.Stops.Count();
|
|
|
|
|
if (count <= 0)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.Stops.RemoveRange(context.Stops);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
return count;
|
|
|
|
|
}
|
2024-04-02 02:15:17 +04:00
|
|
|
|
}
|
|
|
|
|
}
|