2023-04-02 14:32:57 +04:00
|
|
|
|
using HotelContracts.BindingModels;
|
|
|
|
|
using HotelContracts.SearchModels;
|
|
|
|
|
using HotelContracts.StoragesContracts;
|
|
|
|
|
using HotelContracts.ViewModels;
|
2023-04-05 23:31:18 +04:00
|
|
|
|
using HotelDataBaseImplement.Models;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
|
|
|
|
namespace HotelDataBaseImplement.Implemets
|
|
|
|
|
{
|
|
|
|
|
public class ConferenceStorage : IConferenceStorage
|
|
|
|
|
{
|
|
|
|
|
public ConferenceViewModel? Delete(ConferenceBindingModel model)
|
|
|
|
|
{
|
2023-04-05 23:31:18 +04:00
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
|
|
|
|
|
var element = context.Conferences
|
2023-05-16 17:41:58 +04:00
|
|
|
|
.Include(x => x.Members)
|
2023-04-05 23:31:18 +04:00
|
|
|
|
.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
|
|
|
|
|
if (element != null)
|
|
|
|
|
{
|
|
|
|
|
context.Conferences.Remove(element);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
|
|
|
|
|
return element.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConferenceViewModel? GetElement(ConferenceSearchModel model)
|
|
|
|
|
{
|
2023-05-16 17:41:58 +04:00
|
|
|
|
if (string.IsNullOrEmpty(model.ConferenceName) && !model.Id.HasValue)
|
2023-04-05 23:31:18 +04:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
|
|
|
|
|
return context.Conferences
|
|
|
|
|
.Include(x => x.Members)
|
|
|
|
|
.ThenInclude(x => x.Member)
|
|
|
|
|
.ThenInclude(x => x.MealPlanMember)
|
|
|
|
|
.ThenInclude(x => x.MealPlan)
|
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Organiser)
|
2023-05-16 17:41:58 +04:00
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ConferenceName) && x.ConferenceName == model.ConferenceName) || (model.Id.HasValue && x.Id == model.Id))?
|
2023-04-05 23:31:18 +04:00
|
|
|
|
.GetViewModel;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ConferenceViewModel> GetFilteredList(ConferenceSearchModel model)
|
|
|
|
|
{
|
2023-04-05 23:31:18 +04:00
|
|
|
|
if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.OrganiserId.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
|
|
|
|
|
if (model.DateFrom.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return context.Conferences
|
|
|
|
|
.Include(x => x.Members)
|
|
|
|
|
.ThenInclude(x => x.Member)
|
|
|
|
|
.ThenInclude(x => x.MealPlanMember)
|
|
|
|
|
.ThenInclude(x => x.MealPlan)
|
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Organiser)
|
|
|
|
|
.Where(x => x.StartDate >= model.DateFrom && x.StartDate <= model.DateTo)
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
else if (model.OrganiserId.HasValue)
|
|
|
|
|
return context.Conferences
|
|
|
|
|
.Include(x => x.Members)
|
|
|
|
|
.ThenInclude(x => x.Member)
|
|
|
|
|
.ThenInclude(x => x.MealPlanMember)
|
|
|
|
|
.ThenInclude(x => x.MealPlan)
|
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Organiser)
|
|
|
|
|
.Where(x => x.OrganiserId == model.OrganiserId)
|
2023-05-16 17:41:58 +04:00
|
|
|
|
.ToList()
|
2023-04-05 23:31:18 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return context.Conferences
|
|
|
|
|
.Include(x => x.Members)
|
|
|
|
|
.ThenInclude(x => x.Member)
|
|
|
|
|
.ThenInclude(x => x.MealPlanMember)
|
|
|
|
|
.ThenInclude(x => x.MealPlan)
|
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Organiser)
|
2023-05-16 17:41:58 +04:00
|
|
|
|
.Where(x => x.ConferenceName.Contains(model.ConferenceName))
|
|
|
|
|
.ToList()
|
2023-04-05 23:31:18 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ConferenceViewModel> GetFullList()
|
|
|
|
|
{
|
2023-04-05 23:31:18 +04:00
|
|
|
|
using var context = new HotelDataBase();
|
|
|
|
|
|
|
|
|
|
return context.Conferences
|
|
|
|
|
.Include(x => x.Members)
|
|
|
|
|
.ThenInclude(x => x.Member)
|
|
|
|
|
.ThenInclude(x => x.MealPlanMember)
|
|
|
|
|
.ThenInclude(x => x.MealPlan)
|
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Organiser)
|
2023-05-16 17:41:58 +04:00
|
|
|
|
.ToList()
|
2023-04-05 23:31:18 +04:00
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ConferenceViewModel? Insert(ConferenceBindingModel model)
|
|
|
|
|
{
|
2023-04-05 23:31:18 +04:00
|
|
|
|
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.Members)
|
|
|
|
|
.ThenInclude(x => x.Member)
|
|
|
|
|
.ThenInclude(x => x.MealPlanMember)
|
|
|
|
|
.ThenInclude(x => x.MealPlan)
|
|
|
|
|
.Include(x => x.ConferenceBookings)
|
|
|
|
|
.Include(x => x.Organiser)
|
|
|
|
|
.FirstOrDefault(x => x.Id == newConference.Id)
|
|
|
|
|
?.GetViewModel;
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
2023-04-05 23:31:18 +04:00
|
|
|
|
|
2023-04-02 14:32:57 +04:00
|
|
|
|
|
|
|
|
|
public ConferenceViewModel? Update(ConferenceBindingModel model)
|
|
|
|
|
{
|
2023-04-05 23:31:18 +04:00
|
|
|
|
using var context = new HotelDataBase();
|
2023-05-16 17:41:58 +04:00
|
|
|
|
using var transaction = context.Database.BeginTransaction();
|
|
|
|
|
try
|
2023-04-05 23:31:18 +04:00
|
|
|
|
{
|
2023-05-16 17:41:58 +04:00
|
|
|
|
var elem = context.Conferences.FirstOrDefault(rec => rec.Id == model.Id);
|
|
|
|
|
if (elem == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
elem.Update(model);
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
if (model.ConferenceMembers != null)
|
|
|
|
|
elem.UpdateMembers(context, model);
|
|
|
|
|
transaction.Commit();
|
|
|
|
|
return elem.GetViewModel;
|
2023-04-05 23:31:18 +04:00
|
|
|
|
}
|
2023-05-16 17:41:58 +04:00
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
transaction.Rollback();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-02 14:32:57 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|