#nullable disable
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MediatR;
using CORE.APP.Models;
using Users.APP.Features.Roles;
using Microsoft.AspNetCore.Authorization;
//Generated from Custom Microservices Template.
namespace Users.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "Admin")] // Only authenticated users with role Admin can execute all of the actions of this controller.
public class RolesController : ControllerBase
{
private readonly ILogger<RolesController> _logger;
private readonly IMediator _mediator;
// Constructor: injects logger to log the errors to Kestrel Console or Output Window and mediator
public RolesController(ILogger<RolesController> logger, IMediator mediator)
{
_logger = logger;
_mediator = mediator;
}
// GET: api/Roles
[HttpGet]
public async Task<IActionResult> Get()
{
try
{
// Send a query request to get query response
var response = await _mediator.Send(new RoleQueryRequest());
// Convert the query response to a list
var list = await response.ToListAsync();
// If there are items, return them with 200 OK
if (list.Any())
return Ok(list);
// If no items found, return 204 No Content
return NoContent();
}
catch (Exception exception)
{
// Log the exception
_logger.LogError("RolesGet Exception: " + exception.Message);
// Return 500 Internal Server Error with an error command response with message
return StatusCode(StatusCodes.Status500InternalServerError, new CommandResponse(false, "An exception occured during RolesGet."));
}
}
// GET: api/Roles/5
[HttpGet("{id}")]
public async Task<IActionResult> Get(int id)
{
try
{
// Send a query request to get query response
var response = await _mediator.Send(new RoleQueryRequest());
// Find the item with the given id
var item = await response.SingleOrDefaultAsync(r => r.Id == id);
// If item found, return it with 200 OK
if (item is not null)
return Ok(item);
// If item not found, return 204 No Content
return NoContent();
}
catch (Exception exception)
{
// Log the exception
_logger.LogError("RolesGetById Exception: " + exception.Message);
// Return 500 Internal Server Error with an error command response with message
return StatusCode(StatusCodes.Status500InternalServerError, new CommandResponse(false, "An exception occured during RolesGetById."));
}
}
// POST: api/Roles
[HttpPost]
public async Task<IActionResult> Post(RoleCreateRequest request)
{
try
{
// Check if the request model is valid through data annotations
if (ModelState.IsValid)
{
// Send the create request
var response = await _mediator.Send(request);
// If creation is successful, return 200 OK with success command response
if (response.IsSuccessful)
{
//return CreatedAtAction(nameof(Get), new { id = response.Id }, response);
return Ok(response);
}
// If creation failed, add error command response message to model state
ModelState.AddModelError("RolesPost", response.Message);
}
// Return 400 Bad Request with all data annotation validation error messages and the error command response message if added seperated by |
return BadRequest(new CommandResponse(false, string.Join("|", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage))));
}
catch (Exception exception)
{
// Log the exception
_logger.LogError("RolesPost Exception: " + exception.Message);
// Return 500 Internal Server Error with an error command response with message
return StatusCode(StatusCodes.Status500InternalServerError, new CommandResponse(false, "An exception occured during RolesPost."));
}
}
// PUT: api/Roles
[HttpPut]
public async Task<IActionResult> Put(RoleUpdateRequest request)
{
try
{
// Check if the request model is valid through data annotations
if (ModelState.IsValid)
{
// Send the update request
var response = await _mediator.Send(request);
// If update is successful, return 200 OK with success command response
if (response.IsSuccessful)
{
//return NoContent();
return Ok(response);
}
// If update failed, add error command response message to model state
ModelState.AddModelError("RolesPut", response.Message);
}
// Return 400 Bad Request with all data annotation validation error messages and the error command response message if added seperated by |
return BadRequest(new CommandResponse(false, string.Join("|", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage))));
}
catch (Exception exception)
{
// Log the exception
_logger.LogError("RolesPut Exception: " + exception.Message);
// Return 500 Internal Server Error with an error command response with message
return StatusCode(StatusCodes.Status500InternalServerError, new CommandResponse(false, "An exception occured during RolesPut."));
}
}
// DELETE: api/Roles/5
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
try
{
// Send the delete request
var response = await _mediator.Send(new RoleDeleteRequest() { Id = id });
// If delete is successful, return 200 OK with success command response
if (response.IsSuccessful)
{
//return NoContent();
return Ok(response);
}
// If delete failed, add error command response message to model state
ModelState.AddModelError("RolesDelete", response.Message);
// Return 400 Bad Request with the error command response message
return BadRequest(new CommandResponse(false, string.Join("|", ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage))));
}
catch (Exception exception)
{
// Log the exception
_logger.LogError("RolesDelete Exception: " + exception.Message);
// Return 500 Internal Server Error with an error command response with message
return StatusCode(StatusCodes.Status500InternalServerError, new CommandResponse(false, "An exception occured during RolesDelete."));
}
}
}
}