diff --git a/Hotel/HotelContracts/SearchModels/ConferenceSearchModel.cs b/Hotel/HotelContracts/SearchModels/ConferenceSearchModel.cs index a27f1d2..216a781 100644 --- a/Hotel/HotelContracts/SearchModels/ConferenceSearchModel.cs +++ b/Hotel/HotelContracts/SearchModels/ConferenceSearchModel.cs @@ -4,5 +4,8 @@ { public int? Id { get; set; } public string? ConferenceName { get; set; } + public int? OrganiserId { get; set; } + public DateTime? DateFrom { get; set; } + public DateTime? DateTo { get; set; } } } diff --git a/Hotel/HotelDataBaseImplement/Implemets/ConferenceStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/ConferenceStorage.cs index 1be587d..0b7c0bb 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/ConferenceStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/ConferenceStorage.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,149 @@ namespace HotelDataBaseImplement.Implemets { public ConferenceViewModel? Delete(ConferenceBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + var element = context.Conferences + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Conferences.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; } public ConferenceViewModel? GetElement(ConferenceSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; } public List GetFilteredList(ConferenceSearchModel model) { - throw new NotImplementedException(); + if (!model.DateFrom.HasValue && !model.DateTo.HasValue && !model.OrganiserId.HasValue) + { + return new(); + } + using var context = new HotelDataBase(); + + if (model.DateFrom.HasValue) + { + return context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .Where(x => x.StartDate >= model.DateFrom && x.StartDate <= model.DateTo) + .Select(x => x.GetViewModel) + .ToList(); + } + else if (model.OrganiserId.HasValue) + return context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .Where(x => x.OrganiserId == model.OrganiserId) + .Select(x => x.GetViewModel) + .ToList(); + + return context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .Where(x => x.Id == model.Id) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + return context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .Select(x => x.GetViewModel) + .ToList(); } public ConferenceViewModel? Insert(ConferenceBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + var newConference = Conference.Create(context,model); + + if (newConference == null) + { + return null; + } + + context.Conferences.Add(newConference); + context.SaveChanges(); + + return context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .FirstOrDefault(x => x.Id == newConference.Id) + ?.GetViewModel; } + public ConferenceViewModel? Update(ConferenceBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + var order = context.Conferences + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.MealPlanMember) + .ThenInclude(x => x.MealPlan) + .Include(x => x.ConferenceBookings) + .Include(x => x.Organiser) + .FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return order.GetViewModel; } } } diff --git a/Hotel/HotelDataBaseImplement/Implemets/MealPlanStorage.cs b/Hotel/HotelDataBaseImplement/Implemets/MealPlanStorage.cs index e5fe4fc..43f1278 100644 --- a/Hotel/HotelDataBaseImplement/Implemets/MealPlanStorage.cs +++ b/Hotel/HotelDataBaseImplement/Implemets/MealPlanStorage.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,103 @@ namespace HotelDataBaseImplement.Implemets { public MealPlanViewModel? Delete(MealPlanBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + + var element = context.MealPlans + .FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.MealPlans.Remove(element); + context.SaveChanges(); + + return element.GetViewModel; + } + + return null; } public MealPlanViewModel? GetElement(MealPlanSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return null; + } + + using var context = new HotelDataBase(); + + return context.MealPlans + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.ConferenceMember) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .Include(x => x.Organiser) + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))? + .GetViewModel; } public List GetFilteredList(MealPlanSearchModel model) { - throw new NotImplementedException(); + if (string.IsNullOrEmpty(model.MealPlanName)) + { + return new(); + } + + using var context = new HotelDataBase(); + + return context.MealPlans + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.ConferenceMember) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .Include(x => x.Organiser) + .Where(x => x.MealPlanName.Contains(model.MealPlanName)) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFullList() { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + return context.MealPlans + .Include(x => x.Members) + .ThenInclude(x => x.Member) + .ThenInclude(x => x.ConferenceMember) + .ThenInclude(x => x.Conference) + .Include(x => x.Rooms) + .Include(x => x.Organiser) + .Select(x => x.GetViewModel) + .ToList(); } public MealPlanViewModel? Insert(MealPlanBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + var newMealPlan = MealPlan.Create(context,model); + + if (newMealPlan == null) + { + return null; + } + + context.MealPlans.Add(newMealPlan); + context.SaveChanges(); + return newMealPlan.GetViewModel; } public MealPlanViewModel? Update(MealPlanBindingModel model) { - throw new NotImplementedException(); + using var context = new HotelDataBase(); + var mealPlan = context.MealPlans.FirstOrDefault(x => x.Id == model.Id); + if (mealPlan == null) + { + return null; + } + mealPlan.Update(model); + context.SaveChanges(); + return mealPlan.GetViewModel; } } } diff --git a/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs index 1c3d5de..dde204b 100644 --- a/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs @@ -12,7 +12,7 @@ namespace HotelDataBaseImplement.Models [Required] public int ConferenceId { get; set; } - public virtual Conference Conference { get; set; } = new(); - public virtual Member Member { get; set; } = new(); + public virtual Conference Conference { get; set; } + public virtual Member Member { get; set; } } } diff --git a/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs b/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs index 4865f6a..35939df 100644 --- a/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs +++ b/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs @@ -12,7 +12,7 @@ namespace HotelDataBaseImplement.Models [Required] public int MealPlanId { get; set; } - public virtual MealPlan MealPlan { get; set; } = new(); - public virtual Member Member { get; set; } = new(); + public virtual MealPlan MealPlan { get; set; } + public virtual Member Member { get; set; } } }