mirror of
https://github.com/Govor-team/Govor.git
synced 2026-07-21 19:54:55 +00:00
42348c863f
Introduces FriendshipsController for admin operations, including listing and removing friendships. Updates routing for admin controllers, improves error handling in UsersController, and enhances FriendsController and related services to include requester details in friendship DTOs. Refactors friend request acceptance to use query parameters and updates console client for improved user feedback and local development configuration.
78 lines
2.2 KiB
C#
78 lines
2.2 KiB
C#
using Govor.API.Services.AdminsStuff.Interfaces;
|
|
using Govor.Contracts.Responses.Admins;
|
|
using Govor.Core.Models;
|
|
using Govor.Data.Repositories.Exceptions;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace Govor.API.Controllers.AdminStuff;
|
|
|
|
|
|
[ApiController]
|
|
[Route("api/admin/[controller]")]
|
|
[Authorize(Roles = "Admin")]
|
|
public class UsersController : Controller
|
|
{
|
|
private readonly ILogger<UsersController> _logger;
|
|
private readonly IUsersAdministration _users;
|
|
|
|
public UsersController(ILogger<UsersController> logger, IUsersAdministration users, IInvitationGenerator invitationGenerator)
|
|
{
|
|
_logger = logger;
|
|
_users = users;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> AllUsers()
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation("Getting all users by administrator");
|
|
var read = await _users.GetAllUsersAsync();
|
|
|
|
return Ok(BuildUserDtos(read));
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, e.Message);
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
|
|
}
|
|
|
|
[HttpGet("{id:guid}")]
|
|
public async Task<IActionResult> GetUserById(Guid id)
|
|
{
|
|
try
|
|
{
|
|
_logger.LogInformation($"Getting user {id} by administrator");
|
|
var read = await _users.GetUserById(id);
|
|
return Ok(BuildUserDtos([read]).First());
|
|
}
|
|
catch (NotFoundByKeyException<Guid> ex)
|
|
{
|
|
_logger.LogWarning(ex, ex.Message);
|
|
return NotFound(ex.Message);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
_logger.LogError(e, e.Message);
|
|
return StatusCode(500, e.Message);
|
|
}
|
|
}
|
|
|
|
|
|
private List<UserResponse> BuildUserDtos(IEnumerable<User> users) => users.Select(user => new UserResponse
|
|
{
|
|
Id = user.Id,
|
|
Username = user.Username,
|
|
Description = user.Description,
|
|
WasOnline = user.WasOnline,
|
|
IconId = user.IconId,
|
|
PasswordHash = user.PasswordHash,
|
|
InviteId = user.InviteId,
|
|
CreatedOn = user.CreatedOn,
|
|
IsAdmin = user.Invite?.IsAdmin ?? false,
|
|
}).ToList();
|
|
|
|
} |