using AutoMapper; using Microsoft.AspNetCore.Mvc; using Microsoft.SelfService.Portal.Core.API.Dto.Template.Add; using Microsoft.SelfService.Portal.Core.API.Dto.Template.Edit; using Microsoft.SelfService.Portal.Core.API.Dto.Template.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 TemplateController : Controller { private readonly ITemplateInterface _templateInterface; private readonly IQueueJobService _queueJobService; private readonly IMapper _mapper; public TemplateController( ITemplateInterface templateInterface, IQueueJobService queueJobService, IMapper mapper) { _templateInterface = templateInterface; _queueJobService = queueJobService; _mapper = mapper; } [HttpGet] [ProducesResponseType(200, Type = typeof(IEnumerable))] public IActionResult GetTemplates() { var templates = _mapper.Map>(_templateInterface.GetTemplates()); if (!ModelState.IsValid) return BadRequest(ModelState); return Ok(templates); } [HttpGet("{Id}")] [ProducesResponseType(200, Type = typeof(TemplateModel))] [ProducesResponseType(404)] public IActionResult GetTemplateById(Guid Id) { if (!_templateInterface.CheckTemplateById(Id)) return NotFound(); var template = _mapper.Map(_templateInterface.GetTemplateById(Id)); if (!ModelState.IsValid) return BadRequest(ModelState); return Ok(template); } [HttpPost] [ProducesResponseType(200)] [ProducesResponseType(400)] public IActionResult AddTemplateById([FromBody] AddTemplateDto template) { if (template == null) return BadRequest(ModelState); if (_templateInterface.CheckTemplateByName(template.Name)) { ModelState.AddModelError("", "Template already exists"); return StatusCode(422, ModelState); } if (!ModelState.IsValid) return BadRequest(ModelState); var templateMap = _mapper.Map(template); if (!_templateInterface.AddTemplateById(templateMap)) { ModelState.AddModelError("", "Something went wrong while saving"); return StatusCode(500, ModelState); } return Ok(templateMap.Id); } [HttpPut("{Id}")] [ProducesResponseType(204)] [ProducesResponseType(400)] [ProducesResponseType(404)] public IActionResult EditTemplateById(Guid Id, [FromBody] EditTemplateDto template) { if (template == null) return BadRequest(ModelState); if (!_templateInterface.CheckTemplateById(Id)) return NotFound(); var existingTemplate = _templateInterface.GetTemplateById(Id); if (!ModelState.IsValid) return BadRequest(ModelState); var templateMap = _mapper.Map(template); templateMap.Id = Id; if (!_templateInterface.EditTemplateById(templateMap)) { ModelState.AddModelError("", "Something went wrong"); return StatusCode(500, ModelState); } if (!string.Equals(existingTemplate.JSONData, template.JSONData, StringComparison.Ordinal)) { _queueJobService.EnqueueTemplateJsonChanged(Id, existingTemplate.JSONData, template.JSONData); } return NoContent(); } [HttpDelete("{Id}")] [ProducesResponseType(204)] [ProducesResponseType(400)] [ProducesResponseType(404)] public IActionResult DeleteTemplateById(Guid Id) { if (!_templateInterface.CheckTemplateById(Id)) return NotFound(); if (!ModelState.IsValid) return BadRequest(ModelState); var template = _templateInterface.GetTemplateById(Id); if (!_templateInterface.DeleteTemplateById(template)) { ModelState.AddModelError("", "Something went wrong while deleting"); return StatusCode(500, ModelState); } return NoContent(); } } }