Files
Microsoft.SelfService.Porta…/Controllers/DeploymentController.cs
2026-04-15 15:02:32 +02:00

136 lines
4.5 KiB
C#

using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Microsoft.SelfService.Portal.Core.API.Dto.Deployment.Add;
using Microsoft.SelfService.Portal.Core.API.Dto.Deployment.Get;
using Microsoft.SelfService.Portal.Core.API.Dto.Deployment.Edit;
using Microsoft.SelfService.Portal.Core.API.Interfaces;
using Microsoft.SelfService.Portal.Core.API.Models;
using Microsoft.SelfService.Portal.Core.API.Events.Interfaces;
using System.Reflection;
using System.Collections;
using System.Dynamic;
using System.Text.Json;
namespace Microsoft.SelfService.Portal.Core.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DeploymentController : Controller
{
private readonly IDeploymentInterface _deploymentInterface;
private readonly IMapper _mapper;
private readonly IEventHandlerInterface _eventHandlerInterface;
public DeploymentController(IDeploymentInterface deploymentInterface, IMapper mapper, IEventHandlerInterface eventHandlerInterface)
{
_deploymentInterface = deploymentInterface;
_mapper = mapper;
_eventHandlerInterface = eventHandlerInterface;
}
[HttpGet]
[ProducesResponseType(200, Type = typeof(IEnumerable<DeploymentModel>))]
public IActionResult GetDeployments()
{
var deployment = _mapper.Map<List<GetDeploymentDto>>(_deploymentInterface.GetDeployments());
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok(deployment);
}
[HttpGet("{Id}")]
[ProducesResponseType(200, Type = typeof(DeploymentModel))]
[ProducesResponseType(400)]
public IActionResult GetDeploymentById(Guid Id)
{
if (!_deploymentInterface.CheckDeploymentById(Id))
return NotFound();
var deployment = _mapper.Map<GetDeploymentDetailsDto>(_deploymentInterface.GetDeploymentById(Id));
if (!ModelState.IsValid)
return BadRequest(ModelState);
return Ok(deployment);
}
[HttpPost]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
public IActionResult AddDeploymentById([FromBody] AddDeploymentDto deployment)
{
if (deployment == null)
return BadRequest(ModelState);
if (!ModelState.IsValid)
return BadRequest(ModelState);
var deploymentMap = _mapper.Map<DeploymentModel>(deployment);
if (!_deploymentInterface.AddDeploymentById(deploymentMap))
{
ModelState.AddModelError("", "Something went wrong while saving");
return StatusCode(500, ModelState);
}
return Ok(deploymentMap.Id);
}
[HttpDelete("{Id}")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public IActionResult DeleteDeploymentById(Guid Id)
{
if (!_deploymentInterface.CheckDeploymentById(Id))
return NotFound();
if (!ModelState.IsValid)
return BadRequest(ModelState);
var deployment = _deploymentInterface.GetDeploymentById(Id);
if (!_deploymentInterface.DeleteDeploymentById(deployment))
{
ModelState.AddModelError("", "Something went wrong while deleting");
return StatusCode(500, ModelState);
}
return NoContent();
}
[HttpPut("{Id}")]
[ProducesResponseType(204)]
[ProducesResponseType(400)]
[ProducesResponseType(404)]
public IActionResult EditDeploymentById(Guid Id, [FromBody] EditDeploymentDto deployment)
{
if (deployment == null)
return BadRequest(ModelState);
if (!_deploymentInterface.CheckDeploymentById(Id))
return NotFound();
if (!ModelState.IsValid)
return BadRequest(ModelState);
var deploymentMap = _mapper.Map<DeploymentModel>(deployment);
deploymentMap.Id = Id;
if (!_deploymentInterface.EditDeploymentById(deploymentMap))
{
ModelState.AddModelError("", "Something went wrong");
return StatusCode(500, ModelState);
}
_eventHandlerInterface.FireEvent(this.GetType().Name, MethodBase.GetCurrentMethod().Name, Id);
return NoContent();
}
}
}