156 lines
4.1 KiB
C#
Raw Normal View History

2024-04-28 18:11:56 +04:00
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 MealPlanStorage : IMealPlanStorage
{
public List<MealPlanViewModel> GetFullList()
{
using var context = new HotelDataBase();
return context.MealPlans
.Include(x => x.Participants)
.ThenInclude(x => x.Participant)
.ThenInclude(x => x.ConferenceParticipants)
.ThenInclude(x => x.Conference)
.Include(x => x.Rooms)
.Include(x => x.Organiser)
.Select(x => x.GetViewModel)
.ToList();
}
public List<MealPlanViewModel> GetFilteredList(MealPlanSearchModel model)
{
if (string.IsNullOrEmpty(model.MealPlanName) && !model.OrganiserId.HasValue)
{
return new();
}
using var context = new HotelDataBase();
if (model.OrganiserId.HasValue)
{
return context.MealPlans
.Include(x => x.Participants)
.ThenInclude(x => x.Participant)
.ThenInclude(x => x.ConferenceParticipants)
.ThenInclude(x => x.Conference)
.Include(x => x.Rooms)
.Include(x => x.Organiser)
.Where(x => x.OrganiserId == model.OrganiserId)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
return context.MealPlans
.Include(x => x.Participants)
.ThenInclude(x => x.Participant)
.ThenInclude(x => x.ConferenceParticipants)
.ThenInclude(x => x.Conference)
.Include(x => x.Rooms)
.Include(x => x.Organiser)
.Where(x => x.MealPlanName.Contains(model.MealPlanName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public MealPlanViewModel? GetElement(MealPlanSearchModel model)
{
if (string.IsNullOrEmpty(model.MealPlanName) && !model.Id.HasValue)
{
return null;
}
using var context = new HotelDataBase();
return context.MealPlans
.Include(x => x.Participants)
.ThenInclude(x => x.Participant)
.ThenInclude(x => x.ConferenceParticipants)
.ThenInclude(x => x.Conference)
.Include(x => x.Rooms)
.Include(x => x.Organiser)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.MealPlanName) && x.MealPlanName == model.MealPlanName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
2024-05-01 17:00:04 +04:00
public MealPlanViewModel? Insert(MealPlanBindingModel model)
{
using var context = new HotelDataBase();
var newMealPlan = MealPlan.Create(context, model);
if (newMealPlan == null)
{
return null;
}
context.MealPlans.Add(newMealPlan);
context.SaveChanges();
return context.MealPlans
.Include(x => x.Participants)
.ThenInclude(x => x.Participant)
.ThenInclude(x => x.ConferenceParticipants)
.ThenInclude(x => x.Conference)
.Include(x => x.Rooms)
.Include(x => x.Organiser)
.FirstOrDefault(x => x.Id == newMealPlan.Id)
?.GetViewModel;
}
public MealPlanViewModel? Update(MealPlanBindingModel model)
2024-04-28 18:11:56 +04:00
{
using var context = new HotelDataBase();
using var transaction = context.Database.BeginTransaction();
try
{
var elem = context.MealPlans.FirstOrDefault(rec => rec.Id == model.Id);
if (elem == null)
{
return null;
}
elem.Update(model);
context.SaveChanges();
if (model.MealPlanParticipants != null)
2024-05-01 17:00:04 +04:00
elem.UpdateParticipants(context, model);
2024-04-28 18:11:56 +04:00
transaction.Commit();
return elem.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public MealPlanViewModel? Delete(MealPlanBindingModel model)
{
using var context = new HotelDataBase();
var element = context.MealPlans
.Include(x => x.Participants)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.MealPlans.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}