Skip to content

Commit

Permalink
filter deals based on start and end dates (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
eebbesen authored Dec 16, 2024
1 parent b161eba commit 984a66b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
9 changes: 9 additions & 0 deletions StpFoodBlazor/StpFoodBlazor/Helpers/DealFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public DealEvent[] Filter() {

DealEvent[] filteredDeals = Deals;

filteredDeals = FilterByEndAndStartDates(filteredDeals);

if (!string.IsNullOrWhiteSpace(Day))
{
filteredDeals = FilterByDay(filteredDeals, Day);
Expand Down Expand Up @@ -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();
}
}
}
4 changes: 4 additions & 0 deletions StpFoodBlazor/StpFoodBlazor/Models/Deal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}
36 changes: 36 additions & 0 deletions StpFoodBlazor/StpFoodBlazorTest/Helpers/DealFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DealEvent[]> getDeals() {
return await new TestDealService().GetDealsAsync();
}
Expand Down

0 comments on commit 984a66b

Please sign in to comment.