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();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|