using HotelContracts.BindingModels; using HotelContracts.SearchModels; using HotelContracts.StoragesContracts; using HotelContracts.ViewModels; using HotelDataBaseImplement.Models; using Microsoft.EntityFrameworkCore; namespace HotelDataBaseImplement.Implemets { public class MemberStorage : IMemberStorage { public MemberViewModel? Delete(MemberBindingModel model) { using var context = new HotelDataBase(); var element = context.Members.FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Members.Remove(element); context.SaveChanges(); return element.GetViewModel; } return null; } public MemberViewModel? GetElement(MemberSearchModel model) { if (string.IsNullOrEmpty(model.MemberFIO) && !model.Id.HasValue) { return null; } using var context = new HotelDataBase(); return context.Members .Include(x => x.ConferenceMember) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanMember) .ThenInclude(x => x.MealPlan) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.MemberFIO) && x.MemberFIO == model.MemberFIO) || (model.Id.HasValue && x.Id == model.Id))? .GetViewModel; } public List GetFilteredList(MemberSearchModel model) { if (string.IsNullOrEmpty(model.MemberFIO)) { return new(); } using var context = new HotelDataBase(); return context.Members .Include(x => x.ConferenceMember) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanMember) .ThenInclude(x => x.MealPlan) .Where(x => x.MemberFIO.Contains(model.MemberFIO)) .ToList() .Select(x => x.GetViewModel) .ToList(); } public List GetFullList() { using var context = new HotelDataBase(); return context.Members .Include(x => x.ConferenceMember) .ThenInclude(x => x.Conference) .Include(x => x.MealPlanMember) .ThenInclude(x => x.MealPlan) .ToList() .Select(x => x.GetViewModel) .ToList(); } public MemberViewModel? Insert(MemberBindingModel model) { using var context = new HotelDataBase(); var newMember = Member.Create(model); if (newMember == null) { return null; } context.Members.Add(newMember); context.SaveChanges(); return newMember.GetViewModel; } public MemberViewModel? Update(MemberBindingModel model) { using var context = new HotelDataBase(); var member = context.Members.FirstOrDefault(x => x.Id == model.Id); if (member == null) { return null; } member.Update(model); context.SaveChanges(); return member.GetViewModel; } } }