130 lines
4.0 KiB
C#
130 lines
4.0 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="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;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|