using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using UserManagement.Data.Dto;
using UserManagement.MediatR.Commands;
using UserManagement.MediatR.Queries;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UserManagement.Api.Helpers;
namespace UserManagement.API.Controllers
{
///
/// Action
///
[Route("api")]
[ApiController]
[Authorize]
public class ActionController : BaseController
{
public IMediator _mediator { get; set; }
///
/// Action
///
///
public ActionController(IMediator mediator)
{
_mediator = mediator;
}
///
/// Get Action By Id
///
///
///
[HttpGet("Action/{id}", Name = "GetAction")]
[Produces("application/json", "application/xml", Type = typeof(ActionDto))]
[ClaimCheck("action_list")]
public async Task GetAction(Guid id)
{
var getActionQuery = new GetActionQuery { Id = id };
var result = await _mediator.Send(getActionQuery);
return ReturnFormattedResponse(result);
}
///
/// Get All Actions
///
///
[HttpGet("Actions")]
[ClaimCheck("action_list", "role_add", "role_edit", "page_action_edit", "user_permission_edit")]
[Produces("application/json", "application/xml", Type = typeof(List))]
public async Task GetActions()
{
var getAllActionQuery = new GetAllActionQuery { };
var result = await _mediator.Send(getAllActionQuery);
return ReturnFormattedResponse(result);
}
///
/// Create A Action
///
///
///
[HttpPost("Action")]
[ClaimCheck("action_add")]
[Produces("application/json", "application/xml", Type = typeof(ActionDto))]
public async Task AddAction(AddActionCommand addActionCommand)
{
var response = await _mediator.Send(addActionCommand);
if (!response.Success)
{
return ReturnFormattedResponse(response);
}
return CreatedAtAction("GetAction", new { id = response.Data.Id }, response.Data);
}
///
/// Update Exist Action By Id
///
///
///
///
[HttpPut("Action/{Id}")]
[ClaimCheck("action_edit")]
[Produces("application/json", "application/xml", Type = typeof(ActionDto))]
public async Task UpdateAction(Guid Id, UpdateActionCommand updateActionCommand)
{
updateActionCommand.Id = Id;
var result = await _mediator.Send(updateActionCommand);
return ReturnFormattedResponse(result);
}
///
/// Delete Action By Id
///
///
///
[HttpDelete("Action/{Id}")]
[ClaimCheck("action_delete")]
public async Task DeleteAction(Guid Id)
{
var deleteActionCommand = new DeleteActionCommand { Id = Id };
var result = await _mediator.Send(deleteActionCommand);
return ReturnFormattedResponse(result);
}
}
}