add conferencebooking

This commit is contained in:
Salikh 2024-04-28 18:40:56 +04:00
parent a2f46b6b5a
commit c74097ff98
6 changed files with 202 additions and 3 deletions

View File

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

View File

@ -7,7 +7,7 @@ using Microsoft.EntityFrameworkCore;
namespace HotelDataBaseImplement.Implements
{
public class HeadwaiterStorage : IAdministratorStorage
public class AdministratorStorage : IAdministratorStorage
{
public List<AdministratorViewModel> GetFullList()
{

View File

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

View File

@ -30,7 +30,7 @@ namespace HotelDataBaseImplement.Implements
public List<DinnerViewModel> GetFilteredList(DinnerSearchModel model)
{
if (string.IsNullOrEmpty(model.DinnerName) && !model.HeadwaiterId.HasValue)
if (string.IsNullOrEmpty(model.DinnerName) && !model.AdministratorId.HasValue)
{
return new();
}

View File

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

View File

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