mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 11:44:56 +00:00
FriendsController
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
using Govor.Application.Exceptions.FriendsService;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -9,48 +13,151 @@ namespace Govor.API.Controllers;
|
||||
public class FriendsController : Controller
|
||||
{
|
||||
private readonly ILogger<FriendsController> _logger;
|
||||
private readonly IFriendsService _friendsService;
|
||||
|
||||
[HttpGet("search")]
|
||||
public async Task<IActionResult> Search([FromBody] string query)
|
||||
public FriendsController(ILogger<FriendsController> logger, IFriendsService friendsService)
|
||||
{
|
||||
return BadRequest("Not a valid request");
|
||||
_logger = logger;
|
||||
_friendsService = friendsService;
|
||||
}
|
||||
|
||||
[HttpGet("search")]
|
||||
public async Task<IActionResult> Search(string query)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.SearchUsersAsync(query, GetCurrentUserId());
|
||||
|
||||
return Ok(BuildUserDtos(result));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return BadRequest("Something happened during the search... try again");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("request")]
|
||||
public async Task<IActionResult> SendRequest([FromBody] Guid targetUserId)
|
||||
public async Task<IActionResult> SendRequest(Guid targetUserId)
|
||||
{
|
||||
return BadRequest("Not a valid request");
|
||||
try
|
||||
{
|
||||
await _friendsService.SendFriendRequestAsync(targetUserId, GetCurrentUserId());
|
||||
return Ok();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning("The user tried to send a friend request to themselves");
|
||||
return BadRequest("You can't send a friend request to themselves");
|
||||
}
|
||||
catch (RequestAlreadySentException ex)
|
||||
{
|
||||
_logger.LogWarning("The user tried to send a friend request but the request is already sent");
|
||||
return BadRequest("You already sent a friend request");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return BadRequest("Something happened during the request... try again");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("requests")]
|
||||
public async Task<IActionResult> GetIncomingRequests()
|
||||
{
|
||||
return BadRequest("Not a valid request");
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Getting incoming requests for user {GetCurrentUserId()}...");
|
||||
var result = await _friendsService.GetIncomingRequestsAsync(GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return BadRequest("You can't get real friend requests because we can't find your user data! Try again");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("accept")]
|
||||
public async Task<IActionResult> AcceptFriend([FromBody] Guid requesterId)
|
||||
public async Task<IActionResult> AcceptFriend(Guid requesterId)
|
||||
{
|
||||
return BadRequest("Not a valid request");
|
||||
try
|
||||
{
|
||||
await _friendsService.AcceptFriendRequestAsync(requesterId, GetCurrentUserId());
|
||||
return Ok();
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning("The user tried to accept a friend request but the request not exists");
|
||||
return BadRequest(ex.Message);
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning("User tried to accept a friend request but addresseeId not equal userId");
|
||||
return BadRequest("You can't accept a friend request");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetFriends()
|
||||
{
|
||||
return BadRequest("Not a valid request");
|
||||
try
|
||||
{
|
||||
_logger.LogInformation($"Getting friends by user {GetCurrentUserId()}...");
|
||||
var result = await _friendsService.GetFriendsAsync(GetCurrentUserId());
|
||||
|
||||
return Ok(BuildUserDtos(result));
|
||||
}
|
||||
catch(UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return BadRequest("Something happened during the request... try again");
|
||||
}
|
||||
}
|
||||
|
||||
private Guid GetCurrentUserId()
|
||||
{
|
||||
var userIdClaim = HttpContext.User?.FindFirst("userID")?.Value;
|
||||
_logger.LogInformation("Claims: {Claims}", string.Join(", ", HttpContext.User?.Claims.Select(c => $"{c.Type}: {c.Value}") ?? new string[0]));
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim))
|
||||
if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
||||
{
|
||||
_logger.LogError("No userID claim found");
|
||||
return Guid.Empty;
|
||||
throw new UnauthorizedAccessException("userID claim is missing or invalid");
|
||||
}
|
||||
return userId;
|
||||
}
|
||||
|
||||
private List<UserDto> BuildUserDtos(IEnumerable<User> users)
|
||||
{
|
||||
List<UserDto> userDtos = new List<UserDto>();
|
||||
|
||||
foreach (var user in users)
|
||||
{
|
||||
userDtos.Add(new UserDto()
|
||||
{
|
||||
Id = user.Id,
|
||||
Description = user.Description,
|
||||
Username = user.Username,
|
||||
WasOnline = user.WasOnline,
|
||||
IconId = user.IconId,
|
||||
});
|
||||
}
|
||||
return userDtos;
|
||||
}
|
||||
|
||||
private List<FriendshipDto> BuildFriendshipDtos(List<Friendship> result)
|
||||
{
|
||||
List<FriendshipDto> dtos = new List<FriendshipDto>();
|
||||
|
||||
foreach (var friendship in result)
|
||||
{
|
||||
dtos.Add(new FriendshipDto()
|
||||
{
|
||||
Id = friendship.Id,
|
||||
Status = friendship.Status,
|
||||
AddresseeId = friendship.AddresseeId,
|
||||
RequesterId = friendship.RequesterId,
|
||||
});
|
||||
}
|
||||
|
||||
return Guid.TryParse(userIdClaim, out var userId) ? userId : Guid.Empty;
|
||||
return dtos;
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ using Govor.Core.Infrastructure.Extensions;
|
||||
using Govor.Core.Infrastructure.Validators;
|
||||
using Govor.Core.Models;
|
||||
using Govor.Core.Repositories.Admins;
|
||||
using Govor.Core.Repositories.Friendships;
|
||||
using Govor.Core.Repositories.Invaites;
|
||||
using Govor.Core.Repositories.MediasAttachments;
|
||||
using Govor.Core.Repositories.Messages;
|
||||
@@ -30,6 +31,7 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IInvitesService, InvitesService>();
|
||||
services.AddScoped<IInvitationGenerator, InvitationGenerator>();
|
||||
services.AddScoped<IUsernameValidator, UsernameValidator>();
|
||||
services.AddScoped<IFriendsService, FriendsService>();
|
||||
|
||||
services.AddScoped<IStorageService>(sp =>
|
||||
{
|
||||
@@ -45,6 +47,7 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IInvitesRepository, InvitesRepository>();
|
||||
services.AddScoped<IAdminsRepository, AdminsRepository>();
|
||||
services.AddScoped<IMediaAttachmentsRepository, MediaAttachmentsRepository>();
|
||||
services.AddScoped<IFriendshipsRepository, FriendshipsRepository>();
|
||||
}
|
||||
|
||||
public static void AddValidators(this IServiceCollection services)
|
||||
@@ -54,6 +57,7 @@ public static class ConfigurationProgramExtensions
|
||||
services.AddScoped<IObjectValidator<MediaAttachments>, MediaAttachmentsValidator>();
|
||||
services.AddScoped<IObjectValidator<Admin>, AdminValidator>();
|
||||
services.AddScoped<IObjectValidator<Invitation>, InvitationValidator>();
|
||||
services.AddScoped<IObjectValidator<Friendship>, FriendshipValidator>();
|
||||
}
|
||||
|
||||
public static void AddGovorDbContext(this IServiceCollection services, IConfiguration configuration)
|
||||
|
||||
@@ -98,14 +98,10 @@ public class ChatsHub : Hub
|
||||
private Guid GetUserId()
|
||||
{
|
||||
var userIdClaim = Context.User?.FindFirst("userID")?.Value;
|
||||
_logger.LogInformation("Claims: {Claims}", string.Join(", ", Context.User?.Claims.Select(c => $"{c.Type}: {c.Value}") ?? new string[0]));
|
||||
|
||||
if (string.IsNullOrEmpty(userIdClaim))
|
||||
if (string.IsNullOrEmpty(userIdClaim) || !Guid.TryParse(userIdClaim, out var userId))
|
||||
{
|
||||
_logger.LogError("No userID claim found");
|
||||
return Guid.Empty;
|
||||
throw new UnauthorizedAccessException("userID claim is missing or invalid");
|
||||
}
|
||||
|
||||
return Guid.TryParse(userIdClaim, out var userId) ? userId : Guid.Empty;
|
||||
return userId;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user