Refactor method parameters into options class

This commit is contained in:
Slendy 2024-11-02 13:19:14 -05:00
commit 02fbd731e6
No known key found for this signature in database
GPG key ID: 7288D68361B91428
3 changed files with 40 additions and 30 deletions

View file

@ -30,16 +30,21 @@ public class ActivityController : ControllerBase
this.database = 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 private async Task<IQueryable<ActivityDto>> GetFilters
( (
IQueryable<ActivityDto> dtoQuery, IQueryable<ActivityDto> dtoQuery,
GameTokenEntity token, GameTokenEntity token,
bool excludeNews, ActivityFilterOptions options
bool excludeMyLevels,
bool excludeFriends,
bool excludeFavouriteUsers,
bool excludeMyself,
bool excludeMyPlaylists = true
) )
{ {
dtoQuery = token.GameVersion == GameVersion.LittleBigPlanetVita dtoQuery = token.GameVersion == GameVersion.LittleBigPlanetVita
@ -75,37 +80,37 @@ public class ActivityController : ControllerBase
} }
} }
Expression<Func<ActivityDto, bool>> newsPredicate = !excludeNews Expression<Func<ActivityDto, bool>> newsPredicate = !options.ExcludeNews
? new IncludeNewsFilter().GetPredicate() ? new IncludeNewsFilter().GetPredicate()
: new ExcludeNewsFilter().GetPredicate(); : new ExcludeNewsFilter().GetPredicate();
predicate = predicate.Or(newsPredicate); predicate = predicate.Or(newsPredicate);
if (!excludeMyLevels) if (!options.ExcludeMyLevels)
{ {
predicate = predicate.Or(dto => dto.TargetSlotCreatorId == token.UserId); predicate = predicate.Or(dto => dto.TargetSlotCreatorId == token.UserId);
} }
List<int> includedUserIds = []; List<int> includedUserIds = [];
if (!excludeFriends) if (!options.ExcludeFriends)
{ {
includedUserIds.AddRange(friendIds); includedUserIds.AddRange(friendIds);
} }
if (!excludeFavouriteUsers) if (!options.ExcludeFavouriteUsers)
{ {
includedUserIds.AddRange(favouriteUsers); includedUserIds.AddRange(favouriteUsers);
} }
if (!excludeMyself) if (!options.ExcludeMyself)
{ {
includedUserIds.Add(token.UserId); includedUserIds.Add(token.UserId);
} }
predicate = predicate.Or(dto => includedUserIds.Contains(dto.Activity.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) List<int> creatorPlaylists = await this.database.Playlists.Where(p => p.CreatorId == token.UserId)
.Select(p => p.PlaylistId) .Select(p => p.PlaylistId)
@ -200,14 +205,15 @@ public class ActivityController : ControllerBase
if (token.GameVersion != GameVersion.LittleBigPlanet3) return this.NotFound(); if (token.GameVersion != GameVersion.LittleBigPlanet3) return this.NotFound();
IQueryable<ActivityDto> activityEvents = await this.GetFilters( IQueryable<ActivityDto> activityEvents = await this.GetFilters(
this.database.Activities.ToActivityDto(true, true), this.database.Activities.ToActivityDto(true, true), token, new ActivityFilterOptions()
token, {
excludeNews, ExcludeNews = excludeNews,
true, ExcludeMyLevels = true,
true, ExcludeFriends = true,
true, ExcludeFavouriteUsers = true,
excludeMyself, ExcludeMyself = excludeMyself,
excludeMyPlaylists); ExcludeMyPlaylists = excludeMyPlaylists,
});
(DateTime Start, DateTime End) times = await this.GetTimeBounds(activityEvents, timestamp, null); (DateTime Start, DateTime End) times = await this.GetTimeBounds(activityEvents, timestamp, null);
@ -246,11 +252,14 @@ public class ActivityController : ControllerBase
IQueryable<ActivityDto> activityEvents = await this.GetFilters(this.database.Activities.ToActivityDto(true), IQueryable<ActivityDto> activityEvents = await this.GetFilters(this.database.Activities.ToActivityDto(true),
token, token,
excludeNews, new ActivityFilterOptions
excludeMyLevels, {
excludeFriends, ExcludeNews = excludeNews,
excludeFavouriteUsers, ExcludeMyLevels = excludeMyLevels,
excludeMyself); ExcludeFriends = excludeFriends,
ExcludeFavouriteUsers = excludeFavouriteUsers,
ExcludeMyself = excludeMyself,
});
(DateTime Start, DateTime End) times = await this.GetTimeBounds(activityEvents, timestamp, endTimestamp); (DateTime Start, DateTime End) times = await this.GetTimeBounds(activityEvents, timestamp, endTimestamp);

View file

@ -3,6 +3,7 @@ using System.Linq.Expressions;
using LBPUnion.ProjectLighthouse.Database; using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Extensions; using LBPUnion.ProjectLighthouse.Extensions;
using LBPUnion.ProjectLighthouse.Filter; using LBPUnion.ProjectLighthouse.Filter;
using LBPUnion.ProjectLighthouse.Filter.Filters;
using LBPUnion.ProjectLighthouse.Filter.Filters.Slot; using LBPUnion.ProjectLighthouse.Filter.Filters.Slot;
using LBPUnion.ProjectLighthouse.Filter.Sorts; using LBPUnion.ProjectLighthouse.Filter.Sorts;
using LBPUnion.ProjectLighthouse.Filter.Sorts.Metadata; using LBPUnion.ProjectLighthouse.Filter.Sorts.Metadata;

View file

@ -32,7 +32,7 @@ public static class ActivityQueryExtensions
/// Turns a list of <see cref="ActivityDto"/> into a group based on its timestamp /// Turns a list of <see cref="ActivityDto"/> into a group based on its timestamp
/// </summary> /// </summary>
/// <param name="activityQuery">An <see cref="IQueryable{ActivityDto}"/> to group</param> /// <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> /// <returns>The transformed query containing groups of <see cref="ActivityDto"/></returns>
public static IQueryable<IGrouping<ActivityGroup, ActivityDto>> ToActivityGroups public static IQueryable<IGrouping<ActivityGroup, ActivityDto>> ToActivityGroups
(this IQueryable<ActivityDto> activityQuery, bool groupByActor = false) => (this IQueryable<ActivityDto> activityQuery, bool groupByActor = false) =>
@ -84,8 +84,8 @@ public static class ActivityQueryExtensions
/// Converts an <see cref="IQueryable"/>&lt;<see cref="ActivityEntity"/>&gt; into an <see cref="IQueryable"/>&lt;<see cref="ActivityDto"/>&gt; for grouping. /// Converts an <see cref="IQueryable"/>&lt;<see cref="ActivityEntity"/>&gt; into an <see cref="IQueryable"/>&lt;<see cref="ActivityDto"/>&gt; for grouping.
/// </summary> /// </summary>
/// <param name="activityQuery">The activity query to be converted.</param> /// <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="includeSlotCreator">Whether 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="includeTeamPick">Whether the <see cref="ActivityDto.TargetTeamPickId"/> field should be included.</param>
/// <returns>The converted <see cref="IQueryable"/>&lt;<see cref="ActivityDto"/>&gt;</returns> /// <returns>The converted <see cref="IQueryable"/>&lt;<see cref="ActivityDto"/>&gt;</returns>
public static IQueryable<ActivityDto> ToActivityDto public static IQueryable<ActivityDto> ToActivityDto
(this IQueryable<ActivityEntity> activityQuery, bool includeSlotCreator = false, bool includeTeamPick = false) (this IQueryable<ActivityEntity> activityQuery, bool includeSlotCreator = false, bool includeTeamPick = false)
@ -107,8 +107,8 @@ public static class ActivityQueryExtensions
/// Converts an IEnumerable&lt;<see cref="ActivityEntity"/>&gt; into an IEnumerable&lt;<see cref="ActivityDto"/>&gt; for grouping. /// Converts an IEnumerable&lt;<see cref="ActivityEntity"/>&gt; into an IEnumerable&lt;<see cref="ActivityDto"/>&gt; for grouping.
/// </summary> /// </summary>
/// <param name="activityEnumerable">The activity query to be converted.</param> /// <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="includeSlotCreator">Whether 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="includeTeamPick">Whether the <see cref="ActivityDto.TargetTeamPickId"/> field should be included.</param>
/// <returns>The converted IEnumerable&lt;<see cref="ActivityDto"/>&gt;</returns> /// <returns>The converted IEnumerable&lt;<see cref="ActivityDto"/>&gt;</returns>
public static IEnumerable<ActivityDto> ToActivityDto public static IEnumerable<ActivityDto> ToActivityDto
(this IEnumerable<ActivityEntity> activityEnumerable, bool includeSlotCreator = false, bool includeTeamPick = false) (this IEnumerable<ActivityEntity> activityEnumerable, bool includeSlotCreator = false, bool includeTeamPick = false)