using AutoMapper; using Microsoft.AspNetCore.Mvc; using Microsoft.SelfService.Portal.Core.API.Dto.Runbook.Get; using Microsoft.SelfService.Portal.Core.API.Dto.Runbook.Add; 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 RunbookController : Controller { private readonly IRunbookInterface _runbookInterface; private readonly IMapper _mapper; public RunbookController(IRunbookInterface runbookInterface, IMapper mapper) { _runbookInterface = runbookInterface; _mapper = mapper; } [HttpGet] [ProducesResponseType(200, Type = typeof(IEnumerable))] public IActionResult GetRunbooks() { var runbooks = _mapper.Map>(_runbookInterface.GetRunbooks()); if (!ModelState.IsValid) return BadRequest(ModelState); return Ok(runbooks); } [HttpGet("{Id}")] [ProducesResponseType(200, Type = typeof(RunbookModel))] [ProducesResponseType(400)] public IActionResult GetRunbookById(Guid Id) { if (!_runbookInterface.CheckRunbookById(Id)) return NotFound(); var runbook = _mapper.Map(_runbookInterface.GetRunbookById(Id)); if (!ModelState.IsValid) return BadRequest(ModelState); return Ok(runbook); } [HttpPost] [ProducesResponseType(204)] [ProducesResponseType(400)] public IActionResult AddRunbookById([FromBody] AddRunbookDto runbook) { if (runbook == null) return BadRequest(ModelState); if (_runbookInterface.CheckRunbookByName(runbook.Name)) { ModelState.AddModelError("", "Runbook already exists"); return StatusCode(422, ModelState); } if (!ModelState.IsValid) return BadRequest(ModelState); var runbookMap = _mapper.Map(runbook); if (!_runbookInterface.AddRunbookById(runbookMap)) { ModelState.AddModelError("", "Something went wrong while saving"); return StatusCode(500, ModelState); } return Ok("Successfully created"); } [HttpPut("{Id}")] [ProducesResponseType(204)] [ProducesResponseType(400)] [ProducesResponseType(404)] public IActionResult EditRunbookById(Guid Id, [FromBody] GetRunbookDetailsDto runbook) { if (runbook == null) return BadRequest(ModelState); if (Id != runbook.Id) return BadRequest(ModelState); if (!_runbookInterface.CheckRunbookById(Id)) return NotFound(); if (!ModelState.IsValid) return BadRequest(ModelState); var runbookMap = _mapper.Map(runbook); if (_runbookInterface.EditRunbookById(runbookMap)) { ModelState.AddModelError("", "Something went wrong"); return StatusCode(500, ModelState); } return NoContent(); } } }