/// <summary>
/// Gets a collection of prayer requests by applying a standard filtering
/// algorithm as specified in the options.
/// </summary>
/// <param name="options">The options that specifies the filters.</param>
/// <returns>A collection of <see cref="PrayerRequest"/> objects.</returns>
public IQueryable <PrayerRequest> GetPrayerRequests(PrayerRequestQueryOptions options)
{
var qryPrayerRequests = Queryable();
// If not including inactive requests then filter them out.
if (!options.IncludeInactive)
{
qryPrayerRequests = qryPrayerRequests.Where(r => r.IsActive ?? true);
}
// If not including expired requests then filter them out.
if (!options.IncludeExpired)
{
qryPrayerRequests = qryPrayerRequests.Where(r => !r.ExpirationDate.HasValue ||
r.ExpirationDate >= RockDateTime.Now);
}
// If not including unapproved requests then filter them out.
if (!options.IncludeUnapproved)
{
qryPrayerRequests = qryPrayerRequests.Where(r => r.IsApproved == true);
}
// If not including non-public requests then filter them out.
if (!options.IncludeNonPublic)
{
qryPrayerRequests = qryPrayerRequests.Where(r => r.IsPublic == true);
}
// Filter by category if we have been given any.
if (options.Categories != null && options.Categories.Any())
{
var categoryService = new CategoryService(( RockContext )Context);
var categories = new List <Guid>(options.Categories);
// If filtered by category, only show prayer requests in that
// category or any of its descendant categories.
foreach (var categoryGuid in options.Categories)
{
categoryService.GetAllDescendents(categoryGuid)
.Select(a => a.Guid)
.ToList()
.ForEach(c => categories.Add(c));
}
categories = categories.Distinct().ToList();
qryPrayerRequests = qryPrayerRequests
.Include(r => r.Category)
.Where(r => r.CategoryId.HasValue && categories.Contains(r.Category.Guid));
}
// Filter by campus if we have been given any.
if (options.Campuses != null && options.Campuses.Any())
{
if (options.IncludeEmptyCampus)
{
qryPrayerRequests = qryPrayerRequests
.Include(r => r.Campus)
.Where(r => !r.CampusId.HasValue || (r.CampusId.HasValue && options.Campuses.Contains(r.Campus.Guid)));
}
else
{
qryPrayerRequests = qryPrayerRequests
.Include(r => r.Campus)
.Where(r => r.CampusId.HasValue && options.Campuses.Contains(r.Campus.Guid));
}
}
// Filter by group if it has been specified.
if (options.GroupGuids?.Any() ?? false)
{
qryPrayerRequests = qryPrayerRequests
.Where(a => options.GroupGuids.Contains(a.Group.Guid));
}
// If we are not filtering by group, then exclude any group requests
// unless the block setting including them is enabled.
if (!(options.GroupGuids?.Any() ?? false) && !options.IncludeGroupRequests)
{
qryPrayerRequests = qryPrayerRequests.Where(a => !a.GroupId.HasValue);
}
return(qryPrayerRequests);
}