163 lines
5.5 KiB
C#
163 lines
5.5 KiB
C#
using AutoMapper;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Environment.Add;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Environment.Edit;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Environment.Get;
|
|
using Microsoft.SelfService.Portal.Core.API.Dto.Service.Get;
|
|
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 EnvironmentController : Controller
|
|
{
|
|
private readonly IEnvironmentInterface _environmentInterface;
|
|
private readonly IMapper _mapper;
|
|
|
|
public EnvironmentController(IEnvironmentInterface environmentInterface, IMapper mapper)
|
|
{
|
|
_environmentInterface = environmentInterface;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
[HttpGet]
|
|
[ProducesResponseType(200, Type = typeof(IEnumerable<EnvironmentModel>))]
|
|
public IActionResult GetEnvironments()
|
|
{
|
|
var environments = _mapper.Map<List<GetEnvironmentDto>>(_environmentInterface.GetEnvironments());
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(environments);
|
|
}
|
|
|
|
[HttpGet("{Id}")]
|
|
[ProducesResponseType(200, Type = typeof(EnvironmentModel))]
|
|
[ProducesResponseType(400)]
|
|
public IActionResult GetEnvironmentByID(Guid Id)
|
|
{
|
|
|
|
if (!_environmentInterface.CheckEnvironmentById(Id))
|
|
return NotFound();
|
|
|
|
var environment = _mapper.Map<GetEnvironmentDetailsDto>(_environmentInterface.GetEnvironmentById(Id));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(environment);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
public IActionResult AddEnvironmentById([FromBody] AddEnvironmentDto environment)
|
|
{
|
|
|
|
if (environment == null)
|
|
return BadRequest(ModelState);
|
|
|
|
if (_environmentInterface.CheckEnvironmentByName(environment.Name))
|
|
{
|
|
ModelState.AddModelError("", "Domain already exists");
|
|
return StatusCode(422, ModelState);
|
|
}
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var environmentMap = _mapper.Map<EnvironmentModel>(environment);
|
|
|
|
if (!_environmentInterface.AddEnvironmentById(environmentMap))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong while saving");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return Ok("Successfully created");
|
|
}
|
|
|
|
[HttpDelete("{Id}")]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult DeleteEnvironmentById(Guid Id)
|
|
{
|
|
if (!_environmentInterface.CheckEnvironmentById(Id))
|
|
return NotFound();
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var domain = _environmentInterface.GetEnvironmentById(Id);
|
|
|
|
if (!_environmentInterface.DeleteEnvironmentById(domain))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong while deleting");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpPut("{Id}")]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(400)]
|
|
[ProducesResponseType(404)]
|
|
public IActionResult EditDomainById(Guid Id, [FromBody] EditEnvironmentDto environment)
|
|
{
|
|
if (environment == null)
|
|
return BadRequest(ModelState);
|
|
|
|
if (!_environmentInterface.CheckEnvironmentById(Id))
|
|
return NotFound();
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
var environmentMap = _mapper.Map<EnvironmentModel>(environment);
|
|
environmentMap.Id = Id;
|
|
|
|
if (!_environmentInterface.EditEnvironmentById(environmentMap))
|
|
{
|
|
ModelState.AddModelError("", "Something went wrong");
|
|
return StatusCode(500, ModelState);
|
|
}
|
|
|
|
return NoContent();
|
|
}
|
|
|
|
[HttpGet("{Id}/Domains")]
|
|
[ProducesResponseType(200, Type = typeof(EnvironmentModel))]
|
|
[ProducesResponseType(400)]
|
|
public IActionResult GetLinkedDomainsByEnvironmentId(Guid Id)
|
|
{
|
|
|
|
if (!_environmentInterface.CheckEnvironmentById(Id))
|
|
return NotFound();
|
|
|
|
var environment = _mapper.Map<GetEnvironmentDomainDto>(_environmentInterface.GetLinkedDomainsByEnvironmentId(Id));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(environment);
|
|
}
|
|
|
|
[HttpGet("{Id}/Templates")]
|
|
[ProducesResponseType(200, Type = typeof(IEnumerable<TemplateModel>))]
|
|
public IActionResult GetAvailableTemplatesByEnvironmentId(Guid Id)
|
|
{
|
|
var templates = _mapper.Map<List<GetTemplateDetailsDto>>(_environmentInterface.GetAvailableTemplatesByEnvironmentId(Id));
|
|
|
|
if (!ModelState.IsValid)
|
|
return BadRequest(ModelState);
|
|
|
|
return Ok(templates);
|
|
}
|
|
}
|
|
}
|