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

210 lines
6.4 KiB
C#
Raw Permalink Normal View History

2024-04-02 02:15:17 +04:00
using Microsoft.EntityFrameworkCore;
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 RouteStorage : IRouteStorage
{
/// <summary>
/// Получение полного списка
/// </summary>
/// <returns></returns>
public List<RouteViewModel> GetFullList()
{
using var context = new RouteGuideDatabase();
return context.Routes
.Include(x => x.Transport)
.Include(x => x.Stops)
.ThenInclude(x => x.Stop)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
/// <summary>
/// Получение фильтрованного списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<RouteViewModel> GetFilteredList(RouteSearchModel 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.Routes
.Include(x => x.Transport)
.Include(x => x.Stops)
.ThenInclude(x => x.Stop)
.ToList()
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<RouteViewModel> GetList(int count)
{
if (count <= 0)
{
return new();
}
using var context = new RouteGuideDatabase();
return context.Routes
.Include(x => x.Transport)
.Include(x => x.Stops)
.ThenInclude(x => x.Stop)
.ToList()
.Take(count)
.Select(x => x.GetViewModel)
.ToList();
}
2024-04-02 02:15:17 +04:00
/// <summary>
/// Получение элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public RouteViewModel? GetElement(RouteSearchModel model)
{
2024-04-23 19:38:25 +04:00
if (model.Id == null && string.IsNullOrEmpty(model.Name))
2024-04-02 02:15:17 +04:00
{
return null;
}
using var context = new RouteGuideDatabase();
return context.Routes
.Include(x => x.Transport)
.Include(x => x.Stops)
.ThenInclude(x => x.Stop)
2024-04-23 19:38:25 +04:00
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id != null && x.Id.Equals(model.Id)))
2024-04-02 02:15:17 +04:00
?.GetViewModel;
}
/// <summary>
/// Добавление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public RouteViewModel? Insert(RouteBindingModel model)
{
using var context = new RouteGuideDatabase();
var newRoute = Route.Create(context, model);
if (newRoute == null)
{
return null;
}
context.Routes.Add(newRoute);
context.SaveChanges();
return newRoute.GetViewModel;
}
/// <summary>
/// Редактирование элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public RouteViewModel? Update(RouteBindingModel model)
{
using var context = new RouteGuideDatabase();
using var transaction = context.Database.BeginTransaction();
try
{
2024-04-23 19:38:25 +04:00
var route = context.Routes.FirstOrDefault(x => x.Id.Equals(model.Id));
2024-04-02 02:15:17 +04:00
if (route == null)
{
return null;
}
route.Update(model);
context.SaveChanges();
route.UpdateStops(context, model);
transaction.Commit();
return route.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
/// <summary>
/// Удаление элемента
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public RouteViewModel? Delete(RouteBindingModel model)
{
using var context = new RouteGuideDatabase();
var route = context.Routes
.Include(x => x.Stops)
2024-04-23 19:38:25 +04:00
.FirstOrDefault(rec => rec.Id.Equals(model.Id));
2024-04-02 02:15:17 +04:00
if (route == null)
{
return null;
}
context.Routes.Remove(route);
context.SaveChanges();
return route.GetViewModel;
}
2024-04-09 13:48:30 +04:00
/// <summary>
/// Удаление записи
/// </summary>
/// <returns></returns>
public RouteViewModel? Delete()
{
using var context = new RouteGuideDatabase();
if (!context.Routes.Any())
{
return null;
}
var element = context.Routes.First();
context.Routes.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
/// <summary>
/// Удаление всех элементов
/// </summary>
/// <returns></returns>
public int Clear()
{
using var context = new RouteGuideDatabase();
int count = context.Routes.Count();
if (count <= 0)
{
return 0;
}
context.Routes.RemoveRange(context.Routes);
context.SaveChanges();
return count;
}
2024-04-02 02:15:17 +04:00
}
}