Skip to content

Commit

Permalink
Refactor method parameters into options class
Browse files Browse the repository at this point in the history
  • Loading branch information
Slendy committed Nov 2, 2024
1 parent 1172b92 commit 02fbd73
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,21 @@ public ActivityController(DatabaseContext database)
this.database = database;
}

private class ActivityFilterOptions
{
public bool ExcludeNews { get; init; }
public bool ExcludeMyLevels { get; init; }
public bool ExcludeFriends { get; init; }
public bool ExcludeFavouriteUsers { get; init; }
public bool ExcludeMyself { get; init; }
public bool ExcludeMyPlaylists { get; init; } = true;
}

private async Task<IQueryable<ActivityDto>> GetFilters
(
IQueryable<ActivityDto> dtoQuery,
GameTokenEntity token,
bool excludeNews,
bool excludeMyLevels,
bool excludeFriends,
bool excludeFavouriteUsers,
bool excludeMyself,
bool excludeMyPlaylists = true
ActivityFilterOptions options
)
{
dtoQuery = token.GameVersion == GameVersion.LittleBigPlanetVita
Expand Down Expand Up @@ -75,37 +80,37 @@ private async Task<IQueryable<ActivityDto>> GetFilters
}
}

Expression<Func<ActivityDto, bool>> newsPredicate = !excludeNews
Expression<Func<ActivityDto, bool>> newsPredicate = !options.ExcludeNews
? new IncludeNewsFilter().GetPredicate()
: new ExcludeNewsFilter().GetPredicate();

predicate = predicate.Or(newsPredicate);

if (!excludeMyLevels)
if (!options.ExcludeMyLevels)
{
predicate = predicate.Or(dto => dto.TargetSlotCreatorId == token.UserId);
}

List<int> includedUserIds = [];

if (!excludeFriends)
if (!options.ExcludeFriends)
{
includedUserIds.AddRange(friendIds);
}

if (!excludeFavouriteUsers)
if (!options.ExcludeFavouriteUsers)
{
includedUserIds.AddRange(favouriteUsers);
}

if (!excludeMyself)
if (!options.ExcludeMyself)
{
includedUserIds.Add(token.UserId);
}

predicate = predicate.Or(dto => includedUserIds.Contains(dto.Activity.UserId));

if (!excludeMyPlaylists && !excludeMyself && token.GameVersion == GameVersion.LittleBigPlanet3)
if (!options.ExcludeMyPlaylists && !options.ExcludeMyself && token.GameVersion == GameVersion.LittleBigPlanet3)
{
List<int> creatorPlaylists = await this.database.Playlists.Where(p => p.CreatorId == token.UserId)
.Select(p => p.PlaylistId)
Expand Down Expand Up @@ -200,14 +205,15 @@ public async Task<IActionResult> GlobalActivityLBP3
if (token.GameVersion != GameVersion.LittleBigPlanet3) return this.NotFound();

IQueryable<ActivityDto> activityEvents = await this.GetFilters(
this.database.Activities.ToActivityDto(true, true),
token,
excludeNews,
true,
true,
true,
excludeMyself,
excludeMyPlaylists);
this.database.Activities.ToActivityDto(true, true), token, new ActivityFilterOptions()
{
ExcludeNews = excludeNews,
ExcludeMyLevels = true,
ExcludeFriends = true,
ExcludeFavouriteUsers = true,
ExcludeMyself = excludeMyself,
ExcludeMyPlaylists = excludeMyPlaylists,
});

(DateTime Start, DateTime End) times = await this.GetTimeBounds(activityEvents, timestamp, null);

Expand Down Expand Up @@ -246,11 +252,14 @@ bool excludeMyself

IQueryable<ActivityDto> activityEvents = await this.GetFilters(this.database.Activities.ToActivityDto(true),
token,
excludeNews,
excludeMyLevels,
excludeFriends,
excludeFavouriteUsers,
excludeMyself);
new ActivityFilterOptions
{
ExcludeNews = excludeNews,
ExcludeMyLevels = excludeMyLevels,
ExcludeFriends = excludeFriends,
ExcludeFavouriteUsers = excludeFavouriteUsers,
ExcludeMyself = excludeMyself,
});

(DateTime Start, DateTime End) times = await this.GetTimeBounds(activityEvents, timestamp, endTimestamp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Filter;
using LBPUnion.ProjectLighthouse.Filter.Filters;
using LBPUnion.ProjectLighthouse.Filter.Filters.Slot;
using LBPUnion.ProjectLighthouse.Filter.Sorts;
using LBPUnion.ProjectLighthouse.Filter.Sorts.Metadata;
Expand Down
10 changes: 5 additions & 5 deletions ProjectLighthouse/Extensions/ActivityQueryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static List<int> GetIds(this IReadOnlyCollection<OuterActivityGroup> grou
/// Turns a list of <see cref="ActivityDto"/> into a group based on its timestamp
/// </summary>
/// <param name="activityQuery">An <see cref="IQueryable{ActivityDto}"/> to group</param>
/// <param name="groupByActor">Whether or not the groups should be created based on the initiator of the event or the target of the event</param>
/// <param name="groupByActor">Whether the groups should be created based on the initiator of the event or the target of the event</param>
/// <returns>The transformed query containing groups of <see cref="ActivityDto"/></returns>
public static IQueryable<IGrouping<ActivityGroup, ActivityDto>> ToActivityGroups
(this IQueryable<ActivityDto> activityQuery, bool groupByActor = false) =>
Expand Down Expand Up @@ -84,8 +84,8 @@ public static List<OuterActivityGroup> ToOuterActivityGroups
/// Converts an <see cref="IQueryable"/>&lt;<see cref="ActivityEntity"/>&gt; into an <see cref="IQueryable"/>&lt;<see cref="ActivityDto"/>&gt; for grouping.
/// </summary>
/// <param name="activityQuery">The activity query to be converted.</param>
/// <param name="includeSlotCreator">Whether or not the <see cref="ActivityDto.TargetSlotCreatorId"/> field should be included.</param>
/// <param name="includeTeamPick">Whether or not the <see cref="ActivityDto.TargetTeamPickId"/> field should be included.</param>
/// <param name="includeSlotCreator">Whether the <see cref="ActivityDto.TargetSlotCreatorId"/> field should be included.</param>
/// <param name="includeTeamPick">Whether the <see cref="ActivityDto.TargetTeamPickId"/> field should be included.</param>
/// <returns>The converted <see cref="IQueryable"/>&lt;<see cref="ActivityDto"/>&gt;</returns>
public static IQueryable<ActivityDto> ToActivityDto
(this IQueryable<ActivityEntity> activityQuery, bool includeSlotCreator = false, bool includeTeamPick = false)
Expand All @@ -107,8 +107,8 @@ public static IQueryable<ActivityDto> ToActivityDto
/// Converts an IEnumerable&lt;<see cref="ActivityEntity"/>&gt; into an IEnumerable&lt;<see cref="ActivityDto"/>&gt; for grouping.
/// </summary>
/// <param name="activityEnumerable">The activity query to be converted.</param>
/// <param name="includeSlotCreator">Whether or not the <see cref="ActivityDto.TargetSlotCreatorId"/> field should be included.</param>
/// <param name="includeTeamPick">Whether or not the <see cref="ActivityDto.TargetTeamPickId"/> field should be included.</param>
/// <param name="includeSlotCreator">Whether the <see cref="ActivityDto.TargetSlotCreatorId"/> field should be included.</param>
/// <param name="includeTeamPick">Whether the <see cref="ActivityDto.TargetTeamPickId"/> field should be included.</param>
/// <returns>The converted IEnumerable&lt;<see cref="ActivityDto"/>&gt;</returns>
public static IEnumerable<ActivityDto> ToActivityDto
(this IEnumerable<ActivityEntity> activityEnumerable, bool includeSlotCreator = false, bool includeTeamPick = false)
Expand Down

0 comments on commit 02fbd73

Please sign in to comment.