Skip to content

Commit fb81a7e

Browse files
committed
CB-41 Return ProfileImage in UserViewModel
1 parent f3373a2 commit fb81a7e

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

ChatbotBuilderApi.Application/Users/UserApplicationErrors.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ public static class UserApplicationErrors
1111
public static readonly Error OldPasswordRequired = Error.BadRequest(
1212
"Users.OldPasswordRequired",
1313
"Old password is required.");
14+
15+
public static readonly Error UserProfileImageNotFound = Error.NotFound(
16+
"Users.UserProfileImageNotFound",
17+
"User profile image not found.");
1418
}

ChatbotBuilderApi.Presentation/Users/UsersController.cs

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using Asp.Versioning;
22
using ChatbotBuilderApi.Application.Core.Shared;
3+
using ChatbotBuilderApi.Application.Images.ListImages;
34
using ChatbotBuilderApi.Application.Users;
45
using ChatbotBuilderApi.Domain.Users;
56
using ChatbotBuilderApi.Presentation.Core.Abstract;
67
using ChatbotBuilderApi.Presentation.Core.Attributes;
78
using ChatbotBuilderApi.Presentation.Core.Extensions;
89
using ChatbotBuilderApi.Presentation.Core.Responses;
10+
using ChatbotBuilderApi.Presentation.Graphs.Data;
911
using ChatbotBuilderApi.Presentation.Users.Requests;
1012
using ChatbotBuilderApi.Presentation.Users.ViewModels;
1113
using MediatR;
@@ -29,6 +31,26 @@ public UsersController(ISender sender, UserManager<User> userManager) : base(sen
2931
_userManager = userManager;
3032
}
3133

34+
private async Task<Result<ImageDataModel>> GetProfileImageResultAsync(User user)
35+
{
36+
var query = new ListImagesQuery
37+
{
38+
IncludeOnlyProfilePictures = true,
39+
OwnerId = new UserId(user.Id),
40+
PageParams = new PageParams(1, 1)
41+
};
42+
43+
var images = await Sender.Send(query);
44+
if (images.IsFailure)
45+
{
46+
return Result.Failure<ImageDataModel>(UserApplicationErrors.UserProfileImageNotFound);
47+
}
48+
49+
return Result.Success(images.Value.Page.Items
50+
.Select(i => new ImageDataModel(i.Url))
51+
.FirstOrDefault());
52+
}
53+
3254
/// <summary>
3355
/// Gets a user by ID.
3456
/// </summary>
@@ -49,12 +71,19 @@ public async Task<ActionResult<UserViewModel>> GetUserById(
4971
.ToProblemDetails();
5072
}
5173

74+
var imageResult = await GetProfileImageResultAsync(user);
75+
if (imageResult.IsFailure)
76+
{
77+
return imageResult.ToProblemDetails();
78+
}
79+
5280
return Ok(new UserViewModel(
5381
user.Id,
5482
user.UserName!,
5583
user.Email!,
5684
user.FirstName,
57-
user.LastName
85+
user.LastName,
86+
imageResult.Value
5887
));
5988
}
6089

@@ -104,12 +133,19 @@ public async Task<ActionResult<UserViewModel>> GetCurrentUser()
104133
.ToProblemDetails();
105134
}
106135

136+
var imageResult = await GetProfileImageResultAsync(user);
137+
if (imageResult.IsFailure)
138+
{
139+
return imageResult.ToProblemDetails();
140+
}
141+
107142
return Ok(new UserViewModel(
108143
user.Id,
109144
user.UserName!,
110145
user.Email!,
111146
user.FirstName,
112-
user.LastName
147+
user.LastName,
148+
imageResult.Value
113149
));
114150
}
115151

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,20 @@
1-
namespace ChatbotBuilderApi.Presentation.Users.ViewModels;
1+
using ChatbotBuilderApi.Presentation.Graphs.Data;
22

3+
namespace ChatbotBuilderApi.Presentation.Users.ViewModels;
4+
5+
/// <summary>
6+
/// View model for a user.
7+
/// </summary>
8+
/// <param name="Id">ID of the user.</param>
9+
/// <param name="UserName">Username of the user.</param>
10+
/// <param name="Email">Email of the user.</param>
11+
/// <param name="FirstName">First name of the user. If any.</param>
12+
/// <param name="LastName">Last name of the user. If any.</param>
13+
/// <param name="ProfileImage">Profile image of the user. If any.</param>
314
public sealed record UserViewModel(
415
Guid Id,
516
string UserName,
617
string Email,
718
string? FirstName,
8-
string? LastName);
19+
string? LastName,
20+
ImageDataModel? ProfileImage);

0 commit comments

Comments
 (0)