mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
app.MapHub<FriendsHub>("/api/friends");
This commit is contained in:
@@ -1,212 +0,0 @@
|
||||
using Govor.Application.Exceptions.FriendsService;
|
||||
using Govor.Application.Interfaces;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Govor.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
[Authorize(Roles = "User,Admin")]
|
||||
public class FriendsController : Controller
|
||||
{
|
||||
private readonly ILogger<FriendsController> _logger;
|
||||
private readonly IFriendsService _friendsService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public FriendsController(ILogger<FriendsController> logger,
|
||||
IFriendsService friendsService,
|
||||
ICurrentUserService currentUserService)
|
||||
{
|
||||
_logger = logger;
|
||||
_friendsService = friendsService;
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
[HttpGet("search")] // api/friends/search?query=
|
||||
public async Task<IActionResult> Search(string query)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(query))
|
||||
return BadRequest("Query cannot be empty");
|
||||
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.SearchUsersAsync(query, _currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildUserDtos(result));
|
||||
}
|
||||
catch (SearchUsersException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return NotFound(new { error = ex.Message });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal error during user search." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("request")]
|
||||
public async Task<IActionResult> SendRequest(Guid targetUserId)
|
||||
{
|
||||
if (targetUserId == Guid.Empty)
|
||||
return BadRequest("Target user ID is invalid");
|
||||
|
||||
try
|
||||
{
|
||||
await _friendsService.SendFriendRequestAsync(_currentUserService.GetCurrentUserId(), targetUserId);
|
||||
return Ok(new { message = "Friend request sent successfully." });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Tried to send request to self");
|
||||
return UnprocessableEntity(new { error = ex.Message });
|
||||
}
|
||||
catch (RequestAlreadySentException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return Conflict(new { error = ex.Message });
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Failed to send friend request." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("requests")]
|
||||
public async Task<IActionResult> GetIncomingRequests()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetIncomingRequestsAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return BadRequest("Failed to get friend requests. User data missing.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("responses")]
|
||||
public async Task<IActionResult> GetResponses()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetResponsesAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return Ok(Array.Empty<FriendshipDto>());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("accept")]
|
||||
public async Task<IActionResult> AcceptFriend([FromQuery] Guid friendshipId)
|
||||
{
|
||||
if (friendshipId == Guid.Empty)
|
||||
return BadRequest("Requester ID is invalid");
|
||||
|
||||
try
|
||||
{
|
||||
await _friendsService.AcceptFriendRequestAsync(friendshipId, _currentUserService.GetCurrentUserId());
|
||||
return Ok(new { message = "Friend request accepted." });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return NotFound(new { error = ex.Message });
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return Forbid();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Failed to accept friend request." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("reject")]
|
||||
public async Task<IActionResult> RejectFriend([FromQuery] Guid friendshipId)
|
||||
{
|
||||
if (friendshipId == Guid.Empty)
|
||||
return BadRequest("Requester ID is invalid");
|
||||
|
||||
try
|
||||
{
|
||||
await _friendsService.RejectFriendRequestAsync(friendshipId, _currentUserService.GetCurrentUserId());
|
||||
return Ok(new { message = "Friend request rejected." });
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return NotFound(new { error = ex.Message });
|
||||
}
|
||||
catch (UnauthorizedAccessException ex)
|
||||
{
|
||||
_logger.LogWarning(ex, ex.Message);
|
||||
return Forbid();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Failed to accept friend request." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<IActionResult> GetFriends()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetFriendsAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildUserDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return Ok(Array.Empty<UserDto>());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
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();
|
||||
|
||||
private List<FriendshipDto> BuildFriendshipDtos(IEnumerable<Friendship> friendships) => friendships.Select(f => new FriendshipDto
|
||||
{
|
||||
Id = f.Id,
|
||||
Status = f.Status,
|
||||
AddresseeId = f.AddresseeId,
|
||||
RequesterId = f.RequesterId,
|
||||
}).ToList();
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
using Govor.Application.Interfaces.Friends;
|
||||
using Govor.Application.Interfaces.Infrastructure.Extensions;
|
||||
using Govor.Contracts.DTOs;
|
||||
using Govor.Core.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
@@ -10,11 +12,11 @@ namespace Govor.API.Controllers.Friends;
|
||||
[Route("api/friends")]
|
||||
public class FriendsRequestQueryController : Controller
|
||||
{
|
||||
private readonly ILogger<FriendsController> _logger;
|
||||
private readonly ILogger<FriendsRequestQueryController> _logger;
|
||||
private readonly IFriendRequestQueryService _friendsService;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public FriendsRequestQueryController(ILogger<FriendsController> logger,
|
||||
public FriendsRequestQueryController(ILogger<FriendsRequestQueryController> logger,
|
||||
IFriendRequestQueryService friendsService,
|
||||
ICurrentUserService currentUserService)
|
||||
{
|
||||
@@ -23,5 +25,51 @@ public class FriendsRequestQueryController : Controller
|
||||
_currentUserService = currentUserService;
|
||||
}
|
||||
|
||||
[HttpGet("requests")] // api/friends/requests
|
||||
public async Task<IActionResult> GetIncomingRequests()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetIncomingAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return BadRequest("Failed to get friend requests. User data missing.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("responses")]// api/friends/responses
|
||||
public async Task<IActionResult> GetResponses()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetResponsesAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildFriendshipDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return Ok(Array.Empty<FriendshipDto>());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
private List<FriendshipDto> BuildFriendshipDtos(IEnumerable<Friendship> friendships) => friendships.Select(f => new FriendshipDto
|
||||
{
|
||||
Id = f.Id,
|
||||
Status = f.Status,
|
||||
AddresseeId = f.AddresseeId,
|
||||
RequesterId = f.RequesterId,
|
||||
}).ToList();
|
||||
}
|
||||
@@ -10,12 +10,12 @@ namespace Govor.API.Controllers.Friends;
|
||||
[Route("api/friends")]
|
||||
public class FriendshipController : Controller
|
||||
{
|
||||
private readonly ILogger<FriendsController> _logger;
|
||||
private readonly ILogger<FriendshipController> _logger;
|
||||
private readonly IFriendshipService _friendsService;
|
||||
//private readonly IUserDtoBuilder _builder;
|
||||
private readonly ICurrentUserService _currentUserService;
|
||||
|
||||
public FriendshipController(ILogger<FriendsController> logger,
|
||||
public FriendshipController(ILogger<FriendshipController> logger,
|
||||
IFriendshipService friendsService,
|
||||
ICurrentUserService currentUserService)
|
||||
{
|
||||
@@ -47,6 +47,26 @@ public class FriendshipController : Controller
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet] // api/friends
|
||||
public async Task<IActionResult> GetFriends()
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _friendsService.GetFriendsAsync(_currentUserService.GetCurrentUserId());
|
||||
return Ok(BuildUserDtos(result));
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return Ok(Array.Empty<UserDto>());
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, ex.Message);
|
||||
return StatusCode(500, new { error = "Internal server error." });
|
||||
}
|
||||
}
|
||||
|
||||
private List<UserDto> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserDto
|
||||
{
|
||||
Id = user.Id,
|
||||
|
||||
Reference in New Issue
Block a user