LabSubd/Subd/DataBase/Implements/VoyageStorage.cs
2023-05-08 15:15:10 +03:00

105 lines
3.2 KiB
C#

using Contracts.BindingModels;
using Contracts.SearchModel;
using Contracts.Storage;
using Contracts.ViewModels;
using DataModels.Models;
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")
.Include(u => u.Car).Include(u => u.Company).Include(u => u.Human)
.Include(u => u.Route).ThenInclude(u => u.PlaceStartNavigation)
.Include(u => u.Route).ThenInclude(u => u.PlaceEndNavigation)
.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 date_start > {0} AND date_end < {1}", model.DateStart,model.DateEnd)
.Include(u => u.Car).Include(u => u.Company).Include(u => u.Human)
.Include(u => u.Route).ThenInclude(u => u.PlaceStartNavigation)
.Include(u => u.Route).ThenInclude(u => u.PlaceEndNavigation)
.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;
}
public List<ReportHuman> ReportHuman()
{
throw new NotImplementedException();
}
public List<ReportHuman> ReportCompany()
{
throw new NotImplementedException();
}
List<ReportCompany> IVoyageStorage.ReportCompany()
{
throw new NotImplementedException();
}
}
}