CourseWork_Hotel/Hotel/HotelDataBaseImplement/Implemets/MemberStorage.cs

119 lines
3.5 KiB
C#
Raw Normal View History

using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
2023-04-05 13:08:35 +04:00
using HotelDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HotelDataBaseImplement.Implemets
{
public class MemberStorage : IMemberStorage
{
public MemberViewModel? Delete(MemberBindingModel model)
{
2023-04-05 13:08:35 +04:00
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)
{
2023-04-05 13:08:35 +04:00
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)
2023-04-05 23:40:29 +04:00
.Include(x => x.Organiser)
2023-04-05 13:08:35 +04:00
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.MemberFIO) && x.MemberFIO == model.MemberFIO) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<MemberViewModel> GetFilteredList(MemberSearchModel model)
{
2023-04-05 13:08:35 +04:00
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)
2023-04-05 23:40:29 +04:00
.Include(x => x.Organiser)
2023-04-05 13:08:35 +04:00
.Where(x => x.MemberFIO.Contains(model.MemberFIO))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<MemberViewModel> GetFullList()
{
2023-04-05 13:08:35 +04:00
using var context = new HotelDataBase();
return context.Members
.Include(x => x.ConferenceMember)
.ThenInclude(x => x.Conference)
.Include(x => x.MealPlanMember)
.ThenInclude(x => x.MealPlan)
2023-04-05 23:40:29 +04:00
.Include(x => x.Organiser)
2023-04-05 13:08:35 +04:00
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public MemberViewModel? Insert(MemberBindingModel model)
{
2023-04-05 13:08:35 +04:00
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)
{
2023-04-05 13:08:35 +04:00
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;
}
}
}