90 lines
3.2 KiB
C#
90 lines
3.2 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Knot.Modules.Identity.Application.Friends;
|
|
using Knot.Shared.Kernel;
|
|
using MediatR;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Host.Controllers;
|
|
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/friends")]
|
|
public sealed class FriendsController : ControllerBase
|
|
{
|
|
private readonly ISender _sender;
|
|
private readonly IUserContext _userContext;
|
|
|
|
public FriendsController(ISender sender, IUserContext userContext)
|
|
{
|
|
_sender = sender;
|
|
_userContext = userContext;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetFriends(CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new GetFriendsQuery(_userContext.UserId), ct);
|
|
if (result.IsFailure) return BadRequest(result.Error);
|
|
return Ok(result.Value);
|
|
}
|
|
|
|
[HttpGet("requests")]
|
|
public async Task<IActionResult> GetRequests(CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new GetIncomingRequestsQuery(_userContext.UserId), ct);
|
|
if (result.IsFailure) return BadRequest(result.Error);
|
|
return Ok(result.Value);
|
|
}
|
|
|
|
[HttpGet("outgoing")]
|
|
public async Task<IActionResult> GetOutgoingRequests(CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new GetOutgoingRequestsQuery(_userContext.UserId), ct);
|
|
if (result.IsFailure) return BadRequest(result.Error);
|
|
return Ok(result.Value);
|
|
}
|
|
|
|
[HttpPost("request")]
|
|
public async Task<IActionResult> SendRequest([FromBody] Host.Models.SendFriendRequest request, CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new SendFriendRequestCommand(_userContext.UserId, request.FriendId), ct);
|
|
if (result.IsFailure) return BadRequest(result.Error.Description);
|
|
return Ok(new { status = "pending" });
|
|
}
|
|
|
|
[HttpPost("{id:guid}/accept")]
|
|
public async Task<IActionResult> AcceptRequest(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new AcceptFriendRequestCommand(_userContext.UserId, id), ct);
|
|
if (result.IsFailure) return NotFound(result.Error.Description);
|
|
return Ok(new { id = result.Value });
|
|
}
|
|
|
|
[HttpPost("{id:guid}/decline")]
|
|
public async Task<IActionResult> DeclineRequest(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new DeclineFriendRequestCommand(_userContext.UserId, id), ct);
|
|
if (result.IsFailure) return NotFound(result.Error.Description);
|
|
return Ok(new Knot.Shared.Kernel.SuccessResponse(true));
|
|
}
|
|
|
|
[HttpGet("status/{userId:guid}")]
|
|
public async Task<IActionResult> GetStatus(Guid userId, CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new GetFriendshipStatusQuery(_userContext.UserId, userId), ct);
|
|
if (result.IsFailure) return BadRequest(result.Error);
|
|
return Ok(result.Value);
|
|
}
|
|
|
|
[HttpDelete("{id:guid}")]
|
|
public async Task<IActionResult> RemoveFriend(Guid id, CancellationToken ct)
|
|
{
|
|
var result = await _sender.Send(new RemoveFriendCommand(_userContext.UserId, id), ct);
|
|
if (result.IsFailure) return NotFound(result.Error.Description);
|
|
return Ok(new Knot.Shared.Kernel.SuccessResponse(true));
|
|
}
|
|
}
|