adminuser
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
// Auto-generiert von CreateWebshopFiles.ps1
|
||||
// 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
|
||||
{
|
||||
@@ -23,18 +24,72 @@ namespace Webshop.Api.Controllers.Admin
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<IEnumerable<UserDto>>> GetAllUsers()
|
||||
[ProducesResponseType(typeof(IEnumerable<UserDto>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> GetAllUsers()
|
||||
{
|
||||
var users = await _adminUserService.GetAllUsersAsync();
|
||||
return Ok(users);
|
||||
var result = await _adminUserService.GetAllUsersAsync();
|
||||
return Ok(result.Value);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpGet("{userId}")]
|
||||
public async Task<ActionResult<UserDto>> GetUserById(string userId)
|
||||
[ProducesResponseType(typeof(UserDto), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
|
||||
public async Task<IActionResult> GetUserById(string userId)
|
||||
{
|
||||
var user = await _adminUserService.GetUserByIdAsync(userId);
|
||||
if (user == null) return NotFound();
|
||||
return Ok(user);
|
||||
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>();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user