From 984a66b4c881b1fd623612c33ba9044a35d38b60 Mon Sep 17 00:00:00 2001 From: J2EEbbesen Date: Mon, 16 Dec 2024 10:58:05 -0600 Subject: [PATCH] filter deals based on start and end dates (#54) --- .../StpFoodBlazor/Helpers/DealFilter.cs | 9 +++++ StpFoodBlazor/StpFoodBlazor/Models/Deal.cs | 4 +++ .../Helpers/DealFilterTest.cs | 36 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs index 4b8f54c..455ad27 100644 --- a/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs +++ b/StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs @@ -17,6 +17,8 @@ public DealEvent[] Filter() { DealEvent[] filteredDeals = Deals; + filteredDeals = FilterByEndAndStartDates(filteredDeals); + if (!string.IsNullOrWhiteSpace(Day)) { filteredDeals = FilterByDay(filteredDeals, Day); @@ -73,5 +75,12 @@ private static DealEvent[] FilterByHappyHour(DealEvent[] deals, Boolean happyHou return deals.Where(deal => deal.HappyHour == null || deal.HappyHour.Trim() == "").ToArray(); } + + private static DealEvent[] FilterByEndAndStartDates(DealEvent[] deals) { + return deals.Where(deal => + (string.IsNullOrEmpty(deal.End) || DateTime.Parse(deal.End) >= DateTime.Now) && + (string.IsNullOrEmpty(deal.Start) || DateTime.Parse(deal.Start) <= DateTime.Now) + ).ToArray(); + } } } diff --git a/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs b/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs index defedaf..9b82b62 100644 --- a/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs +++ b/StpFoodBlazor/StpFoodBlazor/Models/Deal.cs @@ -12,5 +12,9 @@ public class DealEvent [JsonPropertyName("Happy Hour")] public string? HappyHour { get; set; } + [JsonPropertyName("Start Date")] + public string? Start { get; set; } + [JsonPropertyName("End Date")] + public string? End { get; set; } } } diff --git a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs index 8827ceb..e3746b1 100644 --- a/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs +++ b/StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs @@ -103,6 +103,42 @@ public void ShouldReturnFilteredByHappyHourFalse() { Assert.True(string.IsNullOrWhiteSpace(deal.HappyHour))); } + [Fact] + public void ShouldFilterDealsThatEnded() { + filter.HappyHour = true; + + var yesterday = DateTime.Now.AddDays(-1).ToString("MM/dd/yyyy"); + var dealY = deals[0]; + dealY.End = yesterday; + var tomorrow = DateTime.Now.AddDays(1).ToString("MM/dd/yyyy"); + var dealT = deals[1]; + dealT.End = tomorrow; + filter.Deals = [dealY, dealT]; + + DealEvent[] filteredDeals = filter.Filter(); + + Assert.Single(filteredDeals); + Assert.Equal(tomorrow, filteredDeals[0].End); + } + + [Fact] + public void ShouldFilterDealsThatHaveNotStarted() { + filter.HappyHour = true; + + var today = DateTime.Now.ToString("MM/dd/yyyy"); + var dealToday = deals[0]; + dealToday.Start = today; + var tomorrow = DateTime.Now.AddDays(1).ToString("MM/dd/yyyy"); + var dealTomorrow = deals[1]; + dealTomorrow.Start = tomorrow; + filter.Deals = [dealToday, dealTomorrow]; + + DealEvent[] filteredDeals = filter.Filter(); + + Assert.Single(filteredDeals); + Assert.Equal(today, filteredDeals[0].Start); + } + private static async Task getDeals() { return await new TestDealService().GetDealsAsync(); }