From 25d50f8d55a29abe2a0a26f0f61c2a55553ceced Mon Sep 17 00:00:00 2001 From: Salikh Date: Sun, 28 Apr 2024 18:11:56 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20impl?= =?UTF-8?q?ements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BusinessLogic/MealPlanLogic.cs | 2 +- .../BusinessLogic/OrganiserLogic.cs | 11 +- .../BusinessLogic/ParticipantLogic.cs | 10 +- .../BindingModels/ParticipantBindingModel.cs | 2 +- .../BusinessLogicsContracts/IMealPlanLogic.cs | 2 +- .../SearchModels/OrganiserSearchModel.cs | 10 +- .../SearchModels/ParticipantSearchModel.cs | 2 +- .../ViewModels/ParticipantViewModel.cs | 2 +- Hotel/HotelDataBaseImplement/HotelDataBase.cs | 7 +- .../Implements/ConferenceStorage.cs | 155 +++++++++++++++ .../Implements/DinnerStorage.cs | 133 +++++++++++++ .../Implements/HeadwaiterStorage.cs | 126 ++++++++++++ .../Implements/MealPlanStorage.cs | 159 +++++++++++++++ .../Implements/OrganiserStorage.cs | 113 +++++++++++ .../Implements/ParticipantStorage.cs | 135 +++++++++++++ .../Implements/RoomStorage.cs | 184 ++++++++++++++++++ .../Models/Conference.cs | 84 +++++++- .../Models/ConferenceParticipant.cs | 15 +- Hotel/HotelDataBaseImplement/Models/Dinner.cs | 3 +- .../HotelDataBaseImplement/Models/MealPlan.cs | 104 +++++++++- .../Models/MealPlanParticipant.cs | 14 +- .../Models/MealPlanRoom.cs | 23 +++ .../Models/Participant.cs | 24 ++- Hotel/HotelDataBaseImplement/Models/Room.cs | 35 +++- .../Models/IParticipantModel.cs | 2 +- 25 files changed, 1327 insertions(+), 30 deletions(-) create mode 100644 Hotel/HotelDataBaseImplement/Implements/ConferenceStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Implements/MealPlanStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Implements/OrganiserStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Implements/ParticipantStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Implements/RoomStorage.cs create mode 100644 Hotel/HotelDataBaseImplement/Models/MealPlanRoom.cs diff --git a/Hotel/HotelBusinessLogic/BusinessLogic/MealPlanLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogic/MealPlanLogic.cs index 1918ba6..6b08839 100644 --- a/Hotel/HotelBusinessLogic/BusinessLogic/MealPlanLogic.cs +++ b/Hotel/HotelBusinessLogic/BusinessLogic/MealPlanLogic.cs @@ -99,7 +99,7 @@ namespace HotelBusinessLogic.BusinessLogic return true; } - public bool AddMemberToMealPlan(MealPlanSearchModel model, IParticipantModel participant) + public bool AddParticipantToMealPlan(MealPlanSearchModel model, IParticipantModel participant) { if (model == null) { diff --git a/Hotel/HotelBusinessLogic/BusinessLogic/OrganiserLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogic/OrganiserLogic.cs index 820e657..3b4a399 100644 --- a/Hotel/HotelBusinessLogic/BusinessLogic/OrganiserLogic.cs +++ b/Hotel/HotelBusinessLogic/BusinessLogic/OrganiserLogic.cs @@ -126,7 +126,7 @@ namespace HotelBusinessLogic.BusinessLogic _logger.LogInformation("Organiser. OrganiserFIO: {OrganiserFIO}. OrganiserLogin: {OrganiserLogin}. Id: {Id}", model.OrganiserFIO, model.OrganiserLogin, model.Id); - var element = _organiserStorage.GetElement(new() + var element = _organiserStorage.GetElement(new OrganiserSearchModel { OrganiserEmail = model.OrganiserEmail, }); @@ -135,6 +135,15 @@ namespace HotelBusinessLogic.BusinessLogic { throw new InvalidOperationException("Организатор с таким логином уже есть"); } + + var elementLogin = _organiserStorage.GetElement(new OrganiserSearchModel + { + OrganiserLogin = model.OrganiserLogin + }); + if (elementLogin != null && elementLogin.Id != model.Id) + { + throw new InvalidOperationException("Организатор с таким логином уже есть"); + } } } } diff --git a/Hotel/HotelBusinessLogic/BusinessLogic/ParticipantLogic.cs b/Hotel/HotelBusinessLogic/BusinessLogic/ParticipantLogic.cs index d324040..2749299 100644 --- a/Hotel/HotelBusinessLogic/BusinessLogic/ParticipantLogic.cs +++ b/Hotel/HotelBusinessLogic/BusinessLogic/ParticipantLogic.cs @@ -25,7 +25,7 @@ namespace HotelBusinessLogic.BusinessLogic public List? ReadList(ParticipantSearchModel? model) { - _logger.LogInformation("ReadList. FIO:{FIO}.Id:{Id}", model?.FIO, model?.Id); + _logger.LogInformation("ReadList. FIO:{FIO}.Id:{Id}", model?.ParticipantFIO, model?.Id); var list = model == null ? _participantStorage.GetFullList() : _participantStorage.GetFilteredList(model); @@ -47,7 +47,7 @@ namespace HotelBusinessLogic.BusinessLogic throw new ArgumentNullException(nameof(model)); } - _logger.LogInformation("ReadElement.FIO:{FIO}.Id:{Id}", model.FIO, model.Id); + _logger.LogInformation("ReadElement.FIO:{FIO}.Id:{Id}", model.ParticipantFIO, model.Id); var element = _participantStorage.GetElement(model); @@ -110,12 +110,12 @@ namespace HotelBusinessLogic.BusinessLogic if (!withParams) return; - if (string.IsNullOrEmpty(model.FIO)) - throw new ArgumentNullException("Нет ФИО участника", nameof(model.FIO)); + if (string.IsNullOrEmpty(model.ParticipantFIO)) + throw new ArgumentNullException("Нет ФИО участника", nameof(model.ParticipantFIO)); if (string.IsNullOrEmpty (model.Number)) throw new ArgumentNullException("Нет номера участника", nameof(model.Number)); - _logger.LogInformation("Participant. FIO:{FIO}.Number:{Number}. Id:{Id}", model.FIO, model.Number, model.Id); + _logger.LogInformation("Participant. FIO:{FIO}.Number:{Number}. Id:{Id}", model.ParticipantFIO, model.Number, model.Id); } } } diff --git a/Hotel/HotelContracts/BindingModels/ParticipantBindingModel.cs b/Hotel/HotelContracts/BindingModels/ParticipantBindingModel.cs index aef383d..131990c 100644 --- a/Hotel/HotelContracts/BindingModels/ParticipantBindingModel.cs +++ b/Hotel/HotelContracts/BindingModels/ParticipantBindingModel.cs @@ -11,7 +11,7 @@ namespace HotelContracts.BindingModels { public int Id { get; set; } - public string FIO { get; set; } = string.Empty; + public string ParticipantFIO { get; set; } = string.Empty; public string Number { get; set; } = string.Empty; diff --git a/Hotel/HotelContracts/BusinessLogicsContracts/IMealPlanLogic.cs b/Hotel/HotelContracts/BusinessLogicsContracts/IMealPlanLogic.cs index fa2cc1b..da74654 100644 --- a/Hotel/HotelContracts/BusinessLogicsContracts/IMealPlanLogic.cs +++ b/Hotel/HotelContracts/BusinessLogicsContracts/IMealPlanLogic.cs @@ -13,6 +13,6 @@ namespace HotelContracts.BusinessLogicsContracts bool Create(MealPlanBindingModel model); bool Update(MealPlanBindingModel model); bool Delete(MealPlanBindingModel model); - bool AddMemberToMealPlan(MealPlanSearchModel model, IParticipantModel participant); + bool AddParticipantToMealPlan(MealPlanSearchModel model, IParticipantModel participant); } } diff --git a/Hotel/HotelContracts/SearchModels/OrganiserSearchModel.cs b/Hotel/HotelContracts/SearchModels/OrganiserSearchModel.cs index 1e0dd42..b200bc9 100644 --- a/Hotel/HotelContracts/SearchModels/OrganiserSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/OrganiserSearchModel.cs @@ -8,14 +8,14 @@ namespace HotelContracts.SearchModels { public class OrganiserSearchModel { - public int Id { get; set; } + public int? Id { get; set; } - public string OrganiserLogin { get; set; } = string.Empty; + public string? OrganiserLogin { get; set; } = string.Empty; - public string OrganiserPassword { get; set; } = string.Empty; + public string? OrganiserPassword { get; set; } = string.Empty; - public string OrganiserEmail { get; set; } = string.Empty; + public string? OrganiserEmail { get; set; } = string.Empty; - public string OrganiserFIO { get; set; } = string.Empty; + public string? OrganiserFIO { get; set; } = string.Empty; } } diff --git a/Hotel/HotelContracts/SearchModels/ParticipantSearchModel.cs b/Hotel/HotelContracts/SearchModels/ParticipantSearchModel.cs index 7322f13..bf955a5 100644 --- a/Hotel/HotelContracts/SearchModels/ParticipantSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/ParticipantSearchModel.cs @@ -10,7 +10,7 @@ namespace HotelContracts.SearchModels { public int? Id { get; set; } - public string? FIO { get; set; } + public string? ParticipantFIO { get; set; } public int? OrganiserId { get; set; } } diff --git a/Hotel/HotelContracts/ViewModels/ParticipantViewModel.cs b/Hotel/HotelContracts/ViewModels/ParticipantViewModel.cs index 6962701..e7669c6 100644 --- a/Hotel/HotelContracts/ViewModels/ParticipantViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/ParticipantViewModel.cs @@ -13,7 +13,7 @@ namespace HotelContracts.ViewModels public int Id { get; set; } [DisplayName("ФИО участника")] - public string FIO { get; set; } = string.Empty; + public string ParticipantFIO { get; set; } = string.Empty; [DisplayName("Номер участника")] public string Number { get; set; } = string.Empty; diff --git a/Hotel/HotelDataBaseImplement/HotelDataBase.cs b/Hotel/HotelDataBaseImplement/HotelDataBase.cs index 8ca075c..ed88ece 100644 --- a/Hotel/HotelDataBaseImplement/HotelDataBase.cs +++ b/Hotel/HotelDataBaseImplement/HotelDataBase.cs @@ -19,11 +19,16 @@ namespace HotelDataBaseImplement public virtual DbSet Conferences { get; set; } public virtual DbSet MealPlans { set; get; } - public virtual DbSet Participant { set; get; } + public virtual DbSet Participants { set; get; } public virtual DbSet Organisers { set; get; } public virtual DbSet Administrators { set; get; } public virtual DbSet ConferenceBookings { set; get; } public virtual DbSet Rooms { set; get; } public virtual DbSet Dinners { set; get; } + public virtual DbSet ConferenceBookingDinners { set; get; } + public virtual DbSet ConferenceParticipants { set; get; } + public virtual DbSet MealPlanParticipants { set; get; } + public virtual DbSet MealPlanRooms { set; get; } + public virtual DbSet RoomDinners { set; get; } } } diff --git a/Hotel/HotelDataBaseImplement/Implements/ConferenceStorage.cs b/Hotel/HotelDataBaseImplement/Implements/ConferenceStorage.cs new file mode 100644 index 0000000..d680141 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/ConferenceStorage.cs @@ -0,0 +1,155 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Implements +{ + public class ConferenceStorage : IConferenceStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Conferences + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Bookings) + .Include(x => x.Organiser) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ConferenceSearchModel model) + { + if (!model.OrganiserId.HasValue) + { + return new(); + } + using var context = new HotelDataBase(); + + if (model.OrganiserId.HasValue) + return context.Conferences + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Bookings) + .Include(x => x.Organiser) + .Where(x => x.OrganiserId == model.OrganiserId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + + return context.Conferences + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Bookings) + .Include(x => x.Organiser) + .Where(x => x.ConferenceName.Contains(model.ConferenceName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ConferenceViewModel? GetElement(ConferenceSearchModel model) + { + if (string.IsNullOrEmpty(model.ConferenceName) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Conferences + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Bookings) + .Include(x => x.Organiser) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ConferenceName) && x.ConferenceName == model.ConferenceName) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public ConferenceViewModel? Insert(ConferenceBindingModel model) + { + using var context = new HotelDataBase(); + var newConference = Conference.Create(context, model); + + if (newConference == null) + { + return null; + } + + context.Conferences.Add(newConference); + context.SaveChanges(); + + return context.Conferences + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Bookings) + .Include(x => x.Organiser) + .FirstOrDefault(x => x.Id == newConference.Id) + ?.GetViewModel; + } + + public ConferenceViewModel? Update(ConferenceBindingModel model) + { + using var context = new HotelDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var elem = context.Conferences.FirstOrDefault(rec => rec.Id == model.Id); + if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.ConferenceParticipants != null) + elem.UpdateMembers(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ConferenceViewModel? Delete(ConferenceBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Conferences + .Include(x => x.Participants) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Conferences.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs b/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs new file mode 100644 index 0000000..255891c --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs @@ -0,0 +1,133 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Implements +{ + public class DinnerStorage : IDinnerStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Dinners + .Include(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.Administrator) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(DinnerSearchModel model) + { + if (string.IsNullOrEmpty(model.DinnerName) && !model.HeadwaiterId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + + if (model.AdministratorId.HasValue) + { + return context.Dinners + .Include(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.Administrator) + .Where(x => x.AdministratorId == model.AdministratorId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Dinners + .Include(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.Administrator) + .Where(x => x.DinnerName.Contains(model.DinnerName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public DinnerViewModel? GetElement(DinnerSearchModel model) + { + if (string.IsNullOrEmpty(model.DinnerName) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Dinners + .Include(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.DinnerName) && x.DinnerName == model.DinnerName) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public DinnerViewModel? Insert(DinnerBindingModel model) + { + using var context = new HotelDataBase(); + + var newDinner = Dinner.Create(model); + + if (newDinner == null) + { + return null; + } + + context.Dinners.Add(newDinner); + context.SaveChanges(); + + return newDinner.GetViewModel; + } + + public DinnerViewModel? Update(DinnerBindingModel model) + { + using var context = new HotelDataBase(); + + var dinner = context.Dinners.FirstOrDefault(x => x.Id == model.Id); + + if (dinner == null) + { + return null; + } + + dinner.Update(model); + context.SaveChanges(); + + return dinner.GetViewModel; + } + + public DinnerViewModel? Delete(DinnerBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Dinners.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Dinners.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs b/Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs new file mode 100644 index 0000000..22bbcf7 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs @@ -0,0 +1,126 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HotelDataBaseImplement.Implements +{ + public class HeadwaiterStorage : IAdministratorStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Administrators.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(AdministratorSearchModel model) + { + if (string.IsNullOrEmpty(model.AdministratorFIO)) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Administrators + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .Where(x => x.AdministratorLogin.Contains(model.AdministratorLogin) && x.AdministratorPassword == model.AdministratorPassword) + .Select(x => x.GetViewModel) + .ToList(); + } + + public AdministratorViewModel? GetElement(AdministratorSearchModel model) + { + using var context = new HotelDataBase(); + + if(model.Id.HasValue) + { + return context.Administrators + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + } + + if(!string.IsNullOrEmpty(model.AdministratorEmail) && !string.IsNullOrEmpty(model.AdministratorPassword)) + { + return context.Administrators + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.AdministratorEmail.Equals(model.AdministratorEmail) && x.AdministratorPassword.Equals(model.AdministratorPassword))? + .GetViewModel; + } + + if (!string.IsNullOrEmpty(model.AdministratorEmail)) + { + return context.Administrators + .Include(x => x.ConferenceBookings) + .Include(x => x.Dinners) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.AdministratorEmail.Equals(model.AdministratorEmail))? + .GetViewModel; + } + return null; + } + + public AdministratorViewModel? Insert(AdministratorBindingModel model) + { + var newAdministrator = Administrator.Create(model); + + + if (string.IsNullOrEmpty(model.AdministratorFIO)) + { + return new(); + } + + using var context = new HotelDataBase(); + + context.Administrators.Add(newAdministrator); + context.SaveChanges(); + + return newAdministrator.GetViewModel; + } + + public AdministratorViewModel? Update(AdministratorBindingModel model) + { + using var context = new HotelDataBase(); + + var administrator = context.Administrators + .FirstOrDefault(x => x.Id == model.Id); + + if (administrator == null) + { + return null; + } + + administrator.Update(model); + context.SaveChanges(); + + return administrator.GetViewModel; + } + + public AdministratorViewModel? Delete(AdministratorBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Administrators.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Administrators.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/MealPlanStorage.cs b/Hotel/HotelDataBaseImplement/Implements/MealPlanStorage.cs new file mode 100644 index 0000000..d89ee88 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/MealPlanStorage.cs @@ -0,0 +1,159 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Implements +{ + public class MealPlanStorage : IMealPlanStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + return context.MealPlans + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .ThenInclude(x => x.Room) + .ThenInclude(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .Include(x => x.Organiser) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(MealPlanSearchModel model) + { + if (string.IsNullOrEmpty(model.MealPlanName) && !model.OrganiserId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + + if (model.OrganiserId.HasValue) + { + return context.MealPlans + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .ThenInclude(x => x.Room) + .ThenInclude(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .Include(x => x.Organiser) + .Where(x => x.OrganiserId == model.OrganiserId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.MealPlans + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .ThenInclude(x => x.Room) + .ThenInclude(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .Include(x => x.Organiser) + .Where(x => x.MealPlanName.Contains(model.MealPlanName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public MealPlanViewModel? GetElement(MealPlanSearchModel model) + { + if (string.IsNullOrEmpty(model.MealPlanName) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.MealPlans + .Include(x => x.Participants) + .ThenInclude(x => x.Participant) + .ThenInclude(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .ThenInclude(x => x.Room) + .ThenInclude(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .Include(x => x.Organiser) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.MealPlanName) && x.MealPlanName == model.MealPlanName) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public MealPlanViewModel? Insert(MealPlanBindingModel model) + { + using var context = new HotelDataBase(); + var newMealPlan = MealPlan.Create(context, model); + + if (newMealPlan == null) + { + return null; + } + + context.MealPlans.Add(newMealPlan); + context.SaveChanges(); + return newMealPlan.GetViewModel; + } + + public MealPlanViewModel? Update(MealPlanBindingModel model) + { + using var context = new HotelDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var elem = context.MealPlans.FirstOrDefault(rec => rec.Id == model.Id); + if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.MealPlanParticipants != null) + elem.UpdateMembers(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public MealPlanViewModel? Delete(MealPlanBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.MealPlans + .Include(x => x.Participants) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.MealPlans.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/OrganiserStorage.cs b/Hotel/HotelDataBaseImplement/Implements/OrganiserStorage.cs new file mode 100644 index 0000000..ebf6a0e --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/OrganiserStorage.cs @@ -0,0 +1,113 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HotelDataBaseImplement.Implements +{ + public class OrganiserStorage : IOrganiserStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Organisers.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(OrganiserSearchModel model) + { + if (string.IsNullOrEmpty(model.OrganiserFIO)) + { + return new(); + } + + using var context = new HotelDataBase(); + + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Participants) + .Include(x => x.Conferences) + .Where(x => x.OrganiserLogin.Contains(model.OrganiserLogin) && x.OrganiserPassword == model.OrganiserPassword) + .Select(x => x.GetViewModel) + .ToList(); + } + + public OrganiserViewModel? GetElement(OrganiserSearchModel model) + { + using var context = new HotelDataBase(); + + if (model.Id.HasValue) + { + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Participants) + .Include(x => x.Conferences) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + } + + if (!string.IsNullOrEmpty(model.OrganiserEmail) && !string.IsNullOrEmpty(model.OrganiserPassword)) + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Participants) + .Include(x => x.Conferences) + .FirstOrDefault(x => x.OrganiserEmail.Equals(model.OrganiserEmail) && x.OrganiserPassword.Equals(model.OrganiserPassword))? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.OrganiserEmail)) + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Participants) + .Include(x => x.Conferences) + .FirstOrDefault(x => x.OrganiserEmail.Equals(model.OrganiserEmail))? + .GetViewModel; + + return null; + } + + public OrganiserViewModel? Insert(OrganiserBindingModel model) + { + var newOrganiser = Organiser.Create(model); + + if(newOrganiser == null) return null; + + using var context = new HotelDataBase(); + + context.Organisers.Add(newOrganiser); + context.SaveChanges(); + + return newOrganiser.GetViewModel; + } + + public OrganiserViewModel? Update(OrganiserBindingModel model) + { + using var context = new HotelDataBase(); + + var organiser = context.Organisers.FirstOrDefault(x => x.Id == model.Id); + + if (organiser == null) return null; + + organiser.Update(model); + context.SaveChanges(); + + return organiser.GetViewModel; + } + + public OrganiserViewModel? Delete(OrganiserBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Organisers.FirstOrDefault(x => x.Id == model.Id); + + if (element != null) + { + context.Organisers.Remove(element); + context.SaveChanges(); + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/ParticipantStorage.cs b/Hotel/HotelDataBaseImplement/Implements/ParticipantStorage.cs new file mode 100644 index 0000000..f2f7be9 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/ParticipantStorage.cs @@ -0,0 +1,135 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Diagnostics.Metrics; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Implements +{ + public class ParticipantStorage : IParticipantStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Participants + .Include(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Organiser) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ParticipantSearchModel model) + { + if (string.IsNullOrEmpty(model.ParticipantFIO) && !model.OrganiserId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + + if (model.OrganiserId.HasValue) + { + return context.Participants + .Include(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Organiser) + .Where(x => x.OrganiserId == model.OrganiserId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Participants + .Include(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Organiser) + .Where(x => x.ParticipantFIO.Contains(model.ParticipantFIO)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ParticipantViewModel? GetElement(ParticipantSearchModel model) + { + if (string.IsNullOrEmpty(model.ParticipantFIO) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Participants + .Include(x => x.ConferenceParticipants) + .ThenInclude(x => x.Conference) + .Include(x => x.MealPlanParticipants) + .ThenInclude(x => x.MealPlan) + .Include(x => x.Organiser) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ParticipantFIO) && x.ParticipantFIO == model.ParticipantFIO) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public ParticipantViewModel? Insert(ParticipantBindingModel model) + { + using var context = new HotelDataBase(); + + var newParticipant = Participant.Create(model); + + if (newParticipant == null) + { + return null; + } + + context.Participants.Add(newParticipant); + context.SaveChanges(); + + return newParticipant.GetViewModel; + } + + public ParticipantViewModel? Update(ParticipantBindingModel model) + { + using var context = new HotelDataBase(); + + var participant = context.Participants.FirstOrDefault(x => x.Id == model.Id); + + if (participant == null) + { + return null; + } + + participant.Update(model); + context.SaveChanges(); + + return participant.GetViewModel; + } + + public ParticipantViewModel? Delete(ParticipantBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Participants.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Participants.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/RoomStorage.cs b/Hotel/HotelDataBaseImplement/Implements/RoomStorage.cs new file mode 100644 index 0000000..afd499b --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/RoomStorage.cs @@ -0,0 +1,184 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using Microsoft.IdentityModel.Tokens; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Implements +{ + public class RoomStorage : IRoomStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + return context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlanRooms) + .ThenInclude(x => x.MealPlan) + .ThenInclude(x => x.Participants) + .ThenInclude(x => x.Participant) + .Include(x => x.Administrator) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(RoomSearchModel model) + { + if (!model.Id.HasValue && !model.AdministratorId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + + if (model.AdministratorId.HasValue) + { + return context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlanRooms) + .ThenInclude(x => x.MealPlan) + .ThenInclude(x => x.Participants) + .ThenInclude(x => x.Participant) + .Include(x => x.Administrator) + .Where(x => x.AdministratorId == model.AdministratorId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlanRooms) + .ThenInclude(x => x.MealPlan) + .ThenInclude(x => x.Participants) + .ThenInclude(x => x.Participant) + .Include(x => x.Administrator) + .Where(x => x.RoomNumber == model.RoomNumber) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RoomViewModel? GetElement(RoomSearchModel model) + { + if (!model.Id.HasValue && model.RoomNumber != null) + { + return null; + } + + using var context = new HotelDataBase(); + + var room = context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlanRooms) + .ThenInclude(x => x.MealPlan) + .ThenInclude(x => x.Participants) + .ThenInclude(x => x.Participant) + .Include(x => x.Administrator) + .FirstOrDefault(x => (model.RoomNumber != null && x.RoomNumber == model.RoomNumber) || (model.Id.HasValue && x.Id == model.Id)); + + return room?.GetViewModel; + } + + public RoomViewModel? Insert(RoomBindingModel model) + { + using var context = new HotelDataBase(); + var newRoom = Room.Create(context, model); + + if (newRoom == null) + { + return null; + } + + context.Rooms.Add(newRoom); + context.SaveChanges(); + + return context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlanRooms) + .ThenInclude(x => x.MealPlan) + .ThenInclude(x => x.Participants) + .ThenInclude(x => x.Participant) + .Include(x => x.Administrator) + .FirstOrDefault(x => x.Id == newRoom.Id) + ?.GetViewModel; + } + + public RoomViewModel? Update(RoomBindingModel model) + { + using var context = new HotelDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var elem = context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .ThenInclude(x => x.Conference) + .FirstOrDefault(rec => rec.Id == model.Id); + if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.RoomDinners != null) + elem.UpdateDinners(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public RoomViewModel? Delete(RoomBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Rooms + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .ThenInclude(x => x.Conference) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Rooms.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Models/Conference.cs b/Hotel/HotelDataBaseImplement/Models/Conference.cs index 764cf57..c5cc4e9 100644 --- a/Hotel/HotelDataBaseImplement/Models/Conference.cs +++ b/Hotel/HotelDataBaseImplement/Models/Conference.cs @@ -1,8 +1,11 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; using Microsoft.EntityFrameworkCore; 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; @@ -21,14 +24,89 @@ namespace HotelDataBaseImplement.Models public int OrganiserId { get; private set; } - private Dictionary _conferenceMembers = null; + public virtual Organiser Organiser { get; set; } + private Dictionary _conferenceParticipents = null; + + [ForeignKey("ConferenceId")] + public virtual List Bookings { get; set; } = new(); + + [ForeignKey("ConferenceId")] + public virtual List Participants { get; set; } = new(); + + [NotMapped] public Dictionary ConferenceParticipants { get { - return _conferenceMembers; + if (_conferenceParticipents == null) + { + using var context = new HotelDataBase(); + _conferenceParticipents = Participants.ToDictionary(x => x.ParticipantId, x => (context.Participants + .FirstOrDefault(y => y.Id == x.ParticipantId)! as IParticipantModel)); + } + return _conferenceParticipents; } } + + public static Conference Create(HotelDataBase context, ConferenceBindingModel model) + { + return new Conference() + { + Id = model.Id, + ConferenceName = model.ConferenceName, + Subject = model.Subject, + OrganiserId = model.OrganiserId, + Participants = model.ConferenceParticipants.Select(x => new ConferenceParticipant + { + Participant = context.Participants.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(ConferenceBindingModel model) + { + ConferenceName = model.ConferenceName; + Subject = model.Subject; + OrganiserId = model.OrganiserId; + } + + public ConferenceViewModel GetViewModel => new() + { + Id = Id, + ConferenceName = ConferenceName, + Subject = Subject, + OrganiserId = OrganiserId, + ConferenceParticipants = ConferenceParticipants + }; + + public void UpdateMembers(HotelDataBase context, ConferenceBindingModel model) + { + var сonferenceParticipants = context.ConferenceParticipants.Where(rec => rec.ConferenceId == model.Id).ToList(); + + if (сonferenceParticipants != null && сonferenceParticipants.Count > 0) + { + context.ConferenceParticipants.RemoveRange(сonferenceParticipants.Where(rec => !model.ConferenceParticipants.ContainsKey(rec.ParticipantId))); + context.SaveChanges(); + + foreach (var updateParticipant in сonferenceParticipants) + { + model.ConferenceParticipants.Remove(updateParticipant.ParticipantId); + } + context.SaveChanges(); + } + + var conference = context.Conferences.First(x => x.Id == Id); + foreach (var cm in model.ConferenceParticipants) + { + context.ConferenceParticipants.Add(new ConferenceParticipant + { + Conference = conference, + Participant = context.Participants.First(x => x.Id == cm.Key), + }); + context.SaveChanges(); + } + _conferenceParticipents = null; + } } } diff --git a/Hotel/HotelDataBaseImplement/Models/ConferenceParticipant.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceParticipant.cs index abaf25b..f444759 100644 --- a/Hotel/HotelDataBaseImplement/Models/ConferenceParticipant.cs +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceParticipant.cs @@ -1,6 +1,19 @@ -namespace HotelDataBaseImplement.Models +using System.ComponentModel.DataAnnotations; +using System.Diagnostics.Metrics; + +namespace HotelDataBaseImplement.Models { public class ConferenceParticipant { + public int Id { get; set; } + + [Required] + public int ParticipantId { get; set; } + + [Required] + public int ConferenceId { get; set; } + + public virtual Conference Conference { get; set; } = new(); + public virtual Participant Participant { get; set; } = new(); } } \ No newline at end of file diff --git a/Hotel/HotelDataBaseImplement/Models/Dinner.cs b/Hotel/HotelDataBaseImplement/Models/Dinner.cs index 58313b2..ec47bf7 100644 --- a/Hotel/HotelDataBaseImplement/Models/Dinner.cs +++ b/Hotel/HotelDataBaseImplement/Models/Dinner.cs @@ -25,7 +25,8 @@ namespace HotelDataBaseImplement.Models [ForeignKey("DinnerId")] public virtual List RoomDinners { get; set; } = new(); [ForeignKey("DinnerId")] - public virtual List ConferenceBookingDinner { get; set; } = new(); + public virtual List ConferenceBookingDinner { get; set; } + public static Dinner? Create(DinnerBindingModel model) { if (model == null) diff --git a/Hotel/HotelDataBaseImplement/Models/MealPlan.cs b/Hotel/HotelDataBaseImplement/Models/MealPlan.cs index cffb3ae..b752e9d 100644 --- a/Hotel/HotelDataBaseImplement/Models/MealPlan.cs +++ b/Hotel/HotelDataBaseImplement/Models/MealPlan.cs @@ -1,8 +1,110 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using Npgsql.PostgresTypes; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Globalization; namespace HotelDataBaseImplement.Models { public class MealPlan : IMealPlanModel { + public int Id { get; private set; } + + [Required] + public string MealPlanName { get; set; } = string.Empty; + + [Required] + public double MealPlanPrice { get; set; } + + public int OrganiserId { get; private set; } + + public virtual Organiser Organiser { get; set; } + + private Dictionary _mealPlanParticipants = null; + + [ForeignKey("MealPlanId")] + public virtual List Participants { get; set; } = new(); + + [ForeignKey("MealPlanId")] + public virtual List Rooms { get; set; } = new(); + + [NotMapped] + public Dictionary MealPlanParticipants + { + get + { + if( _mealPlanParticipants == null ) + { + using var contex = new HotelDataBase(); + _mealPlanParticipants = Participants.ToDictionary(x => x.ParticipantId, x => (contex.Participants + .FirstOrDefault(y => y.Id == x.ParticipantId)! as IParticipantModel)); + } + return _mealPlanParticipants; + } + } + + public static MealPlan Create(HotelDataBase context, MealPlanBindingModel model) + { + return new MealPlan() + { + Id = model.Id, + MealPlanName = model.MealPlanName, + MealPlanPrice = model.MealPlanPrice, + OrganiserId = model.OrganiserId, + Participants = model.MealPlanParticipants.Select(x => new MealPlanParticipant + { + Participant = context.Participants.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(MealPlanBindingModel model) + { + if (model == null) return; + MealPlanName = model.MealPlanName; + MealPlanPrice = model.MealPlanPrice; + OrganiserId = model.OrganiserId; + } + + public MealPlanViewModel GetViewModel => new() + { + Id = Id, + MealPlanName = MealPlanName, + MealPlanPrice = MealPlanPrice, + OrganiserId = OrganiserId, + }; + + public void UpdateMembers(HotelDataBase context, MealPlanBindingModel model) + { + var mealPlanParticipants = context.MealPlanParticipants.Where(rec => rec.MealPlanId == model.Id).ToList(); + + if (mealPlanParticipants != null && mealPlanParticipants.Count > 0) + { + context.MealPlanParticipants.RemoveRange(mealPlanParticipants.Where(rec => !model.MealPlanParticipants.ContainsKey(rec.ParticipantId))); + context.SaveChanges(); + + foreach (var updateMember in mealPlanParticipants) + { + model.MealPlanParticipants.Remove(updateMember.ParticipantId); + } + context.SaveChanges(); + } + + var mealPlan = context.MealPlans.First(x => x.Id == Id); + + foreach (var cm in model.MealPlanParticipants) + { + context.MealPlanParticipants.Add(new MealPlanParticipant + { + MealPlan = mealPlan, + Participant = context.Participants.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _mealPlanParticipants = null; + } } } diff --git a/Hotel/HotelDataBaseImplement/Models/MealPlanParticipant.cs b/Hotel/HotelDataBaseImplement/Models/MealPlanParticipant.cs index 46a83fb..5af16c9 100644 --- a/Hotel/HotelDataBaseImplement/Models/MealPlanParticipant.cs +++ b/Hotel/HotelDataBaseImplement/Models/MealPlanParticipant.cs @@ -1,6 +1,18 @@ -namespace HotelDataBaseImplement.Models +using System.ComponentModel.DataAnnotations; + +namespace HotelDataBaseImplement.Models { public class MealPlanParticipant { + public int Id { get; set; } + + [Required] + public int ParticipantId { get; set; } + + [Required] + public int MealPlanId { get; set; } + + public virtual MealPlan MealPlan { get; set; } = new(); + public virtual Participant Participant { get; set; } = new(); } } \ No newline at end of file diff --git a/Hotel/HotelDataBaseImplement/Models/MealPlanRoom.cs b/Hotel/HotelDataBaseImplement/Models/MealPlanRoom.cs new file mode 100644 index 0000000..72c4eec --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Models/MealPlanRoom.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HotelDataBaseImplement.Models +{ + public class MealPlanRoom + { + public int Id { get; set; } + + [Required] + public int RoomId { get; set; } + + [Required] + public int MealPlanId { get; set; } + + public virtual MealPlan MealPlan { get; set; } = new(); + public virtual Room Room { get; set; } = new(); + } +} diff --git a/Hotel/HotelDataBaseImplement/Models/Participant.cs b/Hotel/HotelDataBaseImplement/Models/Participant.cs index 79471ec..7d76859 100644 --- a/Hotel/HotelDataBaseImplement/Models/Participant.cs +++ b/Hotel/HotelDataBaseImplement/Models/Participant.cs @@ -16,7 +16,7 @@ namespace HotelDataBaseImplement.Models public int Id { get; private set; } [Required] - public string FIO { get; set; } = string.Empty; + public string ParticipantFIO { get; set; } = string.Empty; [Required] public string Number { get; set; } = string.Empty; @@ -29,7 +29,7 @@ namespace HotelDataBaseImplement.Models public virtual List MealPlanParticipants { get; set; } = new(); [ForeignKey("ParticipantId")] - public virtual List ConferenceParticipant { get; set; } = new(); + public virtual List ConferenceParticipants { get; set; } = new(); public static Participant? Create(ParticipantBindingModel model) { @@ -37,7 +37,7 @@ namespace HotelDataBaseImplement.Models return new Participant() { Id = model.Id, - FIO = model.FIO, + ParticipantFIO = model.ParticipantFIO, Number = model.Number, OrganiserId = model.OrganiserId, }; @@ -48,10 +48,26 @@ namespace HotelDataBaseImplement.Models return new Participant { Id = model.Id, - FIO = model.FIO, + ParticipantFIO = model.ParticipantFIO, Number = model.Number, OrganiserId = model.OrganiserId, }; } + + public void Update(ParticipantBindingModel model) + { + if (model == null) return; + ParticipantFIO = model.ParticipantFIO; + Number = model.Number; + OrganiserId = model.OrganiserId; + } + + public ParticipantViewModel GetViewModel => new() + { + Id = Id, + ParticipantFIO = ParticipantFIO, + Number = Number, + OrganiserId = OrganiserId, + }; } } diff --git a/Hotel/HotelDataBaseImplement/Models/Room.cs b/Hotel/HotelDataBaseImplement/Models/Room.cs index b7e8c7e..c9f7bf0 100644 --- a/Hotel/HotelDataBaseImplement/Models/Room.cs +++ b/Hotel/HotelDataBaseImplement/Models/Room.cs @@ -25,8 +25,12 @@ namespace HotelDataBaseImplement.Models public int? MealPlanId { get; private set; } public virtual Administrator Administrator { get; set; } public virtual MealPlan? MealPlan { get; set; } + [ForeignKey("RoomId")] - public virtual List Dinners { get; set; } + public virtual List Dinners { get; set; } = new(); + + [ForeignKey("RoomId")] + public virtual List MealPlanRooms { get; set; } private Dictionary _roomDinners = null; [NotMapped] @@ -81,5 +85,34 @@ namespace HotelDataBaseImplement.Models RoomDinners = RoomDinners }; + public void UpdateDinners(HotelDataBase context, RoomBindingModel model) + { + var roomDinners = context.RoomDinners.Where(rec => rec.RoomId == model.Id).ToList(); + + if (roomDinners != null && roomDinners.Count > 0) + { + context.RoomDinners.RemoveRange(roomDinners.Where(rec => !model.RoomDinners.ContainsKey(rec.DinnerId))); + context.SaveChanges(); + + foreach (var updateDinner in roomDinners) + { + model.RoomDinners.Remove(updateDinner.DinnerId); + } + context.SaveChanges(); + } + + var room = context.Rooms.First(x => x.Id == Id); + + foreach (var cm in model.RoomDinners) + { + context.RoomDinners.Add(new RoomDinner + { + Room = room, + Dinner = context.Dinners.First(x => x.Id == cm.Key), + }); + context.SaveChanges(); + } + _roomDinners = null; + } } } diff --git a/Hotel/HotelDataModels/Models/IParticipantModel.cs b/Hotel/HotelDataModels/Models/IParticipantModel.cs index 87d7374..9f0bc6d 100644 --- a/Hotel/HotelDataModels/Models/IParticipantModel.cs +++ b/Hotel/HotelDataModels/Models/IParticipantModel.cs @@ -2,7 +2,7 @@ { public interface IParticipantModel : IId { - string FIO { get; } + string ParticipantFIO { get; } string Number { get; } int OrganiserId { get; } } From c74097ff9810a0c38223dce13041636a65b578ba Mon Sep 17 00:00:00 2001 From: Salikh Date: Sun, 28 Apr 2024 18:40:56 +0400 Subject: [PATCH 2/2] add conferencebooking --- .../ConferenceBookingSearchModel.cs | 2 + ...iterStorage.cs => AdministratorStorage.cs} | 2 +- .../Implements/ConferenceBookingStorage.cs | 167 ++++++++++++++++++ .../Implements/DinnerStorage.cs | 2 +- .../Models/ConferenceBooking.cs | 30 ++++ Hotel/HotelRestApi/Program.cs | 2 +- 6 files changed, 202 insertions(+), 3 deletions(-) rename Hotel/HotelDataBaseImplement/Implements/{HeadwaiterStorage.cs => AdministratorStorage.cs} (98%) create mode 100644 Hotel/HotelDataBaseImplement/Implements/ConferenceBookingStorage.cs diff --git a/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs b/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs index 1bf44b0..aaa8342 100644 --- a/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs @@ -13,5 +13,7 @@ namespace HotelContracts.SearchModels public int? ConferenceId { get; set; } public string? PlaceСonference { get; set; } public DateTime? DateСonference { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } } } diff --git a/Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs b/Hotel/HotelDataBaseImplement/Implements/AdministratorStorage.cs similarity index 98% rename from Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs rename to Hotel/HotelDataBaseImplement/Implements/AdministratorStorage.cs index 22bbcf7..1f9ffb8 100644 --- a/Hotel/HotelDataBaseImplement/Implements/HeadwaiterStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implements/AdministratorStorage.cs @@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore; namespace HotelDataBaseImplement.Implements { - public class HeadwaiterStorage : IAdministratorStorage + public class AdministratorStorage : IAdministratorStorage { public List GetFullList() { diff --git a/Hotel/HotelDataBaseImplement/Implements/ConferenceBookingStorage.cs b/Hotel/HotelDataBaseImplement/Implements/ConferenceBookingStorage.cs new file mode 100644 index 0000000..8316673 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implements/ConferenceBookingStorage.cs @@ -0,0 +1,167 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HotelDataBaseImplement.Implements +{ + public class ConferenceBookingStorage : IConferenceBookingStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Administrator) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ConferenceBookingSearchModel model) + { + if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.AdministratorId.HasValue) + { + return new(); + } + using var context = new HotelDataBase(); + if (model.DateFrom.HasValue) + { + return context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Administrator) + .Where(x => x.DateСonference >= model.DateFrom && x.DateСonference <= model.DateTo && x.AdministratorId == model.AdministratorId) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.AdministratorId.HasValue) + { + return context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Administrator) + .Where(x => x.AdministratorId == model.AdministratorId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Administrator) + .Where(x => x.PlaceСonference.Contains(model.PlaceСonference)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ConferenceBookingViewModel? GetElement(ConferenceBookingSearchModel model) + { + if (string.IsNullOrEmpty(model.PlaceСonference) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Administrator) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.PlaceСonference) && x.PlaceСonference == model.PlaceСonference) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public ConferenceBookingViewModel? Insert(ConferenceBookingBindingModel model) + { + using var context = new HotelDataBase(); + var newConferenceBooking = ConferenceBooking.Create(context, model); + + if (newConferenceBooking == null) + { + return null; + } + + context.ConferenceBookings.Add(newConferenceBooking); + context.SaveChanges(); + + return context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.RoomDinners) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Administrator) + .FirstOrDefault(x => x.Id == newConferenceBooking.Id) + ?.GetViewModel; + } + + public ConferenceBookingViewModel? Update(ConferenceBookingBindingModel model) + { + using var context = new HotelDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var elem = context.ConferenceBookings + .Include(x => x.Dinners) + .ThenInclude(x => x.Dinner) + .ThenInclude(x => x.ConferenceBookingDinner) + .ThenInclude(x => x.ConferenceBooking) + .ThenInclude(x => x.Conference) + .FirstOrDefault(rec => rec.Id == model.Id); if (elem == null) + { + return null; + } + elem.Update(model); + context.SaveChanges(); + if (model.ConferenceBookingDinners != null) + elem.UpdateDinners(context, model); + transaction.Commit(); + return elem.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ConferenceBookingViewModel? Delete(ConferenceBookingBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.ConferenceBookings + .Include(x => x.Dinners) + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.ConferenceBookings.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs b/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs index 255891c..1653352 100644 --- a/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implements/DinnerStorage.cs @@ -30,7 +30,7 @@ namespace HotelDataBaseImplement.Implements public List GetFilteredList(DinnerSearchModel model) { - if (string.IsNullOrEmpty(model.DinnerName) && !model.HeadwaiterId.HasValue) + if (string.IsNullOrEmpty(model.DinnerName) && !model.AdministratorId.HasValue) { return new(); } diff --git a/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs index 6c382b2..d675a70 100644 --- a/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs @@ -70,5 +70,35 @@ namespace HotelDataBaseImplement.Models DateСonference = DateСonference, ConferenceBookingDinners = ConferenceBookingDinners }; + + public void UpdateDinners(HotelDataBase context, ConferenceBookingBindingModel model) + { + var conferenceBookingDinners = context.ConferenceBookingDinners.Where(rec => rec.ConferenceBookingId == model.Id).ToList(); + + if (conferenceBookingDinners != null && conferenceBookingDinners.Count > 0) + { + context.ConferenceBookingDinners.RemoveRange(conferenceBookingDinners.Where(rec => !model.ConferenceBookingDinners.ContainsKey(rec.DinnerId))); + context.SaveChanges(); + + foreach (var updateDinner in conferenceBookingDinners) + { + model.ConferenceBookingDinners.Remove(updateDinner.DinnerId); + } + context.SaveChanges(); + } + + var conferenceBooking = context.ConferenceBookings.First(x => x.Id == Id); + + foreach (var cm in model.ConferenceBookingDinners) + { + context.ConferenceBookingDinners.Add(new ConferenceBookingDinner + { + ConferenceBooking = conferenceBooking, + Dinner = context.Dinners.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _conferenceBookingDinners = null; + } } } diff --git a/Hotel/HotelRestApi/Program.cs b/Hotel/HotelRestApi/Program.cs index c0d01e1..e766aac 100644 --- a/Hotel/HotelRestApi/Program.cs +++ b/Hotel/HotelRestApi/Program.cs @@ -1,7 +1,7 @@ using HotelBusinessLogic.BusinessLogic; using HotelContracts.BusinessLogicsContracts; using HotelContracts.StoragesContracts; -using HotelDataBaseImplement.Implemets; +using HotelDataBaseImplement.Implements; using Microsoft.OpenApi.Models; var builder = WebApplication.CreateBuilder(args);