mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 10:08:39 +00:00
Refactor method parameters into options class
This commit is contained in:
parent
1172b92bc9
commit
02fbd731e6
3 changed files with 40 additions and 30 deletions
|
@ -30,16 +30,21 @@ public class ActivityController : ControllerBase
|
|||
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
|
||||
|
@ -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 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)
|
||||
|
@ -200,14 +205,15 @@ public class ActivityController : ControllerBase
|
|||
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);
|
||||
|
||||
|
@ -246,11 +252,14 @@ public class ActivityController : ControllerBase
|
|||
|
||||
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);
|
||||
|
||||
|
|
|
@ -3,6 +3,7 @@ using System.Linq.Expressions;
|
|||
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;
|
||||
|
|
|
@ -32,7 +32,7 @@ public static class ActivityQueryExtensions
|
|||
/// 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) =>
|
||||
|
@ -84,8 +84,8 @@ public static class ActivityQueryExtensions
|
|||
/// Converts an <see cref="IQueryable"/><<see cref="ActivityEntity"/>> into an <see cref="IQueryable"/><<see cref="ActivityDto"/>> 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"/><<see cref="ActivityDto"/>></returns>
|
||||
public static IQueryable<ActivityDto> ToActivityDto
|
||||
(this IQueryable<ActivityEntity> activityQuery, bool includeSlotCreator = false, bool includeTeamPick = false)
|
||||
|
@ -107,8 +107,8 @@ public static class ActivityQueryExtensions
|
|||
/// Converts an IEnumerable<<see cref="ActivityEntity"/>> into an IEnumerable<<see cref="ActivityDto"/>> 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<<see cref="ActivityDto"/>></returns>
|
||||
public static IEnumerable<ActivityDto> ToActivityDto
|
||||
(this IEnumerable<ActivityEntity> activityEnumerable, bool includeSlotCreator = false, bool includeTeamPick = false)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue