PIAPS_CW/RestAPI/Controllers/RoleController.cs

120 lines
2.4 KiB
C#
Raw Normal View History

2024-06-05 01:25:18 +04:00
using BusinessLogic.BusinessLogic;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
using Contracts.Exceptions;
using Contracts.SearchModels;
using Contracts.ViewModels;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Mvc;
namespace RestAPI.Controllers
{
[Route("[controller]/[action]")]
[ApiController]
public class RoleController
{
private readonly ILogger _logger;
private readonly IRoleLogic _roleLogic;
public RoleController(ILogger<RoleController> logger, IRoleLogic roleLogic)
{
_logger = logger;
_roleLogic = roleLogic;
}
[HttpGet]
public IResult Get([FromQuery] RoleSearchModel model)
{
try
{
var res = _roleLogic.ReadElement(model);
return Results.Ok(res);
}
catch (ElementNotFoundException ex)
{
_logger.LogInformation(ex, "Role not found");
2024-06-05 01:25:18 +04:00
return Results.NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error get role");
return Results.Problem(ex.Message);
}
}
[HttpGet]
public IResult GeFilteredtList([FromQuery] RoleSearchModel model)
{
try
{
var res = _roleLogic.ReadElements(model).ToList();
return Results.Ok(res);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error get list role");
return Results.Problem(ex.Message);
}
}
[HttpGet]
public IResult GetList()
{
try
{
var res = _roleLogic.ReadElements(null).ToList();
return Results.Ok(res);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error get list role");
return Results.Problem(ex.Message);
}
}
[HttpPost]
public IResult Create([FromBody] RoleBindingModel model)
{
try
{
var res = _roleLogic.Create(model);
return Results.Created(string.Empty, res);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error create role");
return Results.Problem(ex.Message);
}
}
[HttpPatch]
2024-06-05 01:25:18 +04:00
public IResult Update([FromBody] RoleBindingModel model)
{
try
{
var res = _roleLogic.Update(model);
return Results.Ok(res);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error update role");
return Results.Problem(ex.Message);
}
}
[HttpDelete]
public IResult Delete([FromQuery] RoleSearchModel model)
{
try
{
var res = _roleLogic.Delete(model); ;
return Results.Ok(res);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error delete role");
return Results.Problem(ex.Message);
}
}
}
}