diff --git a/Hotel/HotelDataBaseImplement/Implemets/MemberStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/MemberStorage.cs index 0801533..1f05076 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/MemberStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/MemberStorage.cs @@ -2,6 +2,8 @@ using HotelContracts.SearchModels; using HotelContracts.StoragesContracts; using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace HotelDataBaseImplement.Implemets { @@ -9,32 +11,105 @@ namespace HotelDataBaseImplement.Implemets { public MemberViewModel? Delete(MemberBindingModel model) { - throw new NotImplementedException(); + 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) { - throw new NotImplementedException(); + 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) { - throw new NotImplementedException(); + 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() { - throw new NotImplementedException(); + 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) { - throw new NotImplementedException(); + 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) { - throw new NotImplementedException(); + 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; } } } diff --git a/Hotel/HotelDataBaseImplement/Implemets/OrganiserStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/OrganiserStorage.cs index 78f96e1..bb85405 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/OrganiserStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/OrganiserStorage.cs @@ -2,6 +2,8 @@ using HotelContracts.SearchModels; using HotelContracts.StoragesContracts; using HotelContracts.ViewModels; +using HotelDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; namespace HotelDataBaseImplement.Implemets { @@ -9,32 +11,112 @@ namespace HotelDataBaseImplement.Implemets { public OrganiserViewModel? Delete(OrganiserBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + var element = context.Organisers.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Organisers.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; } public OrganiserViewModel? GetElement(OrganiserSearchModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + if (model.Id.HasValue) + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Members) + .Include(x => x.Conferences) + .FirstOrDefault(x => x.Id == model.Id)? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.OrganiserLogin) && !string.IsNullOrEmpty(model.OrganiserPassword)) + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Members) + .Include(x => x.Conferences) + .FirstOrDefault(x => x.OrganiserLogin.Equals(model.OrganiserLogin) && x.OrganiserPassword.Equals(model.OrganiserPassword))? + .GetViewModel; + + if (!string.IsNullOrEmpty(model.OrganiserLogin)) + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Members) + .Include(x => x.Conferences) + .FirstOrDefault(x => x.OrganiserLogin.Equals(model.OrganiserLogin))? + .GetViewModel; + + return null; } public List GetFilteredList(OrganiserSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.OrganiserFIO)) + { + return new(); + } + + using var context = new HotelDataBase(); + + return context.Organisers + .Include(x => x.MealPlans) + .Include(x => x.Members) + .Include(x => x.Conferences) + .Where(x => x.OrganiserLogin.Contains(model.OrganiserLogin) && x.OrganiserPassword == model.OrganiserPassword) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + return context.Organisers + .Select(x => x.GetViewModel) + .ToList(); } public OrganiserViewModel? Insert(OrganiserBindingModel model) { - throw new NotImplementedException(); + var newOrganiser = Organiser.Create(model); + + if (newOrganiser == null) + { + return null; + } + + using var context = new HotelDataBase(); + + context.Organisers.Add(newOrganiser); + context.SaveChanges(); + + return newOrganiser.GetViewModel; } public OrganiserViewModel? Update(OrganiserBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + var organiser = context.Organisers + .FirstOrDefault(x => x.Id == model.Id); + + if (organiser == null) + { + return null; + } + + organiser.Update(model); + context.SaveChanges(); + + return organiser.GetViewModel; } } }