CodeLiturgy.Dashboard/CodeLiturgy.Views/Controllers/EnvironmentsApiController.cs

82 lines
2.7 KiB
C#

using CodeLiturgy.Data.Application;
using CodeLiturgy.Data.Auth.Context.Users;
using CodeLiturgy.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace CodeLiturgy.Views.Controllers
{
[ApiController]
[Authorize]
public class EnvironmentsApiController : ControllerBase
{
private ApplicationUserManager _userManager;
private ILogger<SitesApiController> _logger;
private readonly SiteDbContext _siteDbContext;
public EnvironmentsApiController(ApplicationUserManager userManager, ILogger<SitesApiController> logger, SiteDbContext siteDbContext)
{
_logger = logger;
_userManager = userManager;
_siteDbContext = siteDbContext;
}
[HttpGet("/api/environments")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> GetEnvironments(
int skip = 0, int take = 50, int orderDir = 1)
{
var sites = _siteDbContext.Environments.Skip(skip).Take(take).ToListAsync();
return Ok(sites);
}
[HttpPost("/api/environments")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task<ActionResult> AddEnvironment(SiteEnvironmentCreate siteEnvironmentCreate)
{
var newEnvironment= new SiteEnvironment();
newEnvironment.Name = siteEnvironmentCreate.Name;
_siteDbContext.Environments.Add(newEnvironment);
var success = await _siteDbContext.SaveChangesAsync() >= 0;
if (success)
{
return Ok(newEnvironment);
}
return BadRequest();
}
/// <summary>
/// Get Country by Id
/// </summary>
/// <param name="countryId">ISO 3166-1 countryId numeric code</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
[HttpGet("/api/environments/{environmentId}", Name = nameof(GetEnvironmentById))]
public async Task<ActionResult> GetEnvironmentById(string siteId)
{
var environment = await _siteDbContext.Environments.Where(x => x.Id == siteId).FirstOrDefaultAsync();
if (environment != null)
{
return Ok(environment);
}
return new NotFoundResult();
}
}
}