From 7e259e227c6c63eeecffb0a087dee83a68d1020a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9A=D0=B0=D1=82=D1=8F=20=D0=98=D1=85=D0=BE=D0=BD=D0=BA?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0?= Date: Sun, 19 Mar 2023 20:50:59 +0400 Subject: [PATCH] CRUD --- .../Implements/ExcursionStorage.cs | 89 +++++++++++++++++++ .../Implements/PlaceStorage.cs | 89 +++++++++++++++++++ .../Implements/TouristStorage.cs | 63 +++++++++++++ .../Implements/TravelStorage.cs | 85 ++++++++++++++++++ .../Models/ExcursionTravel.cs | 2 +- .../Models/Travel.cs | 2 +- .../TravelCompanyDatabase.cs | 27 ++++++ .../TravelCompanyDatabaseImplement.csproj | 9 ++ 8 files changed, 364 insertions(+), 2 deletions(-) create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/ExcursionStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/PlaceStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/TouristStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs create mode 100644 TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/ExcursionStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ExcursionStorage.cs new file mode 100644 index 0000000..4ca9dbd --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/ExcursionStorage.cs @@ -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 GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Excursions + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/PlaceStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/PlaceStorage.cs new file mode 100644 index 0000000..3e09535 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/PlaceStorage.cs @@ -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 GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Places + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/TouristStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TouristStorage.cs new file mode 100644 index 0000000..4b6ab94 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TouristStorage.cs @@ -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 GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Tourists + .Select(x => x.GetViewModel) + .ToList(); + } + public List 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; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs new file mode 100644 index 0000000..ad8af8f --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/Implements/TravelStorage.cs @@ -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 GetFullList() + { + using var context = new TravelCompanyDatabase(); + return context.Travels.Select(x => x.GetViewModel).ToList(); + } + public List 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; + } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/ExcursionTravel.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/ExcursionTravel.cs index 8a40032..e3fa222 100644 --- a/TravelCompany/TravelCompanyDatabaseImplement/Models/ExcursionTravel.cs +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/ExcursionTravel.cs @@ -6,7 +6,7 @@ using System.Text; using System.Threading.Tasks; using TravelCompanyDatabaseImplement.Models; -namespace PrecastConcretePlantDatabaseImplement.Models +namespace TravelCompanyDatabaseImplement.Models { public class ExcursionTravel { diff --git a/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs b/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs index 165bfeb..02c6856 100644 --- a/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs +++ b/TravelCompany/TravelCompanyDatabaseImplement/Models/Travel.cs @@ -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 { diff --git a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs new file mode 100644 index 0000000..c1b5501 --- /dev/null +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabase.cs @@ -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 Excursions { set; get; } + public virtual DbSet Places { set; get; } + public virtual DbSet Travels { set; get; } + public virtual DbSet Tourists { set; get; } + public virtual DbSet ExcursionTravels { set; get; } + } +} diff --git a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj index bb126aa..61763a0 100644 --- a/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj +++ b/TravelCompany/TravelCompanyDatabaseImplement/TravelCompanyDatabaseImplement.csproj @@ -6,6 +6,15 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + +