mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
Add AutoMapper and message response models
Introduced AutoMapper to the API project and registered it in the DI container. Added mapping profiles for core models to DTOs and response objects. Refactored controllers to use AutoMapper for mapping entities to response DTOs. Implemented new response models for messages, media attachments, reactions, and views. Updated MessagesLoader to load messages with related data. Added a migration to support ChatGroupId in messages. Updated dependencies and fixed minor issues in related files.
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
using AutoMapper;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Contracts.Responses;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -9,8 +12,85 @@ namespace Govor.API.Controllers;
|
||||
[Route("api/chats")]
|
||||
public class ChatLoadController : Controller
|
||||
{
|
||||
public ChatLoadController(ILogger<ChatLoadController> logger, IMessagesLoader messagesLoader)
|
||||
private readonly ICurrentUserService _currentUser;
|
||||
private readonly ILogger<ChatLoadController> _logger;
|
||||
private readonly IMessagesLoader _messagesLoader;
|
||||
private readonly IMapper _mapper;
|
||||
public ChatLoadController(
|
||||
ILogger<ChatLoadController> logger,
|
||||
IMessagesLoader messagesLoader,
|
||||
ICurrentUserService currentUser,
|
||||
IMapper mapper)
|
||||
{
|
||||
|
||||
_logger = logger;
|
||||
_messagesLoader = messagesLoader;
|
||||
_currentUser = currentUser;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet("group-messages")]
|
||||
public async Task<IActionResult> GetChatMessages(
|
||||
[FromQuery] Guid chatId,
|
||||
[FromQuery] Guid? startMessageId,
|
||||
[FromQuery] int pageSize = 20)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _messagesLoader.LoadLastMessagesInChatGroup(
|
||||
chatId,
|
||||
_currentUser.GetCurrentUserId(),
|
||||
startMessageId,
|
||||
pageSize);
|
||||
|
||||
var response = _mapper.Map<List<MessageResponse>>(result);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex.Message);
|
||||
return Unauthorized(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, "Unexpected Error! Please try again later.");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("user-messages")]
|
||||
public async Task<IActionResult> GetUserMessages(
|
||||
[FromQuery] Guid userId,
|
||||
[FromQuery] Guid? startMessageId,
|
||||
[FromQuery] int pageSize = 20)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _messagesLoader.LoadLastMessagesInUserChat(
|
||||
userId,
|
||||
_currentUser.GetCurrentUserId(),
|
||||
startMessageId,
|
||||
pageSize);
|
||||
|
||||
var response = _mapper.Map<List<MessageResponse>>(result);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex.Message);
|
||||
return Unauthorized(ex.Message);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, "Unexpected Error! Please try again later.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using AutoMapper;
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Contracts.DTOs;
|
||||
@@ -15,12 +16,15 @@ public class FriendsRequestQueryController : Controller
|
||||
private readonly ILogger<FriendsRequestQueryController> _logger;
|
||||
private readonly IFriendRequestQueryService _friendsService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public FriendsRequestQueryController(ILogger<FriendsRequestQueryController> logger,
|
||||
IFriendRequestQueryService friendsService,
|
||||
ICurrentUserService currentUserService)
|
||||
ICurrentUserService currentUserService,
|
||||
IMapper mapper)
|
||||
{
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_friendsService = friendsService;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
@@ -31,7 +35,9 @@ public class FriendsRequestQueryController : Controller
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetIncomingAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
var response = _mapper.Map<List<FriendshipDto>>(result);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
using AutoMapper;
|
||||
using Govor.Application.Exceptions.FriendsService;
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
@@ -12,14 +13,16 @@ public class FriendshipController : Controller
|
||||
{
|
||||
private readonly ILogger<FriendshipController> _logger;
|
||||
private readonly IFriendshipService _friendsService;
|
||||
//private readonly IUserDtoBuilder _builder;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public FriendshipController(ILogger<FriendshipController> logger,
|
||||
IFriendshipService friendsService,
|
||||
ICurrentUserService currentUserService)
|
||||
ICurrentUserService currentUserService,
|
||||
IMapper mapper)
|
||||
{
|
||||
_logger = logger;
|
||||
_mapper = mapper;
|
||||
_friendsService = friendsService;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
@@ -33,7 +36,10 @@ public class FriendshipController : Controller
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.SearchUsersAsync(query, _currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildUserDtos(result));
|
||||
|
||||
var response = _mapper.Map<List<UserDto>>(result);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (SearchUsersException ex)
|
||||
{
|
||||
@@ -53,7 +59,10 @@ public class FriendshipController : Controller
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetFriendsAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildUserDtos(result));
|
||||
|
||||
var response = _mapper.Map<List<UserDto>>(result);
|
||||
|
||||
return Ok(response);
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
@@ -66,14 +75,4 @@ public class FriendshipController : Controller
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
private List<UserDto> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
Username = user.Username,
|
||||
Description = user.Description,
|
||||
WasOnline = user.WasOnline,
|
||||
IconId = user.IconId
|
||||
}).ToList();
|
||||
|
||||
}
|
||||
@@ -65,6 +65,9 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IVerifyFriendship, VerifyFriendship>();
|
||||
services.AddScoped<IUserGroupsService, UserGroupsService>();
|
||||
services.AddScoped<IMessagesLoader, MessagesLoader>();
|
||||
|
||||
// Auto Mapper
|
||||
services.AddAutoMapper(typeof(MappingProfile));
|
||||
}
|
||||
|
||||
public static void AddRepositories(this IServiceCollection services)
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using AutoMapper;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Contracts.Responses;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Models.Messages;
|
||||
using Govor.Core.Models.Users;
|
||||
|
||||
namespace Govor.API.Extensions;
|
||||
|
||||
public class MappingProfile : Profile
|
||||
{
|
||||
public MappingProfile()
|
||||
{
|
||||
CreateMap<Message, MessageResponse>();
|
||||
CreateMap<MediaAttachments, MediaAttachmentResponse>();
|
||||
CreateMap<MessageReaction, MessageReactionResponse>();
|
||||
CreateMap<MessageView, MessageViewResponse>();
|
||||
|
||||
CreateMap<User, UserDto>();
|
||||
CreateMap<Friendship, FriendshipDto>();
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="12.0.1" />
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
|
||||
<PackageReference Include="BCrypt.Net-Next" Version="4.0.3" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.5" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
|
||||
|
||||
@@ -193,16 +193,25 @@ public class ChatsHub : Hub
|
||||
request.MessageId,
|
||||
request.NewEncryptedContent,
|
||||
DateTime.UtcNow);
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
var result = await _messageCommandService.EditMessageAsync(editMessageParam);
|
||||
|
||||
if(!result.IsSuccess)
|
||||
return LogAndError<MessageEditResponse>(editor, request.MessageId, "Edit message error", result.Exception);
|
||||
|
||||
|
||||
if (!result.IsSuccess)
|
||||
return LogAndError<MessageEditResponse>(editor, request.MessageId, "Edit message error",
|
||||
result.Exception);
|
||||
|
||||
return HubResult<MessageEditResponse>.Ok();
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
return LogAndUnauthorized<MessageEditResponse>(ex, "Unauthorized editing", editor, request.MessageId);
|
||||
}
|
||||
catch (KeyNotFoundException ex)
|
||||
{
|
||||
return LogAndNotFound<MessageEditResponse>(ex, "Message not found", editor, request.MessageId);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return LogAndError<MessageEditResponse>(editor, request.MessageId, "Unhandled exception error", ex);
|
||||
|
||||
@@ -96,6 +96,7 @@ builder.Services.AddSwaggerGen(options =>
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
//builder.Services.AddOpenApi();
|
||||
|
||||
var app = builder.Build();
|
||||
@@ -104,7 +105,7 @@ var app = builder.Build();
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
//app.MapOpenApi();
|
||||
|
||||
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
|
||||
Reference in New Issue
Block a user