98 lines
2.2 KiB
C#
98 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using TransportGuideContracts.BindingModels;
|
|
using TransportGuideContracts.SearchModels;
|
|
using TransportGuideContracts.StoragesContracts;
|
|
using TransportGuideContracts.ViewModels;
|
|
using TransportGuideDatabaseImplements.Models;
|
|
|
|
namespace TransportGuideDatabaseImplements.Implements
|
|
{
|
|
public class StopStorage : IStopStorage
|
|
{
|
|
public StopViewModel? Delete(StopBindingModel model)
|
|
{
|
|
using var context = new TransportGuideDB();
|
|
|
|
var element = context.Stops.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
if (element != null)
|
|
{
|
|
context.Stops.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public StopViewModel? GetElement(StopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
return context.Stops.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
}
|
|
|
|
public List<StopViewModel> GetFilteredList(StopSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.Name))
|
|
{
|
|
return new();
|
|
}
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
return context.Stops.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public List<StopViewModel> GetFullList()
|
|
{
|
|
using var context = new TransportGuideDB();
|
|
|
|
return context.Stops.Select(x => x.GetViewModel).ToList();
|
|
}
|
|
|
|
public StopViewModel? Insert(StopBindingModel model)
|
|
{
|
|
var newStop = Stop.Create(model);
|
|
|
|
if (newStop == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
using var context = new TransportGuideDB();
|
|
|
|
context.Stops.Add(newStop);
|
|
context.SaveChanges();
|
|
|
|
return newStop.GetViewModel;
|
|
}
|
|
|
|
public StopViewModel? Update(StopBindingModel model)
|
|
{
|
|
using var context = new TransportGuideDB();
|
|
|
|
var Stop = context.Stops.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
if (Stop == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
Stop.Update(model);
|
|
context.SaveChanges();
|
|
|
|
return Stop.GetViewModel;
|
|
}
|
|
}
|
|
}
|