diff --git a/Hotel/HotelContracts/BindingModels/ConferenceBookingBindingModel.cs b/Hotel/HotelContracts/BindingModels/ConferenceBookingBindingModel.cs index 901b475..b527ca1 100644 --- a/Hotel/HotelContracts/BindingModels/ConferenceBookingBindingModel.cs +++ b/Hotel/HotelContracts/BindingModels/ConferenceBookingBindingModel.cs @@ -7,5 +7,8 @@ namespace HotelContracts.BindingModels public int ConferenceId { get; set; } public int HeadwaiterId { get; set; } public int Id { get; set; } + public string NameHall { get; set; } = string.Empty; + public DateTime? BookingDate { get; set; } + public Dictionary ConferenceBookingLunches { get; set; } } } diff --git a/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs b/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs index 459b1ce..b418b61 100644 --- a/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs +++ b/Hotel/HotelContracts/BindingModels/RoomBindingModel.cs @@ -10,5 +10,6 @@ namespace HotelContracts.BindingModels public int MealPlanId { get; set; } public int HeadwaiterId { get; set; } public int Id { get; set; } + public Dictionary RoomLunches { get; set; } } } \ No newline at end of file diff --git a/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs b/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs index 4e82981..08b3999 100644 --- a/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/ConferenceBookingSearchModel.cs @@ -9,5 +9,11 @@ namespace HotelContracts.SearchModels public class ConferenceBookingSearchModel { public int? Id { get; set; } + public int? HeadwaiterId { get; set; } + public int? ConferenceId { get; set; } + public DateTime? BookingDate { get; set; } + public string? NameHall { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } } } diff --git a/Hotel/HotelContracts/SearchModels/HeadwaiterSearchModel.cs b/Hotel/HotelContracts/SearchModels/HeadwaiterSearchModel.cs index e32cb9a..06a2587 100644 --- a/Hotel/HotelContracts/SearchModels/HeadwaiterSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/HeadwaiterSearchModel.cs @@ -7,6 +7,7 @@ public string? HeadwaiterPatronymic { get; set; } public string? HeadwaiterLogin { get; set; } public string? HeadwaiterPassword { get; set; } + public string? HeadwaiterEmail { get; set; } public int? Id { get; set; } } } \ No newline at end of file diff --git a/Hotel/HotelContracts/SearchModels/LunchSearchModel.cs b/Hotel/HotelContracts/SearchModels/LunchSearchModel.cs index 4c7df34..3c925fb 100644 --- a/Hotel/HotelContracts/SearchModels/LunchSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/LunchSearchModel.cs @@ -3,6 +3,7 @@ public class LunchSearchModel { public string? LunchName { get; set; } + public int? HeadwaiterId { get; set; } public int? Id { get; set; } } } \ No newline at end of file diff --git a/Hotel/HotelContracts/SearchModels/RoomSearchModel.cs b/Hotel/HotelContracts/SearchModels/RoomSearchModel.cs index de37c0a..f4e9d90 100644 --- a/Hotel/HotelContracts/SearchModels/RoomSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/RoomSearchModel.cs @@ -3,6 +3,10 @@ public class RoomSearchModel { public string? RoomName { get; set; } + public int? HeadwaiterId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } + public int? MealPlanId { get; set; } public int? Id { get; set; } } } \ No newline at end of file diff --git a/Hotel/HotelContracts/ViewModels/ConferenceBookingViewModel.cs b/Hotel/HotelContracts/ViewModels/ConferenceBookingViewModel.cs index 370f3df..f63e473 100644 --- a/Hotel/HotelContracts/ViewModels/ConferenceBookingViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/ConferenceBookingViewModel.cs @@ -1,4 +1,5 @@ using HotelDataModels.Models; +using System.ComponentModel; namespace HotelContracts.ViewModels { @@ -7,5 +8,13 @@ namespace HotelContracts.ViewModels public int ConferenceId { get; set; } public int HeadwaiterId { get; set; } public int Id { get; set; } + + [DisplayName("Дата начала конференции")] + public DateTime? BookingDate { get; set; } + + public string ConfName { get; set; } = string.Empty; + public string NameHall { get; set; } = string.Empty; + + public Dictionary ConferenceBookingLunches { get; set; } } } \ No newline at end of file diff --git a/Hotel/HotelContracts/ViewModels/RoomViewModel.cs b/Hotel/HotelContracts/ViewModels/RoomViewModel.cs index 7e7739c..3df4455 100644 --- a/Hotel/HotelContracts/ViewModels/RoomViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/RoomViewModel.cs @@ -19,5 +19,7 @@ namespace HotelContracts.ViewModels public int MealPlanId { get; set; } public int HeadwaiterId { get; set; } + + public Dictionary RoomLunches { get; set; } } } diff --git a/Hotel/HotelDataBaseImplement/HotelDataBase.cs b/Hotel/HotelDataBaseImplement/HotelDataBase.cs index bcace4d..f906758 100644 --- a/Hotel/HotelDataBaseImplement/HotelDataBase.cs +++ b/Hotel/HotelDataBaseImplement/HotelDataBase.cs @@ -1,5 +1,6 @@ using HotelDataBaseImplement.Models; using Microsoft.EntityFrameworkCore; +using System.Reflection; namespace HotelDataBaseImplement { @@ -9,7 +10,7 @@ namespace HotelDataBaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=ANNZHIMOL\SQLEXPRESS;Initial Catalog=HotelDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-M2G96S06\SQLEXPRESS;Initial Catalog=HotelDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -20,5 +21,11 @@ namespace HotelDataBaseImplement public virtual DbSet Organisers { set; get; } public virtual DbSet ConferenceMembers { set; get; } public virtual DbSet MealPlanMembers { set; get; } + public virtual DbSet ConferenceBookings { set; get; } + public virtual DbSet Lunches { set; get; } + public virtual DbSet ConferenceBookingLunches { set; get; } + public virtual DbSet Rooms { set; get; } + public virtual DbSet RoomLunches { set; get; } + public virtual DbSet Headwaiters { set; get; } } } \ No newline at end of file diff --git a/Hotel/HotelDataBaseImplement/Implemets/ConferenceBookingStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/ConferenceBookingStorage.cs new file mode 100644 index 0000000..8cab8b6 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implemets/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.Implemets +{ + public class ConferenceBookingStorage : IConferenceBookingStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.ConferenceBookings + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Headwaiter) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ConferenceBookingSearchModel model) + { + if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.HeadwaiterId.HasValue) + { + return new(); + } + using var context = new HotelDataBase(); + if (model.DateFrom.HasValue) + { + return context.ConferenceBookings + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Headwaiter) + .Where(x => x.BookingDate >= model.DateFrom && x.BookingDate <= model.DateTo && x.HeadwaiterId == model.HeadwaiterId) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.HeadwaiterId.HasValue) + { + return context.ConferenceBookings + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Headwaiter) + .Where(x => x.HeadwaiterId == model.HeadwaiterId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + return context.ConferenceBookings + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Headwaiter) + .Where(x => x.NameHall.Contains(model.NameHall)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ConferenceBookingViewModel? GetElement(ConferenceBookingSearchModel model) + { + if (string.IsNullOrEmpty(model.NameHall) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.ConferenceBookings + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Headwaiter) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.NameHall) && x.NameHall == model.NameHall) || (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.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.Conference) + .Include(x => x.Headwaiter) + .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.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .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.ConferenceBookingLunches != null) + { + elem.UpdateLunches(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.Lunches).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/Implemets/HeadwaiterStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs new file mode 100644 index 0000000..1add6f7 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implemets/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.Implemets +{ + public class HeadwaiterStorage : IHeadwaiterStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Headwaiters.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(HeadwaiterSearchModel model) + { + if (string.IsNullOrEmpty(model.HeadwaiterSurname) && string.IsNullOrEmpty(model.HeadwaiterName) + && string.IsNullOrEmpty(model.HeadwaiterPatronymic)) + { + return new(); + } + + using var context = new HotelDataBase(); + + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Lunches) + .Include(x => x.Rooms) + .Where(x => x.HeadwaiterLogin.Contains(model.HeadwaiterLogin) && x.HeadwaiterPassword == model.HeadwaiterPassword) + .Select(x => x.GetViewModel) + .ToList(); + } + + public HeadwaiterViewModel? GetElement(HeadwaiterSearchModel model) + { + using var context = new HotelDataBase(); + + if (model.Id.HasValue) + { + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Lunches) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + } + + if (!string.IsNullOrEmpty(model.HeadwaiterEmail) && !string.IsNullOrEmpty(model.HeadwaiterPassword)) + { + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Lunches) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.HeadwaiterEmail.Equals(model.HeadwaiterEmail) && x.HeadwaiterPassword.Equals(model.HeadwaiterPassword))? + .GetViewModel; + } + + if (!string.IsNullOrEmpty(model.HeadwaiterEmail)) + { + return context.Headwaiters + .Include(x => x.ConferenceBookings) + .Include(x => x.Lunches) + .Include(x => x.Rooms) + .FirstOrDefault(x => x.HeadwaiterEmail.Equals(model.HeadwaiterEmail))? + .GetViewModel; + } + + return null; + } + + public HeadwaiterViewModel? Insert(HeadwaiterBindingModel model) + { + var newHeadwaiter = Headwaiter.Create(model); + + if (newHeadwaiter == null) + { + return null; + } + + using var context = new HotelDataBase(); + + context.Headwaiters.Add(newHeadwaiter); + context.SaveChanges(); + + return newHeadwaiter.GetViewModel; + } + + public HeadwaiterViewModel? Update(HeadwaiterBindingModel model) + { + using var context = new HotelDataBase(); + + var headwaiter = context.Headwaiters.FirstOrDefault(x => x.Id == model.Id); + + if (headwaiter == null) + { + return null; + } + + headwaiter.Update(model); + context.SaveChanges(); + + return headwaiter.GetViewModel; + } + + public HeadwaiterViewModel? Delete(HeadwaiterBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Headwaiters.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Headwaiters.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implemets/LunchStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/LunchStorage.cs new file mode 100644 index 0000000..9f162a6 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implemets/LunchStorage.cs @@ -0,0 +1,129 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System.Reflection; + +namespace HotelDataBaseImplement.Implemets +{ + public class LunchStorage : ILunchStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + + return context.Lunches + .Include(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.Headwaiter) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(LunchSearchModel model) + { + if (string.IsNullOrEmpty(model.LunchName) && !model.HeadwaiterId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + + if (model.HeadwaiterId.HasValue) + { + return context.Lunches + .Include(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.Headwaiter) + .Where(x => x.HeadwaiterId == model.HeadwaiterId) + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Lunches + .Include(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.Headwaiter) + .Where(x => x.LunchName.Contains(model.LunchName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public LunchViewModel? GetElement(LunchSearchModel model) + { + if (string.IsNullOrEmpty(model.LunchName) && !model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Lunches + .Include(x => x.RoomLunches) + .ThenInclude(x => x.Room) + .Include(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.LunchName) && x.LunchName == model.LunchName) || (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; + } + + public LunchViewModel? Insert(LunchBindingModel model) + { + using var context = new HotelDataBase(); + + var newLunch = Lunch.Create(model); + + if (newLunch == null) + { + return null; + } + + context.Lunches.Add(newLunch); + context.SaveChanges(); + + return newLunch.GetViewModel; + } + + public LunchViewModel? Update(LunchBindingModel model) + { + using var context = new HotelDataBase(); + + var lunch = context.Lunches.FirstOrDefault(x => x.Id == model.Id); + + if (lunch == null) + { + return null; + } + + lunch.Update(model); + context.SaveChanges(); + + return lunch.GetViewModel; + } + + public LunchViewModel? Delete(LunchBindingModel model) + { + using var context = new HotelDataBase(); + + var element = context.Lunches.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Lunches.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; + } + } +} diff --git a/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs new file mode 100644 index 0000000..5c0d068 --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs @@ -0,0 +1,168 @@ +using HotelContracts.BindingModels; +using HotelContracts.SearchModels; +using HotelContracts.StoragesContracts; +using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HotelDataBaseImplement.Implemets +{ + public class RoomStorage : IRoomStorage + { + public List GetFullList() + { + using var context = new HotelDataBase(); + return context.Rooms + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(RoomSearchModel model) + { + if (!model.Id.HasValue && !model.HeadwaiterId.HasValue) + { + return new(); + } + + using var context = new HotelDataBase(); + + if (model.HeadwaiterId.HasValue) + { + return context.Rooms + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .Where(x => x.HeadwaiterId == model.HeadwaiterId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + return context.Rooms + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .Where(x => x.RoomName.Contains(model.RoomName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public RoomViewModel? GetElement(RoomSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.RoomName)) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Rooms + .Include(x => x.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.RoomName) && x.RoomName == model.RoomName) || (model.Id.HasValue && x.Id == model.Id))? + .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.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .ThenInclude(x => x.ConferenceBooking) + .ThenInclude(x => x.Conference) + .Include(x => x.MealPlan) + .Include(x => x.Headwaiter) + .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.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .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.RoomLunches != null) + { + elem.UpdateLunches(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.Lunches) + .ThenInclude(x => x.Lunch) + .ThenInclude(x => x.ConferenceBookingLunch) + .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/ConferenceBooking.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs index df2f279..17b274a 100644 --- a/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceBooking.cs @@ -1,11 +1,103 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; namespace HotelDataBaseImplement.Models { public class ConferenceBooking : IConferenceBookingModel { - public int ConferenceId => throw new NotImplementedException(); - public int HeadwaiterId => throw new NotImplementedException(); - public int Id => throw new NotImplementedException(); + public int ConferenceId { get; private set; } + public int HeadwaiterId { get; private set; } + public int Id { get; private set; } + public string NameHall { get; set; } = string.Empty; + public DateTime? BookingDate { get; set; } + public virtual Headwaiter Headwaiter { get; set; } + public virtual Conference Conference { get; set; } + + [ForeignKey("ConferenceBookingId")] + public virtual List Lunches { get; set; } + + private Dictionary _conferenceBookingLunches = null; + + [NotMapped] + public Dictionary ConferenceBookingLunches + { + get + { + if (_conferenceBookingLunches == null) + { + using var context = new HotelDataBase(); + _conferenceBookingLunches = Lunches + .ToDictionary(x => x.LunchId, x => (context.Lunches + .FirstOrDefault(y => y.Id == x.LunchId)! as ILunchModel)); + } + return _conferenceBookingLunches; + } + } + + public static ConferenceBooking Create(HotelDataBase context, ConferenceBookingBindingModel model) + { + return new ConferenceBooking() + { + Id = model.Id, + ConferenceId = model.ConferenceId, + HeadwaiterId = model.HeadwaiterId, + NameHall = model.NameHall, + Lunches = model.ConferenceBookingLunches.Select(x => new ConferenceBookingLunch + { + Lunch = context.Lunches.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(ConferenceBookingBindingModel model) + { + ConferenceId = model.ConferenceId; + NameHall = model.NameHall; + BookingDate = model.BookingDate; + } + + public ConferenceBookingViewModel GetViewModel => new() + { + Id = Id, + ConferenceId = ConferenceId, + HeadwaiterId = HeadwaiterId, + NameHall = NameHall, + BookingDate = BookingDate, + ConferenceBookingLunches = ConferenceBookingLunches, + ConfName = Conference?.ConferenceName + }; + + public void UpdateLunches(HotelDataBase context, ConferenceBookingBindingModel model) + { + var conferenceBookingLunches = context.ConferenceBookingLunches.Where(rec => rec.ConferenceBookingId == model.Id).ToList(); + + if (conferenceBookingLunches != null && conferenceBookingLunches.Count > 0) + { + context.ConferenceBookingLunches.RemoveRange(conferenceBookingLunches.Where(rec => !model.ConferenceBookingLunches.ContainsKey(rec.LunchId))); + context.SaveChanges(); + + foreach (var updateLunch in conferenceBookingLunches) + { + model.ConferenceBookingLunches.Remove(updateLunch.LunchId); + } + context.SaveChanges(); + } + + var conferenceBooking = context.ConferenceBookings.First(x => x.Id == Id); + + foreach (var cm in model.ConferenceBookingLunches) + { + context.ConferenceBookingLunches.Add(new ConferenceBookingLunch + { + ConferenceBooking = conferenceBooking, + Lunch = context.Lunches.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _conferenceBookingLunches = null; + } } } +} diff --git a/Hotel/HotelDataBaseImplement/Models/ConferenceBookingLunch.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceBookingLunch.cs index ad781c3..57c4ce1 100644 --- a/Hotel/HotelDataBaseImplement/Models/ConferenceBookingLunch.cs +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceBookingLunch.cs @@ -10,7 +10,7 @@ namespace HotelDataBaseImplement.Models [Required] public int Count { get; set; } - public virtual ConferenceBooking ConferenceBookings { get; set; } - public virtual Lunch Lunches { get; set; } + public virtual ConferenceBooking ConferenceBooking { get; set; } + public virtual Lunch Lunch { get; set; } } } diff --git a/Hotel/HotelDataBaseImplement/Models/Lunch.cs b/Hotel/HotelDataBaseImplement/Models/Lunch.cs index 83ed1ec..981c2c3 100644 --- a/Hotel/HotelDataBaseImplement/Models/Lunch.cs +++ b/Hotel/HotelDataBaseImplement/Models/Lunch.cs @@ -17,13 +17,13 @@ namespace HotelDataBaseImplement.Models [Required] public double LunchPrice { get; set; } - public virtual Headwaiter Headwaiters { get; set; } + public virtual Headwaiter Headwaiter { get; set; } [ForeignKey("LunchId")] public virtual List RoomLunches { get; set; } = new(); [ForeignKey("LunchId")] - public virtual List ConferenceBookingLunches { get; set; } = new(); + public virtual List ConferenceBookingLunch { get; set; } = new(); public static Lunch? Create(LunchBindingModel model) { diff --git a/Hotel/HotelDataBaseImplement/Models/Room.cs b/Hotel/HotelDataBaseImplement/Models/Room.cs index ff727b9..4aa5582 100644 --- a/Hotel/HotelDataBaseImplement/Models/Room.cs +++ b/Hotel/HotelDataBaseImplement/Models/Room.cs @@ -1,14 +1,116 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HotelDataBaseImplement.Models { public class Room : IRoomModel { - public string RoomName => throw new NotImplementedException(); - public string RoomFrame => throw new NotImplementedException(); - public double RoomPrice => throw new NotImplementedException(); - public int HeadwaiterId => throw new NotImplementedException(); - public int MealPlanId => throw new NotImplementedException(); - public int Id => throw new NotImplementedException(); + public int Id { get; private set; } + + [Required] + public string RoomName { get; set; } = string.Empty; + + [Required] + public string RoomFrame { get; set; } = string.Empty; + + [Required] + public double RoomPrice { get; set; } + + public int MealPlanId { get; private set; } + public int HeadwaiterId { get; private set; } + + public virtual Headwaiter? Headwaiter { get; set; } + public virtual MealPlan? MealPlan { get; set; } + + [ForeignKey("RoomId")] + public virtual List Lunches { get; set; } + + private Dictionary _roomLunches = null; + + [NotMapped] + public Dictionary RoomLunches + { + get + { + if (_roomLunches == null) + { + using var context = new HotelDataBase(); + _roomLunches = Lunches + .ToDictionary(x => x.LunchId, x => (context.Lunches + .FirstOrDefault(y => y.Id == x.LunchId)! as ILunchModel)); + } + return _roomLunches; + } + } + + public static Room Create(HotelDataBase context, RoomBindingModel model) + { + return new Room() + { + Id = model.Id, + RoomName = model.RoomName, + RoomFrame = model.RoomFrame, + RoomPrice = model.RoomPrice, + HeadwaiterId = model.HeadwaiterId, + MealPlanId = model.MealPlanId, + Lunches = model.RoomLunches.Select(x => new RoomLunch + { + Lunch = context.Lunches.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(RoomBindingModel model) + { + RoomName = model.RoomName; + RoomFrame = model.RoomFrame; + RoomPrice = model.RoomPrice; + HeadwaiterId = model.HeadwaiterId; + MealPlanId = model.MealPlanId; + } + + public RoomViewModel GetViewModel => new() + { + Id = Id, + RoomName = RoomName, + RoomFrame = RoomFrame, + HeadwaiterId = HeadwaiterId, + MealPlanId = MealPlanId, + RoomPrice = RoomPrice, + RoomLunches = RoomLunches + }; + + public void UpdateLunches(HotelDataBase context, RoomBindingModel model) + { + var roomLunches = context.RoomLunches.Where(rec => rec.RoomId == model.Id).ToList(); + + if (roomLunches != null) + { + context.RoomLunches.RemoveRange(roomLunches.Where(rec => !model.RoomLunches.ContainsKey(rec.LunchId))); + context.SaveChanges(); + + foreach (var updateLunch in roomLunches) + { + model.RoomLunches.Remove(updateLunch.LunchId); + } + context.SaveChanges(); + } + + var room = context.Rooms.First(x => x.Id == Id); + + foreach (var cm in model.RoomLunches) + { + context.RoomLunches.Add(new RoomLunch + { + Room = room, + Lunch = context.Lunches.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _roomLunches = null; + } } } diff --git a/Hotel/HotelDataBaseImplement/Models/RoomLunch.cs b/Hotel/HotelDataBaseImplement/Models/RoomLunch.cs index a7623b5..ad2c3db 100644 --- a/Hotel/HotelDataBaseImplement/Models/RoomLunch.cs +++ b/Hotel/HotelDataBaseImplement/Models/RoomLunch.cs @@ -10,7 +10,7 @@ namespace HotelDataBaseImplement.Models [Required] public int Count { get; set; } - public virtual Room Rooms { get; set; } - public virtual Lunch Lunches { get; set; } + public virtual Room Room { get; set; } + public virtual Lunch Lunch { get; set; } } } diff --git a/Hotel/HotelDataModels/Models/IConferenceBookingModel.cs b/Hotel/HotelDataModels/Models/IConferenceBookingModel.cs index c74edc0..bcdc9f3 100644 --- a/Hotel/HotelDataModels/Models/IConferenceBookingModel.cs +++ b/Hotel/HotelDataModels/Models/IConferenceBookingModel.cs @@ -4,5 +4,7 @@ { int ConferenceId { get; } int HeadwaiterId { get; } + DateTime? BookingDate { get; } + public Dictionary ConferenceBookingLunches { get; } } } \ No newline at end of file diff --git a/Hotel/HotelDataModels/Models/IRoomModel.cs b/Hotel/HotelDataModels/Models/IRoomModel.cs index df0fd5f..7cb64ce 100644 --- a/Hotel/HotelDataModels/Models/IRoomModel.cs +++ b/Hotel/HotelDataModels/Models/IRoomModel.cs @@ -7,5 +7,6 @@ double RoomPrice { get; } int MealPlanId { get; } int HeadwaiterId { get; } + public Dictionary RoomLunches { get; } } } \ No newline at end of file