Following lists are public; followers are visible to the user and to mutual followers.
Adds GET /users/{id}/followers and /following, exposes isFollowing and canViewFollowers
on the profile, and returns isFollowedByViewer from user search so the web player no
longer calls /is-following per user.
223 lines
8.0 KiB
C#
223 lines
8.0 KiB
C#
using Mediator;
|
|
using Musify.Api.Authentication;
|
|
using Musify.Api.DataTransferObjects.Users;
|
|
using Musify.Api.Extensions;
|
|
using Musify.Api.Filters;
|
|
using Musify.Application.Shared;
|
|
using Musify.Application.Tracks;
|
|
using Musify.Application.Tracks.Responses;
|
|
using Musify.Application.Users;
|
|
using Musify.Application.Users.Responses;
|
|
|
|
namespace Musify.Api.Endpoints;
|
|
|
|
public static class UserEndpoints
|
|
{
|
|
public static IEndpointRouteBuilder MapUserEndpoints(this IEndpointRouteBuilder app)
|
|
{
|
|
var group = app.MapGroup("/users")
|
|
.WithTags("Users");
|
|
|
|
group.MapGet("/", GetUsers)
|
|
.WithName("GetUsers")
|
|
.WithSummary("Get Paginated Users.")
|
|
.Produces<PaginatedResponse<UserSummaryResponse>>();
|
|
|
|
group.MapGet("/{id}", GetUserById)
|
|
.WithName("GetUserById")
|
|
.WithSummary("Get A User By Id.")
|
|
.Produces<UserApplicationResponse>()
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/{id}/listening-history", GetListeningHistory)
|
|
.WithName("GetListeningHistory")
|
|
.WithSummary("Get A User'S Listening History.")
|
|
.Produces<IEnumerable<TrackApplicationResponse>>()
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/{id}/listening-stats", GetListeningStats)
|
|
.WithName("GetListeningStats")
|
|
.WithSummary("Get A User'S Listening Stats For The Current Week And Streak.")
|
|
.Produces<ListeningStatsResponse>();
|
|
|
|
group.MapGet("/{id}/last-listened-track", GetLastTrackListenedByUserId)
|
|
.WithName("GetLastTrackListenedByUserId")
|
|
.WithSummary("Get The Last Track A User Listened To.")
|
|
.Produces<TrackApplicationResponse>()
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/{id}/profile", GetUserProfile)
|
|
.WithName("GetUserProfile")
|
|
.WithSummary("Get A User'S Public Profile, Including Follower Counts.")
|
|
.Produces<UserProfileResponse>()
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/{id}/followers", GetFollowers)
|
|
.WithName("GetUserFollowers")
|
|
.WithSummary("Get The Users Following A User. Visible To The User And To Mutual Followers.")
|
|
.Produces<PaginatedResponse<UserSummaryResponse>>()
|
|
.Produces(StatusCodes.Status403Forbidden)
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/{id}/following", GetFollowing)
|
|
.WithName("GetUserFollowing")
|
|
.WithSummary("Get The Users A User Follows.")
|
|
.Produces<PaginatedResponse<UserSummaryResponse>>()
|
|
.Produces(StatusCodes.Status404NotFound);
|
|
|
|
group.MapGet("/{id}/is-following", IsFollowing)
|
|
.WithName("IsFollowingUser")
|
|
.WithSummary("Check Whether The Current User Follows Another User.")
|
|
.RequireAuthorization()
|
|
.Produces<bool>()
|
|
.Produces(StatusCodes.Status401Unauthorized);
|
|
|
|
group.MapPost("/{id}/follow", FollowUser)
|
|
.WithName("FollowUser")
|
|
.WithSummary("Follow A User.")
|
|
.RequireAuthorization()
|
|
.Produces(StatusCodes.Status204NoContent)
|
|
.Produces(StatusCodes.Status401Unauthorized)
|
|
.Produces(StatusCodes.Status404NotFound)
|
|
.ProducesValidationProblem();
|
|
|
|
group.MapDelete("/{id}/follow", UnfollowUser)
|
|
.WithName("UnfollowUser")
|
|
.WithSummary("Unfollow A User.")
|
|
.RequireAuthorization()
|
|
.Produces(StatusCodes.Status204NoContent)
|
|
.Produces(StatusCodes.Status401Unauthorized);
|
|
|
|
group.MapPost("/", CreateUser)
|
|
.WithName("CreateUser")
|
|
.WithSummary("Create A New User.")
|
|
.AddEndpointFilter<ValidationFilter<CreateUserRequest>>()
|
|
.Produces<UserApplicationResponse>(StatusCodes.Status201Created)
|
|
.ProducesValidationProblem()
|
|
.Produces(StatusCodes.Status409Conflict);
|
|
|
|
return app;
|
|
}
|
|
|
|
private static async Task<IResult> GetUsers(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
CancellationToken cancellationToken,
|
|
string? usernameSearch,
|
|
int pageNumber = 1,
|
|
int pageSize = 10)
|
|
{
|
|
var result = await mediator.Send(new GetUsersQuery(pageNumber, pageSize, usernameSearch, currentUser.Id), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> GetUserById(
|
|
IMediator mediator,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetUserByIdQuery(id), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IEnumerable<TrackApplicationResponse>> GetListeningHistory(
|
|
IMediator mediator,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await mediator.Send(new GetListeningHistoryQuery(id), cancellationToken);
|
|
}
|
|
|
|
private static async Task<ListeningStatsResponse> GetListeningStats(
|
|
IMediator mediator,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await mediator.Send(new GetListeningStatsQuery(id), cancellationToken);
|
|
}
|
|
|
|
private static async Task<IResult> GetLastTrackListenedByUserId(
|
|
IMediator mediator,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetLastTrackListenedByUserIdQuery(id), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> GetUserProfile(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new GetUserProfileQuery(id, currentUser.Id), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> GetFollowers(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
long id,
|
|
CancellationToken cancellationToken,
|
|
int pageNumber = 1,
|
|
int pageSize = 20)
|
|
{
|
|
var result = await mediator.Send(new GetUserFollowersQuery(currentUser.Id, id, pageNumber, pageSize), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> GetFollowing(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
long id,
|
|
CancellationToken cancellationToken,
|
|
int pageNumber = 1,
|
|
int pageSize = 20)
|
|
{
|
|
var result = await mediator.Send(new GetUserFollowingQuery(currentUser.Id, id, pageNumber, pageSize), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<bool> IsFollowing(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
return await mediator.Send(new IsFollowingUserQuery(currentUser.RequiredId, id), cancellationToken);
|
|
}
|
|
|
|
private static async Task<IResult> FollowUser(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new FollowUserCommand(currentUser.RequiredId, id), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> UnfollowUser(
|
|
IMediator mediator,
|
|
CurrentUser currentUser,
|
|
long id,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(new UnfollowUserCommand(currentUser.RequiredId, id), cancellationToken);
|
|
return result.ToHttpResult();
|
|
}
|
|
|
|
private static async Task<IResult> CreateUser(
|
|
IMediator mediator,
|
|
CreateUserRequest request,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var result = await mediator.Send(
|
|
new CreateUserCommand(request.Id, request.Name, request.FirstName, request.SecondName),
|
|
cancellationToken);
|
|
|
|
return result.ToCreatedResult(user => $"/users/{user.Id}");
|
|
}
|
|
}
|