mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 01:54:43 +00:00
Show a user's avatar on the public profile (#333)
This commit is contained in:
@@ -1,19 +1,39 @@
|
||||
using Domain.Entities;
|
||||
using Features.ImageUploads.Services;
|
||||
|
||||
namespace Features.Users.Dtos;
|
||||
|
||||
/// <summary>Converts user domain entities into API response models.</summary>
|
||||
public static class UserDtoMapper
|
||||
{
|
||||
/// <summary>
|
||||
/// How long a presigned avatar URL stays valid. Long enough to outlive the page that embeds it,
|
||||
/// short enough that a leaked URL stops working.
|
||||
/// </summary>
|
||||
public static readonly TimeSpan AvatarUrlLifetime = TimeSpan.FromHours(1);
|
||||
|
||||
/// <summary>Converts a user account into its public-profile representation.</summary>
|
||||
public static PublicUserProfileDto ToPublicProfileDto(this UserAccount user)
|
||||
/// <param name="user">The account to convert.</param>
|
||||
/// <param name="fileStorageProvider">
|
||||
/// Signs the download URL for the account's avatar. <c>Photo.Hyperlink</c> holds an object key
|
||||
/// rather than a URL, so it has to be signed before a client can fetch it.
|
||||
/// </param>
|
||||
public static PublicUserProfileDto ToPublicProfileDto(
|
||||
this UserAccount user,
|
||||
IFileStorageProvider fileStorageProvider
|
||||
)
|
||||
{
|
||||
string? avatarKey = user.UserAvatar?.Photo?.Hyperlink;
|
||||
|
||||
return new PublicUserProfileDto(
|
||||
UserAccountId : user.UserAccountId,
|
||||
Username : user.Username,
|
||||
FirstName : user.FirstName,
|
||||
LastName : user.LastName,
|
||||
Biography : user.UserProfile?.Biography ?? string.Empty,
|
||||
AvatarUrl : string.IsNullOrWhiteSpace(avatarKey)
|
||||
? string.Empty
|
||||
: fileStorageProvider.GetPresignedUrl(avatarKey, AvatarUrlLifetime),
|
||||
CreatedAt : user.CreatedAt
|
||||
);
|
||||
}
|
||||
|
||||
@@ -7,5 +7,6 @@ public record PublicUserProfileDto(
|
||||
string FirstName,
|
||||
string LastName,
|
||||
string Biography,
|
||||
string AvatarUrl,
|
||||
DateTime CreatedAt
|
||||
);
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
using Domain.Entities;
|
||||
using Features.ImageUploads.Services;
|
||||
using Features.Users.Dtos;
|
||||
using Features.Users.Repository;
|
||||
using MediatR;
|
||||
|
||||
namespace Features.Users.Queries.GetAllUsers;
|
||||
|
||||
public class GetAllUsersHandler(IUserListRepository repository)
|
||||
: IRequestHandler<GetAllUsersQuery, IEnumerable<PublicUserProfileDto>>
|
||||
public class GetAllUsersHandler(
|
||||
IUserListRepository repository,
|
||||
IFileStorageProvider fileStorageProvider
|
||||
) : IRequestHandler<GetAllUsersQuery, IEnumerable<PublicUserProfileDto>>
|
||||
{
|
||||
public async Task<IEnumerable<PublicUserProfileDto>> Handle(
|
||||
GetAllUsersQuery request,
|
||||
@@ -14,6 +17,6 @@ public class GetAllUsersHandler(IUserListRepository repository)
|
||||
)
|
||||
{
|
||||
IEnumerable<UserAccount> users = await repository.GetAllAsync(request.Limit, request.Offset);
|
||||
return users.Select(user => user.ToPublicProfileDto());
|
||||
return users.Select(user => user.ToPublicProfileDto(fileStorageProvider));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
using Domain.Entities;
|
||||
using Domain.Exceptions;
|
||||
using Features.ImageUploads.Services;
|
||||
using Features.Users.Dtos;
|
||||
using Features.Users.Repository;
|
||||
using MediatR;
|
||||
|
||||
namespace Features.Users.Queries.GetPublicUserProfileById;
|
||||
|
||||
public class GetPublicUserProfileByIdHandler(IUserListRepository repository)
|
||||
: IRequestHandler<GetPublicUserProfileByIdQuery, PublicUserProfileDto>
|
||||
public class GetPublicUserProfileByIdHandler(
|
||||
IUserListRepository repository,
|
||||
IFileStorageProvider fileStorageProvider
|
||||
) : IRequestHandler<GetPublicUserProfileByIdQuery, PublicUserProfileDto>
|
||||
{
|
||||
/// <exception cref="NotFoundException">Thrown when no user account exists with the given ID.</exception>
|
||||
public async Task<PublicUserProfileDto> Handle(
|
||||
@@ -18,6 +21,6 @@ public class GetPublicUserProfileByIdHandler(IUserListRepository repository)
|
||||
UserAccount? user = await repository.GetByIdAsync(request.UserAccountId);
|
||||
if (user is null)
|
||||
throw new NotFoundException($"User with ID {request.UserAccountId} not found");
|
||||
return user.ToPublicProfileDto();
|
||||
return user.ToPublicProfileDto(fileStorageProvider);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@ public class UserListRepository(ISqlConnectionFactory connectionFactory)
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
/// Left-joins <c>Social.UserProfile</c>, so <see cref="UserAccount.UserProfile" /> is
|
||||
/// <c>null</c> for an account whose profile has yet to be created.
|
||||
/// <c>null</c> for an account whose profile has yet to be created, and left-joins the
|
||||
/// account's active <c>Social.UserAvatar</c> (<c>ValidTo IS NULL</c>) with its
|
||||
/// <c>Media.Photo</c>, so <see cref="UserAccount.UserAvatar" /> is <c>null</c> for an account
|
||||
/// with no avatar.
|
||||
/// </remarks>
|
||||
public async Task<UserAccount?> GetByIdAsync(Guid id)
|
||||
{
|
||||
@@ -23,51 +26,89 @@ public class UserListRepository(ISqlConnectionFactory connectionFactory)
|
||||
IEnumerable<UserAccount> results = await connection.QueryAsync<
|
||||
UserAccount,
|
||||
UserProfile,
|
||||
Photo,
|
||||
UserAvatar,
|
||||
UserAccount
|
||||
>(
|
||||
"""
|
||||
SELECT
|
||||
ua.UserAccountID,
|
||||
ua.Username,
|
||||
ua.FirstName, ua.LastName, ua.Email, ua.CreatedAt, ua.UpdatedAt,
|
||||
ua.DateOfBirth, ua.RowVersion,
|
||||
up.UserProfileID, up.UserAccountID, up.Biography, up.RowVersion
|
||||
ua.UserAccountID, ua.Username, ua.FirstName, ua.LastName,
|
||||
ua.Email, ua.CreatedAt, ua.UpdatedAt, ua.DateOfBirth,
|
||||
ua.RowVersion, up.UserProfileID, up.UserAccountID,
|
||||
up.Biography, up.RowVersion, p.PhotoID, p.UploadedByID,
|
||||
p.Hyperlink, p.UploadedAt, p.RowVersion, av.UserAvatarID,
|
||||
av.UserProfileID, av.PhotoID, av.ValidFrom, av.ValidTo,
|
||||
av.RowVersion
|
||||
FROM Auth.UserAccount ua
|
||||
LEFT JOIN Social.UserProfile up ON ua.UserAccountID = up.UserAccountID
|
||||
LEFT JOIN Social.UserProfile up
|
||||
ON ua.UserAccountID = up.UserAccountID
|
||||
LEFT JOIN Social.UserAvatar av
|
||||
ON up.UserProfileID = av.UserProfileID
|
||||
AND av.ValidTo IS NULL
|
||||
LEFT JOIN Media.Photo p ON av.PhotoID = p.PhotoID
|
||||
WHERE ua.UserAccountID = @UserAccountId
|
||||
""",
|
||||
MapUserRow,
|
||||
new { UserAccountId = id },
|
||||
splitOn: "UserProfileID"
|
||||
splitOn: AvatarSplitOn
|
||||
);
|
||||
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
|
||||
private static UserAccount MapUserRow(UserAccount user, UserProfile? profile)
|
||||
{
|
||||
user.UserProfile = profile;
|
||||
return user;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
/// <remarks>
|
||||
/// Always applies pagination via <c>OFFSET</c>/<c>FETCH</c>, ordered by creation date descending.
|
||||
/// A <c>null</c> <paramref name="limit" />/<paramref name="offset" /> is treated as "no limit"/"no offset".
|
||||
/// Does not select <c>Email</c> or <c>DateOfBirth</c>: this listing is consumed only as public
|
||||
/// profiles, so those fields are left at their default rather than read from the database.
|
||||
/// Joins the same profile and active-avatar rows as <see cref="GetByIdAsync" />, both of which are
|
||||
/// part of a public profile. Does not select <c>Email</c> or <c>DateOfBirth</c>: this listing is
|
||||
/// consumed only as public profiles, so those fields are left at their default rather than read
|
||||
/// from the database.
|
||||
/// </remarks>
|
||||
public async Task<IEnumerable<UserAccount>> GetAllAsync(int? limit, int? offset)
|
||||
{
|
||||
await using DbConnection connection = await CreateConnection();
|
||||
return await connection.QueryAsync<UserAccount>(
|
||||
return await connection.QueryAsync<
|
||||
UserAccount,
|
||||
UserProfile,
|
||||
Photo,
|
||||
UserAvatar,
|
||||
UserAccount
|
||||
>(
|
||||
"""
|
||||
SELECT UserAccountID, Username, FirstName, LastName, CreatedAt, UpdatedAt, RowVersion
|
||||
FROM Auth.UserAccount
|
||||
ORDER BY CreatedAt DESC
|
||||
SELECT
|
||||
ua.UserAccountID, ua.Username, ua.FirstName, ua.LastName,
|
||||
ua.CreatedAt, ua.UpdatedAt, ua.RowVersion,
|
||||
up.UserProfileID, up.UserAccountID, up.Biography, up.RowVersion,
|
||||
p.PhotoID, p.UploadedByID, p.Hyperlink, p.UploadedAt, p.RowVersion,
|
||||
av.UserAvatarID, av.UserProfileID, av.PhotoID, av.ValidFrom, av.ValidTo,
|
||||
av.RowVersion
|
||||
FROM Auth.UserAccount ua
|
||||
LEFT JOIN Social.UserProfile up ON ua.UserAccountID = up.UserAccountID
|
||||
LEFT JOIN Social.UserAvatar av
|
||||
ON up.UserProfileID = av.UserProfileID AND av.ValidTo IS NULL
|
||||
LEFT JOIN Media.Photo p ON av.PhotoID = p.PhotoID
|
||||
ORDER BY ua.CreatedAt DESC
|
||||
OFFSET @Offset ROWS FETCH NEXT @Limit ROWS ONLY
|
||||
""",
|
||||
new { Offset = offset ?? 0, Limit = limit ?? int.MaxValue }
|
||||
MapUserRow,
|
||||
new { Offset = offset ?? 0, Limit = limit ?? int.MaxValue },
|
||||
splitOn: AvatarSplitOn
|
||||
);
|
||||
}
|
||||
|
||||
private const string AvatarSplitOn = "UserProfileID,PhotoID,UserAvatarID";
|
||||
|
||||
private static UserAccount MapUserRow(
|
||||
UserAccount user,
|
||||
UserProfile? profile,
|
||||
Photo? photo,
|
||||
UserAvatar? avatar
|
||||
)
|
||||
{
|
||||
user.UserProfile = profile;
|
||||
avatar?.Photo = photo;
|
||||
user.UserAvatar = avatar;
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user