initial commit
This commit is contained in:
230
Controllers/DomainController.cs
Normal file
230
Controllers/DomainController.cs
Normal file
@@ -0,0 +1,230 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.SelfService.Portal.Core.API.Dto.Domain.Add;
|
||||
using Microsoft.SelfService.Portal.Core.API.Dto.Domain.Edit;
|
||||
using Microsoft.SelfService.Portal.Core.API.Dto.Domain.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 DomainController : Controller
|
||||
{
|
||||
private readonly IDomainInterface _domainInterface;
|
||||
private readonly IEnvironmentInterface _environmentInterface;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public DomainController(IDomainInterface domainInterface,IEnvironmentInterface environmentInterface, IMapper mapper)
|
||||
{
|
||||
_domainInterface = domainInterface;
|
||||
_environmentInterface = environmentInterface;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[ProducesResponseType(200, Type = typeof(IEnumerable<DomainModel>))]
|
||||
public IActionResult GetDomains()
|
||||
{
|
||||
var domains = _mapper.Map<List<GetDomainDto>>(_domainInterface.GetDomains());
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
return Ok(domains);
|
||||
}
|
||||
|
||||
[HttpGet("{Id}")]
|
||||
[ProducesResponseType(200, Type = typeof(DomainModel))]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult GetDomainById(Guid Id)
|
||||
{
|
||||
|
||||
if (!_domainInterface.CheckDomainById(Id))
|
||||
return NotFound();
|
||||
|
||||
var domain = _mapper.Map<GetDomainDetailsDto>(_domainInterface.GetDomainById(Id));
|
||||
|
||||
if(!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
return Ok(domain);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
[ProducesResponseType(204)]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult AddDomainById([FromBody] AddDomainDto domain)
|
||||
{
|
||||
|
||||
if (domain == null)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
if (_domainInterface.CheckDomainByName(domain.Name))
|
||||
{
|
||||
ModelState.AddModelError("", "Domain already exists");
|
||||
return StatusCode(422, ModelState);
|
||||
}
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var domainMap = _mapper.Map<DomainModel>(domain);
|
||||
|
||||
if (!_domainInterface.AddDomainById(domainMap))
|
||||
{
|
||||
ModelState.AddModelError("", "Something went wrong while saving");
|
||||
return StatusCode(500, ModelState);
|
||||
}
|
||||
|
||||
//return Ok("Successfully created");
|
||||
return Ok(domainMap.Id);
|
||||
}
|
||||
|
||||
[HttpDelete("{Id}")]
|
||||
[ProducesResponseType(204)]
|
||||
[ProducesResponseType(400)]
|
||||
[ProducesResponseType(404)]
|
||||
public IActionResult DeleteDomainById(Guid Id)
|
||||
{
|
||||
if (!_domainInterface.CheckDomainById(Id))
|
||||
return NotFound();
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var domain = _domainInterface.GetDomainById(Id);
|
||||
|
||||
if (!_domainInterface.DeleteDomainById(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] EditDomainDto domain)
|
||||
{
|
||||
if (domain == null)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
if (!_domainInterface.CheckDomainById(Id))
|
||||
return NotFound();
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var domainMap = _mapper.Map<DomainModel>(domain);
|
||||
domainMap.Id = Id;
|
||||
|
||||
if (!_domainInterface.EditDomainById(domainMap))
|
||||
{
|
||||
ModelState.AddModelError("", "Something went wrong");
|
||||
return StatusCode(500, ModelState);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{Id}/Environments")]
|
||||
[ProducesResponseType(200, Type = typeof(DomainModel))]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult GetDomainByIdInEnvironments(Guid Id)
|
||||
{
|
||||
|
||||
if (!_domainInterface.CheckDomainById(Id))
|
||||
return NotFound();
|
||||
|
||||
var domain = _mapper.Map<GetDomainEnvironmentDto>(_domainInterface.GetDomainByIdInEnvironments(Id));
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
return Ok(domain);
|
||||
}
|
||||
|
||||
[HttpPost("{DomainId}/Environment/{EnvironmentId}")]
|
||||
[ProducesResponseType(200, Type = typeof(EnvironmentDomainsModel))]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult LinkDomainByIdToEnvironment(Guid DomainId, Guid EnvironmentId)
|
||||
{
|
||||
|
||||
if(!_domainInterface.CheckDomainById(DomainId))
|
||||
return new NotFoundObjectResult("Domain with Id was not found");
|
||||
|
||||
if(!_environmentInterface.CheckEnvironmentById(EnvironmentId))
|
||||
return new NotFoundObjectResult("Environment with Id was not found");
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var environmentDomain = new EnvironmentDomainsModel
|
||||
{
|
||||
DomainId = DomainId,
|
||||
Domain = _domainInterface.GetDomainById(DomainId),
|
||||
EnvironmentId = EnvironmentId,
|
||||
Environment = _environmentInterface.GetEnvironmentById(EnvironmentId)
|
||||
};
|
||||
|
||||
var environmentDomainMap = _mapper.Map<EnvironmentDomainsModel>(environmentDomain);
|
||||
|
||||
if (!_domainInterface.LinkDomainByIdToEnvironment(environmentDomainMap))
|
||||
{
|
||||
ModelState.AddModelError("", "Something went wrong while saving");
|
||||
return StatusCode(500, ModelState);
|
||||
}
|
||||
|
||||
return Ok("Successfully created");
|
||||
}
|
||||
|
||||
[HttpDelete("{DomainId}/Environment/{EnvironmentId}")]
|
||||
[ProducesResponseType(200, Type = typeof(EnvironmentDomainsModel))]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult UnlinkDomainByIdFromEnvironment(Guid DomainId, Guid EnvironmentId)
|
||||
{
|
||||
if (!_domainInterface.CheckDomainById(DomainId) & !_environmentInterface.CheckEnvironmentById(EnvironmentId))
|
||||
return new NotFoundObjectResult("Domain with Id is not linked to Environment");
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
var environmentDomain = new EnvironmentDomainsModel
|
||||
{
|
||||
DomainId = DomainId,
|
||||
Domain = _domainInterface.GetDomainById(DomainId),
|
||||
EnvironmentId = EnvironmentId,
|
||||
Environment = _environmentInterface.GetEnvironmentById(EnvironmentId)
|
||||
};
|
||||
|
||||
if (!_domainInterface.UnlinkDomainByIdFromEnvironment(environmentDomain))
|
||||
{
|
||||
ModelState.AddModelError("", "Something went wrong while deleting");
|
||||
return StatusCode(500, ModelState);
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpGet("{Id}/VirtualMachines")]
|
||||
[ProducesResponseType(200, Type = typeof(DomainModel))]
|
||||
[ProducesResponseType(400)]
|
||||
public IActionResult GetVirtualMachinesByDomainId(Guid Id)
|
||||
{
|
||||
|
||||
if (!_domainInterface.CheckDomainById(Id))
|
||||
return NotFound();
|
||||
|
||||
var domain = _mapper.Map<GetDomainVirtualMachineDetailsDto>(_domainInterface.GetVirtualMachinesByDomainId(Id));
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(ModelState);
|
||||
|
||||
return Ok(domain);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user