Skip to content

Commit

Permalink
Merge pull request #9 from TheInfinityNet/3-retrieve-user-profiles
Browse files Browse the repository at this point in the history
Feature (#3): Retrieve User Profile
  • Loading branch information
LeDuyNhan1201 authored Oct 29, 2024
2 parents 6343287 + 048db5c commit 2522e3a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 7 deletions.
3 changes: 3 additions & 0 deletions Identity.Presentation/appsettings.Local.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{

}
8 changes: 8 additions & 0 deletions Ocelot.Presentaition/appsettings.Local.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
4 changes: 4 additions & 0 deletions Profile.Application/Profile.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@
<ProjectReference Include="..\Profile.Domain\Profile.Domain.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="DTOs\Responses\" />
</ItemGroup>

</Project>
3 changes: 3 additions & 0 deletions Profile.Application/Services/IUserProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ public interface IUserProfileService

Task<UserProfile> GetUserProfileById(string id);

// Định nghĩa trong service interface
Task<UserProfile> GetUserProfileByAccountId(string id);

}
}
9 changes: 8 additions & 1 deletion Profile.Domain/Repositories/IUserProfileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ namespace InfinityNetServer.Services.Profile.Domain.Repositories
public interface IUserProfileRepository : ISqlRepository<UserProfile, Guid>
{


//Định nghĩa hàm repository
// lý do mình ko gọi repository trức tiếp trong controller:
// tách biệt logic vì trong service ngoài gọi truy vấn có thể sẽ làm những việc khác
// ông hình dung:
// tầng DAL <==> repository
// tầng BLL(BUS) <==> service
// tầng GUI <==> controller (ở đây là api thay vì view)
Task<UserProfile> GetUserProfileByAccountIdAsync(Guid accountId);

}
}
8 changes: 6 additions & 2 deletions Profile.Infrastructure/Repositories/UserProfileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using InfinityNetServer.Services.Profile.Domain.Entities;
using InfinityNetServer.Services.Profile.Domain.Repositories;
using InfinityNetServer.BuildingBlocks.Infrastructure.PostgreSQL.Repositories;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace InfinityNetServer.Services.Profile.Infrastructure.Repositories
{
Expand All @@ -12,7 +14,9 @@ public class UserProfileRepository : SqlRepository<UserProfile, Guid>, IUserProf
public UserProfileRepository(ProfileDbContext context) : base(context)
{ }



public async Task<UserProfile> GetUserProfileByAccountIdAsync(Guid accountId)
{
return await ((ProfileDbContext)_context).UserProfiles.FirstOrDefaultAsync(profile => profile.AccountId == accountId);
}
}
}
26 changes: 23 additions & 3 deletions Profile.Presentation/Controllers/UserProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
using System.Threading.Tasks;
using InfinityNetServer.BuildingBlocks.Application.Services;
using InfinityNetServer.Services.Profile.Application.Services;
using Microsoft.AspNetCore.Authorization;
using InfinityNetServer.Services.Profile.Domain.Entities;
using AutoMapper;

namespace InfinityNetServer.Services.Profile.Presentation.Controllers
{
Expand All @@ -23,24 +26,29 @@ public class UserProfileController : BaseApiController

private readonly ILogger<UserProfileController> _logger;

// khai báo thêm mapper
private readonly IMapper _mapper;

private readonly IConfiguration _configuration;

private readonly IUserProfileService _profileService;
private readonly IUserProfileService _userProfileService;

private readonly IMessageBus _messageBus;

public UserProfileController(
IAuthenticatedUserService authenticatedUserService,
IStringLocalizer<ProfileSharedResource> localizer,
ILogger<UserProfileController> logger,
IMapper mapper,
IConfiguration configuration,
IUserProfileService profileService,
IMessageBus messageBus) : base(authenticatedUserService)
{
_localizer = localizer;
_logger = logger;
_mapper = mapper;
_configuration = configuration;
_profileService = profileService;
_userProfileService = profileService;
_messageBus = messageBus;
}

Expand All @@ -50,14 +58,26 @@ public UserProfileController(
public IActionResult UpdateProfile([FromBody] UpdateUserProfileRequest request)
{
// TODO: Call the service to update the user profile
// await _profileService.UpdateProfile(request);
// await _userProfileService.UpdateProfile(request);

return Ok(new CommonMessageResponse
(
_localizer["profile_updated_success", request.Username].ToString()
));
}

[Authorize]
[EndpointDescription("Retrieve user profile")]
[HttpGet("{userId}")]
[ProducesResponseType(typeof(UserProfileResponse), StatusCodes.Status200OK)]
public async Task<IActionResult> RetrieveProfile(string userId)
{
_logger.LogInformation("Retrieve user profile");

UserProfile currentProfile = await _userProfileService.GetUserProfileById(userId);

return Ok(_mapper.Map<UserProfileResponse>(currentProfile));
}

}
}
13 changes: 12 additions & 1 deletion Profile.Presentation/Mappers/ProfileMapper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using InfinityNetServer.Services.Profile.Domain.Entities;
using InfinityNetServer.BuildingBlocks.Application.DTOs.Responses;
using InfinityNetServer.Services.Profile.Domain.Entities;

namespace InfinityNetServer.Services.Profile.Presentation.Mappers;

Expand All @@ -7,5 +8,15 @@ public class ProfileMapper : AutoMapper.Profile
public ProfileMapper()
{
CreateMap<UserProfile, BuildingBlocks.Application.Protos.UserProfileResponse>();

// tạo mapper
// cú pháp <nguồn, đích>
CreateMap<UserProfile, UserProfileResponse>()
.AfterMap((src, dest) =>
{
//chỗ này custome nếu trg hợp đích (dest) và nguồn (src) khác tên thuộc tính
dest.CoverId = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmCy16nhIbV3pI1qLYHMJKwbH2458oiC9EmA&s";
dest.AvatarId = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRmCy16nhIbV3pI1qLYHMJKwbH2458oiC9EmA&s";
});
}
}
9 changes: 9 additions & 0 deletions Profile.Presentation/Services/UserProfileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using InfinityNetServer.Services.Profile.Application.Services;
using InfinityNetServer.Services.Profile.Domain.Entities;
using InfinityNetServer.Services.Profile.Domain.Repositories;
using MassTransit;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using System;
Expand All @@ -25,6 +26,14 @@ public UserProfileService(IUserProfileRepository userProfileRepository, ILogger<
_localizer = localizer;
}

// await async là kiến thức về bất đồng bộ có j ông search youtube xem thêm nha
public async Task<UserProfile> GetUserProfileByAccountId(string id)
{
// chỗ này implement cái đã định nghĩa trong interface
// truyền id vào là string nên phải parse ra Guid
return await _userProfileRepository.GetUserProfileByAccountIdAsync(Guid.Parse(id));
}

public Task<UserProfile> GetUserProfileById(string id)
{
return _userProfileRepository.GetByIdAsync(Guid.Parse(id));
Expand Down
8 changes: 8 additions & 0 deletions Profile.Presentation/appsettings.Local.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

0 comments on commit 2522e3a

Please sign in to comment.