95 lines
3.9 KiB
C#
95 lines
3.9 KiB
C#
// src/Webshop.Api/Controllers/Admin/AdminUsersController.cs
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Webshop.Application.DTOs.Users;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Threading.Tasks;
|
||
using Webshop.Application;
|
||
using Webshop.Application.Services.Admin.Interfaces;
|
||
using Microsoft.AspNetCore.Http;
|
||
|
||
namespace Webshop.Api.Controllers.Admin
|
||
{
|
||
[ApiController]
|
||
[Route("api/v1/admin/[controller]")]
|
||
[Authorize(Roles = "Admin")]
|
||
public class AdminUsersController : ControllerBase
|
||
{
|
||
private readonly IAdminUserService _adminUserService;
|
||
|
||
public AdminUsersController(IAdminUserService adminUserService)
|
||
{
|
||
_adminUserService = adminUserService;
|
||
}
|
||
|
||
[HttpGet]
|
||
[ProducesResponseType(typeof(IEnumerable<UserDto>), StatusCodes.Status200OK)]
|
||
public async Task<IActionResult> GetAllUsers()
|
||
{
|
||
var result = await _adminUserService.GetAllUsersAsync();
|
||
return Ok(result.Value);
|
||
}
|
||
|
||
|
||
|
||
[HttpGet("{userId}")]
|
||
[ProducesResponseType(typeof(UserDto), StatusCodes.Status200OK)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> GetUserById(string userId)
|
||
{
|
||
var result = await _adminUserService.GetUserByIdAsync(userId);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => Ok(result.Value),
|
||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
|
||
[HttpPut("{userId}/roles")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> UpdateUserRoles(string userId, [FromBody] UpdateUserRolesRequest request)
|
||
{
|
||
if (!ModelState.IsValid)
|
||
{
|
||
return BadRequest(ModelState);
|
||
}
|
||
|
||
var result = await _adminUserService.UpdateUserRolesAsync(userId, request.NewRoles);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => NoContent(),
|
||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||
ServiceResultType.Failure => BadRequest(new { Message = result.ErrorMessage }), // Identity errors are often validation-like
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
|
||
[HttpDelete("{userId}")]
|
||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||
public async Task<IActionResult> DeleteUser(string userId)
|
||
{
|
||
var result = await _adminUserService.DeleteUserAsync(userId);
|
||
|
||
return result.Type switch
|
||
{
|
||
ServiceResultType.Success => NoContent(),
|
||
ServiceResultType.NotFound => NotFound(new { Message = result.ErrorMessage }),
|
||
ServiceResultType.Failure => BadRequest(new { Message = result.ErrorMessage }),
|
||
_ => StatusCode(StatusCodes.Status500InternalServerError, new { Message = result.ErrorMessage ?? "Ein unerwarteter Fehler ist aufgetreten." })
|
||
};
|
||
}
|
||
|
||
// Kleines DTO f<>r die Anfrage zum Rollen-Update
|
||
public class UpdateUserRolesRequest
|
||
{
|
||
public List<string> NewRoles { get; set; } = new List<string>();
|
||
}
|
||
}
|
||
} |