DatabaseImplement

This commit is contained in:
russell 2024-04-28 23:08:12 +04:00
parent ccbd9b2a7f
commit 1c584ba71a
29 changed files with 1395 additions and 14 deletions

View File

@ -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

View File

@ -12,6 +12,8 @@ namespace TravelAgencyContracts.BindingModels
public double Price { get; set; }
public DateTime TourDate { get; set; }
public int UserId { get; set; }
}
}

View File

@ -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;
}
}

View File

@ -6,8 +6,6 @@
public string? TripName { get; set; }
public DateTime? DateTrip { get; set; }
public int? GuideId { get; set; }
}
}

View File

@ -10,8 +10,6 @@
public string? PhoneNumber { get; set; }
public string? Login { get; set; }
public string? Password { get; set; }
}
}

View File

@ -15,6 +15,9 @@ namespace TravelAgencyContracts.ViewModels
[DisplayName("Цена")]
public double Price { get; set; }
[DisplayName("Дата тура")]
public DateTime TourDate { get; set; }
public int UserId { get; set; }
}
}

View File

@ -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;
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace TravelAgencyDataModels.Models
{
public interface IGuideModel :IId
public interface IGuideModel : IId
{
string GuideFIO { get; }

View File

@ -8,6 +8,8 @@
double Price { get; }
DateTime TourDate { get; }
int UserId { get; }
}
}

View File

@ -8,8 +8,6 @@
string PhoneNumber { get; }
string Login { get; }
string Password { get; }
}
}

View File

@ -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<ExcursionGroupViewModel> 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<ExcursionGroupViewModel> 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;
}
}
}

View File

@ -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<ExcursionViewModel> 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<ExcursionViewModel> 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;
}
}
}

View File

@ -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<GuideViewModel> GetFullList()
{
using var context = new TravelAgencyDatabase();
return context.Guides
.Select(x => x.GetViewModel)
.ToList();
}
public List<GuideViewModel> 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;
}
}
}

View File

@ -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<PlaceViewModel> GetFullList()
{
using var context = new TravelAgencyDatabase();
return context.Places
.Include(x => x.Excursion)
.Select(x => x.GetViewModel)
.ToList();
}
public List<PlaceViewModel> 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;
}
}
}

View File

@ -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<TourViewModel> GetFullList()
{
using var context = new TravelAgencyDatabase();
return context.Tours
.Include(x => x.User)
.Select(x => x.GetViewModel)
.ToList();
}
public List<TourViewModel> 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;
}
}
}

View File

@ -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<TripViewModel> 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<TripViewModel> 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;
}
}
}

View File

@ -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<UserViewModel> GetFullList()
{
using var context = new TravelAgencyDatabase();
return context.Users
.Select(x => x.GetViewModel)
.ToList();
}
public List<UserViewModel> 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;
}
}
}

View File

@ -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<int, ITourModel>? _excursionTours = null;
[NotMapped]
public Dictionary<int, ITourModel> ExcursionTours
{
get
{
if (_excursionTours == null)
{
_excursionTours = Tours
.ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel);
}
return _excursionTours;
}
}
[ForeignKey("ExcursionId")]
public virtual List<ExcursionTour> 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;
}
}
}

View File

@ -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<int, ITourModel>? _excursionGroupTours = null;
[NotMapped]
public Dictionary<int, ITourModel> ExcursionGroupTours
{
get
{
if (_excursionGroupTours == null)
{
_excursionGroupTours = Tours
.ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel);
}
return _excursionGroupTours;
}
}
[ForeignKey("ExcursionGroupId")]
public virtual List<ExcursionGroupTour> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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
};
}
}

View File

@ -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<TripPlace> 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
};
}
}

View File

@ -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<ExcursionTour> Excursions { get; set; } = new();
[ForeignKey("TourId")]
public virtual List<ExcursionGroupTour> 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
};
}
}

View File

@ -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<int, IPlaceModel>? _tripPlaces = null;
[NotMapped]
public Dictionary<int, IPlaceModel> TripPlaces
{
get
{
if (_tripPlaces == null)
{
_tripPlaces = Places
.ToDictionary(recPC => recPC.PlaceId, recPC => recPC.Place as IPlaceModel);
}
return _tripPlaces;
}
}
[ForeignKey("TripId")]
public virtual List<TripPlace> 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;
}
}
}

View File

@ -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();
}
}

View File

@ -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
};
}
}

View File

@ -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<Excursion> Excursions { set; get; }
public virtual DbSet<ExcursionGroup> ExcursionGroups { set; get; }
public virtual DbSet<Tour> Tours { set; get; }
public virtual DbSet<ExcursionTour> ExcursionTours { set; get; }
public virtual DbSet<ExcursionGroupTour> ExcursionGroupTours { set; get; }
public virtual DbSet<Guide> Guides { set; get; }
public virtual DbSet<Place> Places { set; get; }
public virtual DbSet<User> Users { set; get; }
public virtual DbSet<Trip> Trips { set; get; }
public virtual DbSet<TripPlace> TripPlaces { set; get; }
}
}

View File

@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.27" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.27" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.27">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TravelAgencyContracts\TravelAgencyContracts.csproj" />
<ProjectReference Include="..\TravelAgencyDataModels\TravelAgencyDataModels.csproj" />
</ItemGroup>
</Project>