Промежуточный слой БД + правки
This commit is contained in:
parent
0be5169069
commit
d33d2f1724
@ -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<int, ILunchModel> ConferenceBookingLunches { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -10,5 +10,6 @@ namespace HotelContracts.BindingModels
|
||||
public int MealPlanId { get; set; }
|
||||
public int HeadwaiterId { get; set; }
|
||||
public int Id { get; set; }
|
||||
public Dictionary<int, ILunchModel> RoomLunches { get; set; }
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
public class LunchSearchModel
|
||||
{
|
||||
public string? LunchName { get; set; }
|
||||
public int? HeadwaiterId { get; set; }
|
||||
public int? Id { get; set; }
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
}
|
@ -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<int, ILunchModel> ConferenceBookingLunches { get; set; }
|
||||
}
|
||||
}
|
@ -19,5 +19,7 @@ namespace HotelContracts.ViewModels
|
||||
public int MealPlanId { get; set; }
|
||||
|
||||
public int HeadwaiterId { get; set; }
|
||||
|
||||
public Dictionary<int, ILunchModel> RoomLunches { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -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<Organiser> Organisers { set; get; }
|
||||
public virtual DbSet<ConferenceMember> ConferenceMembers { set; get; }
|
||||
public virtual DbSet<MealPlanMember> MealPlanMembers { set; get; }
|
||||
public virtual DbSet<ConferenceBooking> ConferenceBookings { set; get; }
|
||||
public virtual DbSet<Lunch> Lunches { set; get; }
|
||||
public virtual DbSet<ConferenceBookingLunch> ConferenceBookingLunches { set; get; }
|
||||
public virtual DbSet<Room> Rooms { set; get; }
|
||||
public virtual DbSet<RoomLunch> RoomLunches { set; get; }
|
||||
public virtual DbSet<Headwaiter> Headwaiters { set; get; }
|
||||
}
|
||||
}
|
@ -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<ConferenceBookingViewModel> 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<ConferenceBookingViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
126
Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs
Normal file
126
Hotel/HotelDataBaseImplement/Implemets/HeadwaiterStorage.cs
Normal file
@ -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<HeadwaiterViewModel> GetFullList()
|
||||
{
|
||||
using var context = new HotelDataBase();
|
||||
|
||||
return context.Headwaiters.Select(x => x.GetViewModel).ToList();
|
||||
}
|
||||
|
||||
public List<HeadwaiterViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
129
Hotel/HotelDataBaseImplement/Implemets/LunchStorage.cs
Normal file
129
Hotel/HotelDataBaseImplement/Implemets/LunchStorage.cs
Normal file
@ -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<LunchViewModel> 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<LunchViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
168
Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs
Normal file
168
Hotel/HotelDataBaseImplement/Implemets/RoomStorage.cs
Normal file
@ -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<RoomViewModel> 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<RoomViewModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -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<ConferenceBookingLunch> Lunches { get; set; }
|
||||
|
||||
private Dictionary<int, ILunchModel> _conferenceBookingLunches = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, ILunchModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -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<RoomLunch> RoomLunches { get; set; } = new();
|
||||
|
||||
[ForeignKey("LunchId")]
|
||||
public virtual List<ConferenceBookingLunch> ConferenceBookingLunches { get; set; } = new();
|
||||
public virtual List<ConferenceBookingLunch> ConferenceBookingLunch { get; set; } = new();
|
||||
|
||||
public static Lunch? Create(LunchBindingModel model)
|
||||
{
|
||||
|
@ -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<RoomLunch> Lunches { get; set; }
|
||||
|
||||
private Dictionary<int, ILunchModel> _roomLunches = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, ILunchModel> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
}
|
||||
}
|
||||
|
@ -4,5 +4,7 @@
|
||||
{
|
||||
int ConferenceId { get; }
|
||||
int HeadwaiterId { get; }
|
||||
DateTime? BookingDate { get; }
|
||||
public Dictionary<int, ILunchModel> ConferenceBookingLunches { get; }
|
||||
}
|
||||
}
|
@ -7,5 +7,6 @@
|
||||
double RoomPrice { get; }
|
||||
int MealPlanId { get; }
|
||||
int HeadwaiterId { get; }
|
||||
public Dictionary<int, ILunchModel> RoomLunches { get; }
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user