186 lines
5.6 KiB
C#
186 lines
5.6 KiB
C#
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 ScheduleStorage : IScheduleStorage
|
|
{
|
|
/// <summary>
|
|
/// Получение полного списка
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public List<ScheduleViewModel> GetFullList()
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Schedules
|
|
.Include(x => x.Route)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение фильтрованного списка
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public List<ScheduleViewModel> GetFilteredList(ScheduleSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Schedules
|
|
.Include(x => x.Route)
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получить список из заданного количества элементов
|
|
/// </summary>
|
|
/// <param name="count"></param>
|
|
/// <returns></returns>
|
|
public List<ScheduleViewModel> GetList(int count)
|
|
{
|
|
if (count <= 0)
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Schedules
|
|
.Include(x => x.Route)
|
|
.Take(count)
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Получение элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ScheduleViewModel? GetElement(ScheduleSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new RouteGuideDatabase();
|
|
return context.Schedules
|
|
.Include(x => x.Route)
|
|
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
|
|
?.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Добавление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ScheduleViewModel? Insert(ScheduleBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var newSchedule = Schedule.Create(context, model);
|
|
if (newSchedule == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Schedules.Add(newSchedule);
|
|
context.SaveChanges();
|
|
return newSchedule.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Редактирование элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ScheduleViewModel? Update(ScheduleBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var schedule = context.Schedules.FirstOrDefault(x => x.Id == model.Id);
|
|
if (schedule == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
schedule.Update(model);
|
|
context.SaveChanges();
|
|
return schedule.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление элемента
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
public ScheduleViewModel? Delete(ScheduleBindingModel model)
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
var schedule = context.Schedules.FirstOrDefault(x => x.Id == model.Id);
|
|
if (schedule == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
context.Schedules.Remove(schedule);
|
|
context.SaveChanges();
|
|
return schedule.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление записи
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public ScheduleViewModel? Delete()
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
if (!context.Schedules.Any())
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var element = context.Schedules.First();
|
|
context.Schedules.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Удаление всех элементов
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public int Clear()
|
|
{
|
|
using var context = new RouteGuideDatabase();
|
|
int count = context.Schedules.Count();
|
|
if (count <= 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
context.Schedules.RemoveRange(context.Schedules);
|
|
context.SaveChanges();
|
|
return count;
|
|
}
|
|
}
|
|
}
|