I have method which checks period and return schedules:
public IEnumerable<EventSchedule> GetSchedulesForPeriod(PeriodEvent period, string tab = "")
{
switch (period)
{
case PeriodEvent.Today:
return GetTodaySchedules(tab);
case PeriodEvent.Tomorrow:
return GetTomorrowSchedules(tab);
case PeriodEvent.Week:
return GetWeekSchedules(tab);
case PeriodEvent.FewWeek:
return GetFewWeekSchedules(tab);
case PeriodEvent.Month:
return GetMonthSchedules(tab);
case PeriodEvent.All:
return GetAllFromTodaySchedules(tab);
default:
return GetTodaySchedules(tab);
}
}
Consider the GetTodaySchedules
and GetTomorrowSchedules
:
private IEnumerable<EventSchedule> GetTodaySchedules(string tab)
{
var today = DateTime.Now.Date;
var result = Database.EventSchedules.Where(s => s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceEnd.Value.Date >= today &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(
c => c.EventCategory.Name == tab)).ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}
private IEnumerable<EventSchedule> GetTomorrowSchedules(string tab)
{
var today = DateTime.Now.AddDays(1).Date;
var result = Database.EventSchedules.Where(s => s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceEnd.Value.Date >= today &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(
c => c.EventCategory.Name == tab)).ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}
The similar code have all methods above. How to rewrite it? May be, there is some pattern for this?
And, the second, now I want that tab
checks only if it filled and introduce new var called placeName
(also check if filled):
private IEnumerable<EventSchedule> GetTodaySchedules(string tab, string placeName)
{
var today = DateTime.Now.Date;
var result = Database.EventSchedules.Where(s => s.RecurrenceStart.Value.Date <= today &&
s.RecurrenceEnd.Value.Date >= today &&
s.BaseEvent.IsApproved.Value && !s.IsRemoved.Value &&
s.BaseEvent.EventsCategories.Any(
c => c.EventCategory.Name == tab) && s.BasePlace.Name).ToList();
return result.Where(s => Evaluator.CheckDate(s, today)).ToList();
}
I don't want to create separate methods for this. What is best way to do this?
Thanks.