131 lines
4.6 KiB
C#
131 lines
4.6 KiB
C#
using AutoMapper;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
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]
|
|
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]
|
|
[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}")]
|
|
[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}")]
|
|
[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();
|
|
}
|
|
}
|
|
}
|