This migration updates the ScopesJson for the ApiClient with Id 91000000-0000-0000-0000-000000000001 to include additional authorization scopes for improved API access control. The Down method reverts the changes if necessary.
138 lines
5.0 KiB
C#
138 lines
5.0 KiB
C#
using AutoMapper;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.SelfService.Portal.Core.API.Authorization;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.TemplateCategory.Add;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.TemplateCategory.Edit;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.TemplateCategory.Get;
|
|
using Microsoft.SelfService.Portal.Core.API.Interfaces;
|
|
using Microsoft.SelfService.Portal.Core.API.Models;
|
|
|
|
namespace Microsoft.SelfService.Portal.Core.API.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize(Policy = ApiPolicies.TemplateRead)]
|
|
public class TemplateCategoryController : Controller
|
|
{
|
|
private readonly IMapper _mapper;
|
|
private readonly ITemplateCategoryInterface _templateCategoryInterface;
|
|
|
|
public TemplateCategoryController(IMapper mapper, ITemplateCategoryInterface templateCategoryInterface)
|
|
{
|
|
_mapper = mapper;
|
|
_templateCategoryInterface = templateCategoryInterface;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(200, Type = typeof(IEnumerable<TemplateCategoryModel>))]
|
|
public IActionResult GetTemplateCategories()
|
|
{
|
|
var templateCategories = _mapper.Map<List<GetTemplateCategoryDto>>(_templateCategoryInterface.GetTemplateCategories());
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(templateCategories);
|
|
}
|
|
|
|
[HttpGet("{Id}")]
|
|
[ProducesResponseType(200, Type = typeof(TemplateCategoryModel))]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult GetTemplateCategoryById(Guid Id)
|
|
{
|
|
if (!_templateCategoryInterface.CheckTemplateCategoryById(Id))
|
|
return NotFound();
|
|
|
|
var templateCategory = _mapper.Map<GetTemplateCategoryDetailsDto>(_templateCategoryInterface.GetTemplateCategoryById(Id));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(templateCategory);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize(Policy = ApiPolicies.TemplateWrite)]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(400)]
|
|
public IActionResult AddTemplateCategoryById([FromBody] AddTemplateCategoryDto templateCategory)
|
|
{
|
|
if (templateCategory == null)
|
|
return BadRequest(ModelState);
|
|
|
|
if (_templateCategoryInterface.CheckTemplateCategoryByName(templateCategory.Name))
|
|
{
|
|
ModelState.AddModelError("", "TemplateCategory already exists");
|
|
return StatusCode(422, ModelState);
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var templateCategoryMap = _mapper.Map<TemplateCategoryModel>(templateCategory);
|
|
|
|
if (!_templateCategoryInterface.AddTemplateCategoryById(templateCategoryMap))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong while saving");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return Ok(templateCategoryMap.Id);
|
|
}
|
|
|
|
[HttpPut("{Id}")]
|
|
[Authorize(Policy = ApiPolicies.TemplateWrite)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult EditTemplateCategoryById(Guid Id, [FromBody] EditTemplateCategoryDto templateCategory)
|
|
{
|
|
if (templateCategory == null)
|
|
return BadRequest(ModelState);
|
|
|
|
if (!_templateCategoryInterface.CheckTemplateCategoryById(Id))
|
|
return NotFound();
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var templateCategoryMap = _mapper.Map<TemplateCategoryModel>(templateCategory);
|
|
templateCategoryMap.Id = Id;
|
|
|
|
if (!_templateCategoryInterface.EditTemplateCategoryById(templateCategoryMap))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpDelete("{Id}")]
|
|
[Authorize(Policy = ApiPolicies.TemplateWrite)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult DeleteTemplateCategoryById(Guid Id)
|
|
{
|
|
if (!_templateCategoryInterface.CheckTemplateCategoryById(Id))
|
|
return NotFound();
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var templateCategory = _templateCategoryInterface.GetTemplateCategoryById(Id);
|
|
|
|
if (!_templateCategoryInterface.DeleteTemplateCategoryById(templateCategory))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong while deleting");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
}
|
|
}
|
|
|