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.Diagnostics.Metrics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HotelDataBaseImplement.Implements { public class ParticipantStorage : IParticipantStorage { public List GetFullList() { using var context = new HotelDataBase(); return context.Participants .Include(x => x.ConferenceParticipants) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanParticipants) .ThenInclude(x => x.MealPlan) .Include(x => x.Organiser) .Select(x => x.GetViewModel) .ToList(); } public List GetFilteredList(ParticipantSearchModel model) { if (string.IsNullOrEmpty(model.ParticipantFIO) && !model.OrganiserId.HasValue) { return new(); } using var context = new HotelDataBase(); if (model.OrganiserId.HasValue) { return context.Participants .Include(x => x.ConferenceParticipants) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanParticipants) .ThenInclude(x => x.MealPlan) .Include(x => x.Organiser) .Where(x => x.OrganiserId == model.OrganiserId) .Select(x => x.GetViewModel) .ToList(); } return context.Participants .Include(x => x.ConferenceParticipants) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanParticipants) .ThenInclude(x => x.MealPlan) .Include(x => x.Organiser) .Where(x => x.ParticipantFIO.Contains(model.ParticipantFIO)) .Select(x => x.GetViewModel) .ToList(); } public ParticipantViewModel? GetElement(ParticipantSearchModel model) { if (string.IsNullOrEmpty(model.ParticipantFIO) && !model.Id.HasValue) { return null; } using var context = new HotelDataBase(); return context.Participants .Include(x => x.ConferenceParticipants) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanParticipants) .ThenInclude(x => x.MealPlan) .Include(x => x.Organiser) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ParticipantFIO) && x.ParticipantFIO == model.ParticipantFIO) || (model.Id.HasValue && x.Id == model.Id))? .GetViewModel; } public ParticipantViewModel? Insert(ParticipantBindingModel model) { using var context = new HotelDataBase(); var newParticipant = Participant.Create(model); if (newParticipant == null) { return null; } context.Participants.Add(newParticipant); context.SaveChanges(); return newParticipant.GetViewModel; } public ParticipantViewModel? Update(ParticipantBindingModel model) { using var context = new HotelDataBase(); var participant = context.Participants.FirstOrDefault(x => x.Id == model.Id); if (participant == null) { return null; } participant.Update(model); context.SaveChanges(); return participant.GetViewModel; } public ParticipantViewModel? Delete(ParticipantBindingModel model) { using var context = new HotelDataBase(); var element = context.Participants.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Participants.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } } }