179 lines
5.4 KiB
C#
179 lines
5.4 KiB
C#
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<DinnerViewModel> 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<DinnerViewModel> GetFilteredList(DinnerSearchModel model)
|
|
{
|
|
if (string.IsNullOrEmpty(model.DinnerName) && !model.AdministratorId.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;
|
|
}
|
|
public List<ReportRoomsConferencesViewModel> GetReportRoomsConferences(ReportRoomsConferencesSearchModel model)
|
|
{
|
|
using var context = new HotelDataBase();
|
|
return context.Dinners.Where(dinner => dinner.AdministratorId == model.AdministratorId)
|
|
.Select(x => new ReportRoomsConferencesViewModel()
|
|
{
|
|
DinnerName = x.DinnerName,
|
|
Rooms = context.Rooms
|
|
.Where(room => room.DateCreate <= model.DateTo &&
|
|
room.DateCreate >= model.DateFrom && room.Dinners.Any(m => m.DinnerId == x.Id) && room.AdministratorId == model.AdministratorId)
|
|
.Select(room => room.GetViewModel)
|
|
.ToList(),
|
|
Conferences = context.ConferenceBookings
|
|
.Include(conferencebooking => conferencebooking.Conference)
|
|
.Where(conferencebooking => conferencebooking.Conference != null && conferencebooking.Conference.StartDate <= model.DateTo &&
|
|
conferencebooking.Conference.StartDate >= model.DateFrom && conferencebooking.Dinners.Any(m => m.DinnerId == x.Id) && conferencebooking.AdministratorId == model.AdministratorId)
|
|
.Select(conferencebooking => conferencebooking.Conference.GetViewModel)
|
|
.ToList(),
|
|
})
|
|
.ToList();
|
|
}
|
|
|
|
public List<ReportMealPlansDinnersViewModel> GetReportDinnerMealPlansList(ListMealPlansSearchModel model)
|
|
{
|
|
if (model.dinnersIds == null)
|
|
{
|
|
return new();
|
|
}
|
|
using var context = new HotelDataBase();
|
|
return context.Dinners
|
|
.Where(dinner => model.dinnersIds.Contains(dinner.Id))
|
|
.Select(dinner => new ReportMealPlansDinnersViewModel
|
|
{
|
|
DinnerName = dinner.DinnerName,
|
|
MealPlans = context.MealPlans
|
|
.Include(mealplan => mealplan.Rooms)
|
|
.ThenInclude(room => room.Dinners)
|
|
.Where(mealplan => mealplan.Rooms
|
|
.SelectMany(room => room.Dinners)
|
|
.Any(rd => rd.DinnerId == dinner.Id))
|
|
.Select(mealplan => mealplan.GetViewModel)
|
|
.ToList()
|
|
})
|
|
.ToList();
|
|
}
|
|
}
|
|
}
|