LabSubd/Subd/DataBase/Implements/VoyageStorage.cs

82 lines
2.4 KiB
C#

using Contracts.BindingModels;
using Contracts.SearchModel;
using Contracts.Storage;
using Contracts.ViewModels;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase.Implements
{
public class VoyageStorage : IVoyageStorage
{
public List<VoyageVM> GetFullList()
{
using var context = new LogisticContext();
return context.Voyages.FromSqlRaw("Select * From Voyage")
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<VoyageVM> GetFilteredList(VoyageSM model)
{
if (!model.DateStart.HasValue || !model.DateEnd.HasValue)
{
return new();
}
using var context = new LogisticContext();
return context.Voyages
.FromSqlRaw("Select * FROM Voyage WHERE DateStart > {0} AND DateEnd < {1}", model.DateStart,model.DateEnd)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public VoyageVM? GetElement(VoyageSM model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new LogisticContext();
return context.Voyages
.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public VoyageVM? Insert(VoyageBM model)
{
using var context = new LogisticContext();
var newVoyage = Voyage.Create(context, model);
if (newVoyage == null)
{
return null;
}
context.Voyages.Add(newVoyage);
context.SaveChanges();
return newVoyage.GetViewModel;
}
public VoyageVM? Delete(VoyageBM model)
{
using var context = new LogisticContext();
var element = context.Voyages
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Voyages.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}