CRUD
This commit is contained in:
parent
a58c282c6a
commit
7e259e227c
@ -0,0 +1,89 @@
|
||||
using TravelCompanyContracts.BindingModels;
|
||||
using TravelCompanyContracts.SearchModels;
|
||||
using TravelCompanyContracts.StoragesContracts;
|
||||
using TravelCompanyContracts.ViewModels;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class ExcursionStorage : IExcursionStorage
|
||||
{
|
||||
public List<ExcursionViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Excursions
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<ExcursionViewModel> GetFilteredList(ExcursionSearchModel
|
||||
model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ExcursionName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Excursions
|
||||
.Where(x => x.ExcursionName.Contains(model.ExcursionName))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public ExcursionViewModel? GetElement(ExcursionSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ExcursionName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Excursions
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.ExcursionName) && x.ExcursionName ==
|
||||
model.ExcursionName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public ExcursionViewModel? Insert(ExcursionBindingModel model)
|
||||
{
|
||||
var newExcursion = Excursion.Create(model);
|
||||
if (newExcursion == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
context.Excursions.Add(newExcursion);
|
||||
context.SaveChanges();
|
||||
return newExcursion.GetViewModel;
|
||||
}
|
||||
public ExcursionViewModel? Update(ExcursionBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var component = context.Excursions.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
public ExcursionViewModel? Delete(ExcursionBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.Excursions.FirstOrDefault(rec => rec.Id ==
|
||||
model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Excursions.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,89 @@
|
||||
using TravelCompanyContracts.BindingModels;
|
||||
using TravelCompanyContracts.SearchModels;
|
||||
using TravelCompanyContracts.StoragesContracts;
|
||||
using TravelCompanyContracts.ViewModels;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class PlaceStorage : IPlaceStorage
|
||||
{
|
||||
public List<PlaceViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Places
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<PlaceViewModel> GetFilteredList(PlaceSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.PlaceName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Places
|
||||
.Where(x => x.PlaceName.Contains(model.PlaceName))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public PlaceViewModel? GetElement(PlaceSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.PlaceName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Places
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.PlaceName) && x.PlaceName ==
|
||||
model.PlaceName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public PlaceViewModel? Insert(PlaceBindingModel model)
|
||||
{
|
||||
|
||||
var newComponent = Place.Create(model);
|
||||
if (newComponent == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
context.Places.Add(newComponent);
|
||||
context.SaveChanges();
|
||||
return newComponent.GetViewModel;
|
||||
}
|
||||
public PlaceViewModel? Update(PlaceBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var component = context.Places.FirstOrDefault(x => x.Id ==
|
||||
model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
public PlaceViewModel? Delete(PlaceBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.Places.FirstOrDefault(rec => rec.Id ==
|
||||
model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Places.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
using TravelCompanyContracts.BindingModels;
|
||||
using TravelCompanyContracts.SearchModels;
|
||||
using TravelCompanyContracts.StoragesContracts;
|
||||
using TravelCompanyContracts.ViewModels;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class TouristStorage : ITouristStorage
|
||||
{
|
||||
public List<TouristViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Tourists
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<TouristViewModel> GetFilteredList(TouristSearchModel
|
||||
model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Login))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Tourists
|
||||
.Where(x => x.Login.Contains(model.Login))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public TouristViewModel? GetElement(TouristSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Tourists
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.Login) && x.Login ==
|
||||
model.Login) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public TouristViewModel? Insert(TouristBindingModel model)
|
||||
{
|
||||
var newTourist = Tourist.Create(model);
|
||||
if (newTourist == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
context.Tourists.Add(newTourist);
|
||||
context.SaveChanges();
|
||||
return newTourist.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
using TravelCompanyContracts.BindingModels;
|
||||
using TravelCompanyContracts.SearchModels;
|
||||
using TravelCompanyContracts.StoragesContracts;
|
||||
using TravelCompanyContracts.ViewModels;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements
|
||||
{
|
||||
public class TravelStorage : ITravelStorage
|
||||
{
|
||||
public List<TravelViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Travels.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
public List<TravelViewModel> GetFilteredList(TravelSearchModel
|
||||
model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.TravelName))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Travels
|
||||
.Where(x => x.TravelName.Contains(model.TravelName))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public TravelViewModel? GetElement(TravelSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.TravelName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Travels
|
||||
.FirstOrDefault(x =>
|
||||
(!string.IsNullOrEmpty(model.TravelName) && x.TravelName ==
|
||||
model.TravelName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
public TravelViewModel? Insert(TravelBindingModel model)
|
||||
{
|
||||
var newTravel = Travel.Create(model);
|
||||
if (newTravel == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
context.Travels.Add(newTravel);
|
||||
context.SaveChanges();
|
||||
return newTravel.GetViewModel;
|
||||
}
|
||||
public TravelViewModel? Update(TravelBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var component = context.Travels.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (component == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
component.Update(model);
|
||||
context.SaveChanges();
|
||||
return component.GetViewModel;
|
||||
}
|
||||
public TravelViewModel? Delete(TravelBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.Travels.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Travels.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
|
||||
namespace PrecastConcretePlantDatabaseImplement.Models
|
||||
namespace TravelCompanyDatabaseImplement.Models
|
||||
{
|
||||
public class ExcursionTravel
|
||||
{
|
||||
|
@ -8,7 +8,7 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using PrecastConcretePlantDatabaseImplement.Models;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models
|
||||
{
|
||||
|
@ -0,0 +1,27 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TravelCompanyDatabaseImplement.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement
|
||||
{
|
||||
public class TravelCompanyDatabase : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseSqlServer(@"Data Source=DESKTOP-EJNKGL2;Initial Catalog= TravelCompanyDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Excursion> Excursions { set; get; }
|
||||
public virtual DbSet<Place> Places { set; get; }
|
||||
public virtual DbSet<Travel> Travels { set; get; }
|
||||
public virtual DbSet<Tourist> Tourists { set; get; }
|
||||
public virtual DbSet<ExcursionTravel> ExcursionTravels { set; get; }
|
||||
}
|
||||
}
|
@ -6,6 +6,15 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.4" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\TravelCompanyContracts\TravelCompanyContracts.csproj" />
|
||||
</ItemGroup>
|
||||
|
Loading…
Reference in New Issue
Block a user