From 1c584ba71aa71df78fd649ed8e27f9e410e95e10 Mon Sep 17 00:00:00 2001 From: russell Date: Sun, 28 Apr 2024 23:08:12 +0400 Subject: [PATCH] DatabaseImplement --- TravelAgency/TravelAgency.sln | 10 +- .../BindingModels/TourBindingModel.cs | 2 + .../BindingModels/UserBindingModel.cs | 2 - .../SearchModels/TripSearchModel.cs | 2 - .../SearchModels/UserSearchModel.cs | 2 - .../ViewModels/TourViewModel.cs | 3 + .../ViewModels/UserViewModel.cs | 3 - .../Models/IGuideModel.cs | 2 +- .../Models/ITourModel.cs | 2 + .../Models/IUserModel.cs | 2 - .../Implements/ExcursionGroupStorage.cs | 112 ++++++++++++++++++ .../Implements/ExcursionStorage.cs | 109 +++++++++++++++++ .../Implements/GuideStorage.cs | 83 +++++++++++++ .../Implements/PlaceStorage.cs | 97 +++++++++++++++ .../Implements/TourStorage.cs | 97 +++++++++++++++ .../Implements/TripStorage.cs | 109 +++++++++++++++++ .../Implements/UserStorage.cs | 83 +++++++++++++ .../Models/Excursion.cs | 106 +++++++++++++++++ .../Models/ExcursionGroup.cs | 108 +++++++++++++++++ .../Models/ExcursionGroupTour.cs | 19 +++ .../Models/ExcursionTour.cs | 19 +++ .../Models/Guide.cs | 54 +++++++++ .../Models/Place.cs | 65 ++++++++++ .../Models/Tour.cs | 73 ++++++++++++ .../Models/Trip.cs | 106 +++++++++++++++++ .../Models/TripPlace.cs | 19 +++ .../Models/User.cs | 60 ++++++++++ .../TravelAgencyDatabase.cs | 37 ++++++ .../TravelAgencyDatabaseImplement.csproj | 23 ++++ 29 files changed, 1395 insertions(+), 14 deletions(-) create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/Excursion.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroup.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroupTour.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionTour.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/Guide.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/Place.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/Tour.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/Trip.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/TripPlace.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/Models/User.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabase.cs create mode 100644 TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabaseImplement.csproj diff --git a/TravelAgency/TravelAgency.sln b/TravelAgency/TravelAgency.sln index ba8cd38..e0b7e33 100644 --- a/TravelAgency/TravelAgency.sln +++ b/TravelAgency/TravelAgency.sln @@ -3,9 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32929.385 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyDataModels", "TravelAgencyDataModels\TravelAgencyDataModels.csproj", "{9C824913-01A0-456C-AD55-F47471596EAD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyDataModels", "TravelAgencyDataModels\TravelAgencyDataModels.csproj", "{9C824913-01A0-456C-AD55-F47471596EAD}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyContracts", "TravelAgencyContracts\TravelAgencyContracts.csproj", "{5A5C7696-4047-4BB8-BEC3-9DDB4000394E}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TravelAgencyContracts", "TravelAgencyContracts\TravelAgencyContracts.csproj", "{5A5C7696-4047-4BB8-BEC3-9DDB4000394E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TravelAgencyDatabaseImplement", "TravelAgencyDatabaseImplement\TravelAgencyDatabaseImplement.csproj", "{7F470B39-8E7F-4F32-A79F-CB77FDAEE513}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -21,6 +23,10 @@ Global {5A5C7696-4047-4BB8-BEC3-9DDB4000394E}.Debug|Any CPU.Build.0 = Debug|Any CPU {5A5C7696-4047-4BB8-BEC3-9DDB4000394E}.Release|Any CPU.ActiveCfg = Release|Any CPU {5A5C7696-4047-4BB8-BEC3-9DDB4000394E}.Release|Any CPU.Build.0 = Release|Any CPU + {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F470B39-8E7F-4F32-A79F-CB77FDAEE513}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/TravelAgency/TravelAgencyContracts/BindingModels/TourBindingModel.cs b/TravelAgency/TravelAgencyContracts/BindingModels/TourBindingModel.cs index f39b6de..6fbfd1a 100644 --- a/TravelAgency/TravelAgencyContracts/BindingModels/TourBindingModel.cs +++ b/TravelAgency/TravelAgencyContracts/BindingModels/TourBindingModel.cs @@ -12,6 +12,8 @@ namespace TravelAgencyContracts.BindingModels public double Price { get; set; } + public DateTime TourDate { get; set; } + public int UserId { get; set; } } } diff --git a/TravelAgency/TravelAgencyContracts/BindingModels/UserBindingModel.cs b/TravelAgency/TravelAgencyContracts/BindingModels/UserBindingModel.cs index e55b6f7..0f0d875 100644 --- a/TravelAgency/TravelAgencyContracts/BindingModels/UserBindingModel.cs +++ b/TravelAgency/TravelAgencyContracts/BindingModels/UserBindingModel.cs @@ -12,8 +12,6 @@ namespace TravelAgencyContracts.BindingModels public string PhoneNumber { get; set; } = string.Empty; - public string Login { get; set; } = string.Empty; - public string Password { get; set; } = string.Empty; } } diff --git a/TravelAgency/TravelAgencyContracts/SearchModels/TripSearchModel.cs b/TravelAgency/TravelAgencyContracts/SearchModels/TripSearchModel.cs index 2a3b495..4271e78 100644 --- a/TravelAgency/TravelAgencyContracts/SearchModels/TripSearchModel.cs +++ b/TravelAgency/TravelAgencyContracts/SearchModels/TripSearchModel.cs @@ -6,8 +6,6 @@ public string? TripName { get; set; } - public DateTime? DateTrip { get; set; } - public int? GuideId { get; set; } } } diff --git a/TravelAgency/TravelAgencyContracts/SearchModels/UserSearchModel.cs b/TravelAgency/TravelAgencyContracts/SearchModels/UserSearchModel.cs index acc2a8d..c280fbc 100644 --- a/TravelAgency/TravelAgencyContracts/SearchModels/UserSearchModel.cs +++ b/TravelAgency/TravelAgencyContracts/SearchModels/UserSearchModel.cs @@ -10,8 +10,6 @@ public string? PhoneNumber { get; set; } - public string? Login { get; set; } - public string? Password { get; set; } } } diff --git a/TravelAgency/TravelAgencyContracts/ViewModels/TourViewModel.cs b/TravelAgency/TravelAgencyContracts/ViewModels/TourViewModel.cs index c4d2e5c..97472fd 100644 --- a/TravelAgency/TravelAgencyContracts/ViewModels/TourViewModel.cs +++ b/TravelAgency/TravelAgencyContracts/ViewModels/TourViewModel.cs @@ -15,6 +15,9 @@ namespace TravelAgencyContracts.ViewModels [DisplayName("Цена")] public double Price { get; set; } + [DisplayName("Дата тура")] + public DateTime TourDate { get; set; } + public int UserId { get; set; } } } diff --git a/TravelAgency/TravelAgencyContracts/ViewModels/UserViewModel.cs b/TravelAgency/TravelAgencyContracts/ViewModels/UserViewModel.cs index a9c9ead..f4f5fa9 100644 --- a/TravelAgency/TravelAgencyContracts/ViewModels/UserViewModel.cs +++ b/TravelAgency/TravelAgencyContracts/ViewModels/UserViewModel.cs @@ -15,9 +15,6 @@ namespace TravelAgencyContracts.ViewModels [DisplayName("Номер телефона")] public string PhoneNumber { get; set; } = string.Empty; - [DisplayName("Логин")] - public string Login { get; set; } = string.Empty; - [DisplayName("Пароль")] public string Password { get; set; } = string.Empty; } diff --git a/TravelAgency/TravelAgencyDataModels/Models/IGuideModel.cs b/TravelAgency/TravelAgencyDataModels/Models/IGuideModel.cs index 71c74a4..5d4140f 100644 --- a/TravelAgency/TravelAgencyDataModels/Models/IGuideModel.cs +++ b/TravelAgency/TravelAgencyDataModels/Models/IGuideModel.cs @@ -6,7 +6,7 @@ using System.Threading.Tasks; namespace TravelAgencyDataModels.Models { - public interface IGuideModel :IId + public interface IGuideModel : IId { string GuideFIO { get; } diff --git a/TravelAgency/TravelAgencyDataModels/Models/ITourModel.cs b/TravelAgency/TravelAgencyDataModels/Models/ITourModel.cs index 4dc8199..66077c4 100644 --- a/TravelAgency/TravelAgencyDataModels/Models/ITourModel.cs +++ b/TravelAgency/TravelAgencyDataModels/Models/ITourModel.cs @@ -8,6 +8,8 @@ double Price { get; } + DateTime TourDate { get; } + int UserId { get; } } } diff --git a/TravelAgency/TravelAgencyDataModels/Models/IUserModel.cs b/TravelAgency/TravelAgencyDataModels/Models/IUserModel.cs index 8f800de..87c5072 100644 --- a/TravelAgency/TravelAgencyDataModels/Models/IUserModel.cs +++ b/TravelAgency/TravelAgencyDataModels/Models/IUserModel.cs @@ -8,8 +8,6 @@ string PhoneNumber { get; } - string Login { get; } - string Password { get; } } } diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs new file mode 100644 index 0000000..62f63b3 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionGroupStorage.cs @@ -0,0 +1,112 @@ +using Microsoft.EntityFrameworkCore; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class ExcursionGroupStorage : IExcursionGroupStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.ExcursionGroups + .Include(x => x.User) + .Include(x => x.Guide) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ExcursionGroupSearchModel model) + { + if (string.IsNullOrEmpty(model.ExcursionGroupName)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.ExcursionGroups + .Include(x => x.User) + .Include(x => x.Guide) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .Where(x => x.ExcursionGroupName.Contains(model.ExcursionGroupName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ExcursionGroupViewModel? GetElement(ExcursionGroupSearchModel model) + { + if (string.IsNullOrEmpty(model.ExcursionGroupName) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelAgencyDatabase(); + return context.ExcursionGroups + .Include(x => x.User) + .Include(x => x.Guide) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ExcursionGroupName) && x.ExcursionGroupName == model.ExcursionGroupName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ExcursionGroupViewModel? Insert(ExcursionGroupBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var newExcursionGroup = ExcursionGroup.Create(context, model); + if (newExcursionGroup == null) + { + return null; + } + context.ExcursionGroups.Add(newExcursionGroup); + context.SaveChanges(); + return newExcursionGroup.GetViewModel; + } + + public ExcursionGroupViewModel? Update(ExcursionGroupBindingModel model) + { + using var context = new TravelAgencyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var excursionGroup = context.ExcursionGroups.FirstOrDefault(rec => rec.Id == model.Id); + if (excursionGroup == null) + { + return null; + } + excursionGroup.Update(model); + context.SaveChanges(); + excursionGroup.UpdateTours(context, model); + transaction.Commit(); + return excursionGroup.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ExcursionGroupViewModel? Delete(ExcursionGroupBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.ExcursionGroups + .Include(x => x.Tours) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.ExcursionGroups.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs new file mode 100644 index 0000000..d219637 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/ExcursionStorage.cs @@ -0,0 +1,109 @@ +using Microsoft.EntityFrameworkCore; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class ExcursionStorage : IExcursionStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.Excursions + .Include(x => x.User) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ExcursionSearchModel model) + { + if (string.IsNullOrEmpty(model.ExcursionName)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.Excursions + .Include(x => x.User) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .Where(x => x.ExcursionName.Contains(model.ExcursionName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ExcursionViewModel? GetElement(ExcursionSearchModel model) + { + if (string.IsNullOrEmpty(model.ExcursionName) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelAgencyDatabase(); + return context.Excursions + .Include(x => x.User) + .Include(x => x.Tours) + .ThenInclude(x => x.Tour) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ExcursionName) && x.ExcursionName == model.ExcursionName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public ExcursionViewModel? Insert(ExcursionBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var newExcursion = Excursion.Create(context, model); + if (newExcursion == null) + { + return null; + } + context.Excursions.Add(newExcursion); + context.SaveChanges(); + return newExcursion.GetViewModel; + } + + public ExcursionViewModel? Update(ExcursionBindingModel model) + { + using var context = new TravelAgencyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var excursion = context.Excursions.FirstOrDefault(rec => rec.Id == model.Id); + if (excursion == null) + { + return null; + } + excursion.Update(model); + context.SaveChanges(); + excursion.UpdateTours(context, model); + transaction.Commit(); + return excursion.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ExcursionViewModel? Delete(ExcursionBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.Excursions + .Include(x => x.Tours) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Excursions.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs new file mode 100644 index 0000000..4d37a00 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/GuideStorage.cs @@ -0,0 +1,83 @@ +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class GuideStorage : IGuideStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.Guides + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(GuideSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.Guides + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public GuideViewModel? GetElement(GuideSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelAgencyDatabase(); + return context.Guides.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public GuideViewModel? Insert(GuideBindingModel model) + { + var newGuide = Guide.Create(model); + if (newGuide == null) + { + return null; + } + using var context = new TravelAgencyDatabase(); + context.Guides.Add(newGuide); + context.SaveChanges(); + return newGuide.GetViewModel; + } + + public GuideViewModel? Update(GuideBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var client = context.Guides.FirstOrDefault(x => x.Id == model.Id); + if (client == null) + { + return null; + } + client.Update(model); + context.SaveChanges(); + return client.GetViewModel; + } + + public GuideViewModel? Delete(GuideBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.Guides.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Guides.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs new file mode 100644 index 0000000..62d43e3 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/PlaceStorage.cs @@ -0,0 +1,97 @@ +using Microsoft.EntityFrameworkCore; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class PlaceStorage : IPlaceStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.Places + .Include(x => x.Excursion) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(PlaceSearchModel model) + { + if (string.IsNullOrEmpty(model.PlaceName)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.Places + .Include(x => x.Excursion) + .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 TravelAgencyDatabase(); + return context.Places.Include(x => x.Excursion) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaceName) && x.PlaceName == model.PlaceName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public PlaceViewModel? Insert(PlaceBindingModel model) + { + var newPlace = Place.Create(model); + if (newPlace == null) + { + return null; + } + using var context = new TravelAgencyDatabase(); + context.Places.Add(newPlace); + context.SaveChanges(); + return context.Places + .Include(x => x.Excursion) + .FirstOrDefault(x => x.Id == newPlace.Id) + ?.GetViewModel; + } + + public PlaceViewModel? Update(PlaceBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var place = context.Places.FirstOrDefault(x => x.Id == model.Id); + if (place == null) + { + return null; + } + place.Update(model); + context.SaveChanges(); + return context.Places + .Include(x => x.Excursion) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public PlaceViewModel? Delete(PlaceBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.Places.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Places + .Include(x => x.Excursion) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Places.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs new file mode 100644 index 0000000..e690676 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TourStorage.cs @@ -0,0 +1,97 @@ +using Microsoft.EntityFrameworkCore; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class TourStorage : ITourStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.Tours + .Include(x => x.User) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(TourSearchModel model) + { + if (string.IsNullOrEmpty(model.TourName)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.Tours + .Include(x => x.User) + .Where(x => x.TourName.Contains(model.TourName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public TourViewModel? GetElement(TourSearchModel model) + { + if (string.IsNullOrEmpty(model.TourName) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelAgencyDatabase(); + return context.Tours.Include(x => x.User) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.TourName) && x.TourName == model.TourName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public TourViewModel? Insert(TourBindingModel model) + { + var newTour = Tour.Create(model); + if (newTour == null) + { + return null; + } + using var context = new TravelAgencyDatabase(); + context.Tours.Add(newTour); + context.SaveChanges(); + return context.Tours + .Include(x => x.User) + .FirstOrDefault(x => x.Id == newTour.Id) + ?.GetViewModel; + } + + public TourViewModel? Update(TourBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var tour = context.Tours.FirstOrDefault(x => x.Id == model.Id); + if (tour == null) + { + return null; + } + tour.Update(model); + context.SaveChanges(); + return context.Tours + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public TourViewModel? Delete(TourBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.Tours.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + var deletedElement = context.Tours + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + context.Tours.Remove(element); + context.SaveChanges(); + return deletedElement; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs new file mode 100644 index 0000000..7b55280 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/TripStorage.cs @@ -0,0 +1,109 @@ +using Microsoft.EntityFrameworkCore; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class TripStorage : ITripStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.Trips + .Include(x => x.Guide) + .Include(x => x.Places) + .ThenInclude(x => x.Place) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(TripSearchModel model) + { + if (string.IsNullOrEmpty(model.TripName)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.Trips + .Include(x => x.Guide) + .Include(x => x.Places) + .ThenInclude(x => x.Place) + .Where(x => x.TripName.Contains(model.TripName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public TripViewModel? GetElement(TripSearchModel model) + { + if (string.IsNullOrEmpty(model.TripName) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelAgencyDatabase(); + return context.Trips + .Include(x => x.Guide) + .Include(x => x.Places) + .ThenInclude(x => x.Place) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.TripName) && x.TripName == model.TripName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public TripViewModel? Insert(TripBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var newTrip = Trip.Create(context, model); + if (newTrip == null) + { + return null; + } + context.Trips.Add(newTrip); + context.SaveChanges(); + return newTrip.GetViewModel; + } + + public TripViewModel? Update(TripBindingModel model) + { + using var context = new TravelAgencyDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var trip = context.Trips.FirstOrDefault(rec => rec.Id == model.Id); + if (trip == null) + { + return null; + } + trip.Update(model); + context.SaveChanges(); + trip.UpdatePlaces(context, model); + transaction.Commit(); + return trip.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public TripViewModel? Delete(TripBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.Trips + .Include(x => x.Places) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Trips.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs b/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs new file mode 100644 index 0000000..bbdff45 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Implements/UserStorage.cs @@ -0,0 +1,83 @@ +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.SearchModels; +using TravelAgencyContracts.StoragesContracts; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement.Implements +{ + public class UserStorage : IUserStorage + { + public List GetFullList() + { + using var context = new TravelAgencyDatabase(); + return context.Users + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Email)) + { + return new(); + } + using var context = new TravelAgencyDatabase(); + return context.Users + .Where(x => x.Email.Contains(model.Email)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public UserViewModel? GetElement(UserSearchModel model) + { + if (string.IsNullOrEmpty(model.Email) && string.IsNullOrEmpty(model.Password) && !model.Id.HasValue) + { + return null; + } + using var context = new TravelAgencyDatabase(); + return context.Users.FirstOrDefault(x => + (!string.IsNullOrEmpty(model.Email) && x.Email == model.Email && !string.IsNullOrEmpty(model.Password) && x.Password == model.Password) + || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public UserViewModel? Insert(UserBindingModel model) + { + var newUser = User.Create(model); + if (newUser == null) + { + return null; + } + using var context = new TravelAgencyDatabase(); + context.Users.Add(newUser); + context.SaveChanges(); + return newUser.GetViewModel; + } + + public UserViewModel? Update(UserBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var user = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (user == null) + { + return null; + } + user.Update(model); + context.SaveChanges(); + return user.GetViewModel; + } + + public UserViewModel? Delete(UserBindingModel model) + { + using var context = new TravelAgencyDatabase(); + var element = context.Users.FirstOrDefault(x => x.Id == model.Id); + if (element != null) + { + context.Users.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/Excursion.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/Excursion.cs new file mode 100644 index 0000000..f81ad60 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/Excursion.cs @@ -0,0 +1,106 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class Excursion : IExcursionModel + { + public int Id { get; set; } + + [Required] + public string ExcursionName { get; set; } = string.Empty; + + public string ExcursionDescription { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + [Required] + public int UserId { get; set; } + + public virtual User User { get; set; } + + private Dictionary? _excursionTours = null; + + [NotMapped] + public Dictionary ExcursionTours + { + get + { + if (_excursionTours == null) + { + _excursionTours = Tours + .ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel); + } + return _excursionTours; + } + } + + [ForeignKey("ExcursionId")] + public virtual List Tours { get; set; } = new(); + + public static Excursion? Create(TravelAgencyDatabase context, ExcursionBindingModel? model) + { + if (model == null) + { + return null; + } + return new Excursion() + { + Id = model.Id, + ExcursionName = model.ExcursionName, + ExcursionDescription = model.ExcursionDescription, + Price = model.Price, + UserId = model.UserId, + Tours = model.ExcursionTours.Select(x => new ExcursionTour + { + Tour = context.Tours.First(y => y.Id == x.Key) + }).ToList() + }; + } + public void Update(ExcursionBindingModel? model) + { + if (model == null) + { + return; + } + ExcursionName = model.ExcursionName; + ExcursionDescription = model.ExcursionDescription; + Price = model.Price; + } + + public ExcursionViewModel GetViewModel => new() + { + Id = Id, + ExcursionName = ExcursionName, + ExcursionDescription = ExcursionDescription, + Price = Price, + UserId = UserId, + ExcursionTours = ExcursionTours + }; + + public void UpdateTours(TravelAgencyDatabase context, ExcursionBindingModel model) + { + var excursionTours = context.ExcursionTours.Where(rec => rec.ExcursionId == model.Id).ToList(); + if (excursionTours != null && excursionTours.Count > 0) + { // удалили те, которых нет в модели + context.ExcursionTours.RemoveRange(excursionTours.Where(rec => !model.ExcursionTours.ContainsKey(rec.TourId))); + context.SaveChanges(); + } + var excursion = context.Excursions.First(x => x.Id == Id); + foreach (var et in model.ExcursionTours) + { + context.ExcursionTours.Add(new ExcursionTour + { + Excursion = excursion, + Tour = context.Tours.First(x => x.Id == et.Key) + }); + context.SaveChanges(); + } + _excursionTours = null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroup.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroup.cs new file mode 100644 index 0000000..f12a4df --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroup.cs @@ -0,0 +1,108 @@ +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class ExcursionGroup : IExcursionGroupModel + { + public int Id { get; set; } + + [Required] + public string ExcursionGroupName { get; set; } = string.Empty; + + [Required] + public int ParticipantsAmount { get; set; } + + [Required] + public int UserId { get; set; } + + [Required] + public int GuideId { get; set; } + + public virtual Guide Guide { get; set; } + + public virtual User User { get; set; } + + private Dictionary? _excursionGroupTours = null; + + [NotMapped] + public Dictionary ExcursionGroupTours + { + get + { + if (_excursionGroupTours == null) + { + _excursionGroupTours = Tours + .ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel); + } + return _excursionGroupTours; + } + } + + [ForeignKey("ExcursionGroupId")] + public virtual List Tours { get; set; } = new(); + + public static ExcursionGroup? Create(TravelAgencyDatabase context, ExcursionGroupBindingModel? model) + { + if (model == null) + { + return null; + } + return new ExcursionGroup() + { + Id = model.Id, + ExcursionGroupName = model.ExcursionGroupName, + ParticipantsAmount = model.ParticipantsAmount, + UserId = model.UserId, + GuideId = model.GuideId, + Tours = model.ExcursionGroupTours.Select(x => new ExcursionGroupTour + { + Tour = context.Tours.First(y => y.Id == x.Key) + }).ToList() + }; + } + public void Update(ExcursionGroupBindingModel? model) + { + if (model == null) + { + return; + } + ExcursionGroupName = model.ExcursionGroupName; + ParticipantsAmount = model.ParticipantsAmount; + } + + public ExcursionGroupViewModel GetViewModel => new() + { + Id = Id, + ExcursionGroupName = ExcursionGroupName, + ParticipantsAmount = ParticipantsAmount, + UserId = UserId, + GuideId = GuideId, + ExcursionGroupTours = ExcursionGroupTours + }; + + public void UpdateTours(TravelAgencyDatabase context, ExcursionGroupBindingModel model) + { + var excursionGroupTours = context.ExcursionGroupTours.Where(rec => rec.ExcursionGroupId == model.Id).ToList(); + if (excursionGroupTours != null && excursionGroupTours.Count > 0) + { // удалили те, которых нет в модели + context.ExcursionGroupTours.RemoveRange(excursionGroupTours.Where(rec => !model.ExcursionGroupTours.ContainsKey(rec.TourId))); + context.SaveChanges(); + } + var excursionGroup = context.ExcursionGroups.First(x => x.Id == Id); + foreach (var et in model.ExcursionGroupTours) + { + context.ExcursionGroupTours.Add(new ExcursionGroupTour + { + ExcursionGroup = excursionGroup, + Tour = context.Tours.First(x => x.Id == et.Key) + }); + context.SaveChanges(); + } + _excursionGroupTours = null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroupTour.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroupTour.cs new file mode 100644 index 0000000..3de9079 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionGroupTour.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class ExcursionGroupTour + { + public int Id { get; set; } + + [Required] + public int ExcursionGroupId { get; set; } + + [Required] + public int TourId { get; set; } + + public virtual ExcursionGroup ExcursionGroup { get; set; } = new(); + + public virtual Tour Tour { get; set; } = new(); + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionTour.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionTour.cs new file mode 100644 index 0000000..48119ff --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/ExcursionTour.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class ExcursionTour + { + public int Id { get; set; } + + [Required] + public int ExcursionId { get; set; } + + [Required] + public int TourId { get; set; } + + public virtual Excursion Excursion { get; set; } = new(); + + public virtual Tour Tour { get; set; } = new(); + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/Guide.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/Guide.cs new file mode 100644 index 0000000..4e00d75 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/Guide.cs @@ -0,0 +1,54 @@ +using System.ComponentModel.DataAnnotations; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class Guide : IGuideModel + { + public int Id { get; set; } + + [Required] + public string GuideFIO { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string PhoneNumber { get; set; } = string.Empty; + + public static Guide? Create(GuideBindingModel? model) + { + if (model == null) + { + return null; + } + return new Guide() + { + Id = model.Id, + GuideFIO = model.GuideFIO, + Email = model.Email, + PhoneNumber = model.PhoneNumber + }; + } + public void Update(GuideBindingModel? model) + { + if (model == null) + { + return; + } + GuideFIO = model.GuideFIO; + Email = model.Email; + PhoneNumber = model.PhoneNumber; + } + + public GuideViewModel GetViewModel => new() + { + Id = Id, + GuideFIO = GuideFIO, + Email = Email, + PhoneNumber = PhoneNumber + }; + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/Place.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/Place.cs new file mode 100644 index 0000000..85ccf0e --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/Place.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class Place : IPlaceModel + { + public int Id { get; set; } + + [Required] + public string PlaceName { get; set; } = string.Empty; + + [Required] + public string PlaceAddress { get; set; } = string.Empty; + + [Required] + public int ExcursionId { get; set; } + + public virtual Excursion Excursion { get; set; } + + [ForeignKey("PlaceId")] + public virtual List Trips { get; set; } = new(); + + public static Place? Create(PlaceBindingModel? model) + { + if (model == null) + { + return null; + } + return new Place() + { + Id = model.Id, + PlaceName = model.PlaceName, + PlaceAddress = model.PlaceAddress, + ExcursionId = model.ExcursionId + }; + } + public void Update(PlaceBindingModel? model) + { + if (model == null) + { + return; + } + PlaceName = model.PlaceName; + PlaceAddress = model.PlaceAddress; + ExcursionId = model.ExcursionId; + } + + public PlaceViewModel GetViewModel => new() + { + Id = Id, + PlaceName = PlaceName, + PlaceAddress = PlaceAddress, + ExcursionId = ExcursionId + }; + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/Tour.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/Tour.cs new file mode 100644 index 0000000..1f9d94d --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/Tour.cs @@ -0,0 +1,73 @@ +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class Tour : ITourModel + { + public int Id { get; set; } + + [Required] + public string TourName { get; set; } = string.Empty; + + public string TourDescription { get; set; } = string.Empty; + + [Required] + public double Price { get; set; } + + [Required] + public DateTime TourDate { get; set; } + + [Required] + public int UserId { get; set; } + + public virtual User User { get; set; } + + [ForeignKey("TourId")] + public virtual List Excursions { get; set; } = new(); + + [ForeignKey("TourId")] + public virtual List ExcursionGroups { get; set; } = new(); + + public static Tour? Create(TourBindingModel? model) + { + if (model == null) + { + return null; + } + return new Tour() + { + Id = model.Id, + TourName = model.TourName, + TourDescription = model.TourDescription, + Price = model.Price, + TourDate = model.TourDate, + UserId = model.UserId, + }; + } + public void Update(TourBindingModel? model) + { + if (model == null) + { + return; + } + TourName = model.TourName; + TourDescription = model.TourDescription; + Price = model.Price; + TourDate = model.TourDate; + } + + public TourViewModel GetViewModel => new() + { + Id = Id, + TourName = TourName, + TourDescription = TourDescription, + Price = Price, + TourDate = TourDate, + UserId = UserId + }; + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/Trip.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/Trip.cs new file mode 100644 index 0000000..fc40e14 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/Trip.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class Trip : ITripModel + { + public int Id { get; set; } + + [Required] + public string TripName { get; set; } = string.Empty; + + [Required] + public DateTime TripDate { get; set; } + + [Required] + public int GuideId { get; set; } + + public virtual Guide Guide { get; set; } + + private Dictionary? _tripPlaces = null; + + [NotMapped] + public Dictionary TripPlaces + { + get + { + if (_tripPlaces == null) + { + _tripPlaces = Places + .ToDictionary(recPC => recPC.PlaceId, recPC => recPC.Place as IPlaceModel); + } + return _tripPlaces; + } + } + + [ForeignKey("TripId")] + public virtual List Places { get; set; } = new(); + + public static Trip? Create(TravelAgencyDatabase context, TripBindingModel? model) + { + if (model == null) + { + return null; + } + return new Trip() + { + Id = model.Id, + TripName = model.TripName, + TripDate = model.TripDate, + GuideId = model.GuideId, + Places = model.TripPlaces.Select(x => new TripPlace + { + Place = context.Places.First(y => y.Id == x.Key) + }).ToList() + }; + } + public void Update(TripBindingModel? model) + { + if (model == null) + { + return; + } + TripName = model.TripName; + TripDate = model.TripDate; + } + + public TripViewModel GetViewModel => new() + { + Id = Id, + TripName = TripName, + TripDate = TripDate, + GuideId = GuideId, + TripPlaces = TripPlaces + }; + + public void UpdatePlaces(TravelAgencyDatabase context, TripBindingModel model) + { + var tripPlaces = context.TripPlaces.Where(rec => rec.TripId == model.Id).ToList(); + if (tripPlaces != null && tripPlaces.Count > 0) + { // удалили те, которых нет в модели + context.TripPlaces.RemoveRange(tripPlaces.Where(rec => !model.TripPlaces.ContainsKey(rec.PlaceId))); + context.SaveChanges(); + } + var trip = context.Trips.First(x => x.Id == Id); + foreach (var et in model.TripPlaces) + { + context.TripPlaces.Add(new TripPlace + { + Trip = trip, + Place = context.Places.First(x => x.Id == et.Key) + }); + context.SaveChanges(); + } + _tripPlaces = null; + } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/TripPlace.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/TripPlace.cs new file mode 100644 index 0000000..3aa56c5 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/TripPlace.cs @@ -0,0 +1,19 @@ +using System.ComponentModel.DataAnnotations; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class TripPlace + { + public int Id { get; set; } + + [Required] + public int TripId { get; set; } + + [Required] + public int PlaceId { get; set; } + + public virtual Trip Trip { get; set; } = new(); + + public virtual Place Place { get; set; } = new(); + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/Models/User.cs b/TravelAgency/TravelAgencyDatabaseImplement/Models/User.cs new file mode 100644 index 0000000..2932ff0 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/Models/User.cs @@ -0,0 +1,60 @@ +using System.ComponentModel.DataAnnotations; +using TravelAgencyContracts.BindingModels; +using TravelAgencyContracts.ViewModels; +using TravelAgencyDataModels.Models; + +namespace TravelAgencyDatabaseImplement.Models +{ + public class User : IUserModel + { + public int Id { get; set; } + + [Required] + public string UserFIO { get; set; } = string.Empty; + + [Required] + public string Email { get; set; } = string.Empty; + + [Required] + public string PhoneNumber { get; set; } = string.Empty; + + [Required] + public string Password { get; set; } = string.Empty; + + public static User? Create(UserBindingModel? model) + { + if (model == null) + { + return null; + } + return new User() + { + Id = model.Id, + UserFIO = model.UserFIO, + Email = model.Email, + PhoneNumber = model.PhoneNumber, + Password = model.Password, + }; + } + public void Update(UserBindingModel? model) + { + if (model == null) + { + return; + } + UserFIO = model.UserFIO; + Email = model.Email; + PhoneNumber = model.PhoneNumber; + Password = model.Password; + } + + public UserViewModel GetViewModel => new() + { + Id = Id, + UserFIO = UserFIO, + Email = Email, + PhoneNumber = PhoneNumber, + Password = Password + }; + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabase.cs b/TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabase.cs new file mode 100644 index 0000000..f1c592b --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabase.cs @@ -0,0 +1,37 @@ +using Microsoft.EntityFrameworkCore; +using TravelAgencyDatabaseImplement.Models; + +namespace TravelAgencyDatabaseImplement +{ + public class TravelAgencyDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=.\SQLEXPRESS;Initial Catalog=TravelAgencyDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + + public virtual DbSet Excursions { set; get; } + + public virtual DbSet ExcursionGroups { set; get; } + + public virtual DbSet Tours { set; get; } + + public virtual DbSet ExcursionTours { set; get; } + + public virtual DbSet ExcursionGroupTours { set; get; } + + public virtual DbSet Guides { set; get; } + + public virtual DbSet Places { set; get; } + + public virtual DbSet Users { set; get; } + + public virtual DbSet Trips { set; get; } + + public virtual DbSet TripPlaces { set; get; } + } +} diff --git a/TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabaseImplement.csproj b/TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabaseImplement.csproj new file mode 100644 index 0000000..312e9a9 --- /dev/null +++ b/TravelAgency/TravelAgencyDatabaseImplement/TravelAgencyDatabaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + +