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

82 lines
2.7 KiB
C#
Raw Normal View History

2022-12-02 05:31:11 +03:00
using CodeLiturgy.Data.Application;
2022-11-26 01:35:47 +03:00
using CodeLiturgy.Data.Auth.Context.Users;
using CodeLiturgy.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2022-11-26 01:35:47 +03:00
namespace CodeLiturgy.Views.Controllers
{
[ApiController]
[Authorize]
public class EnvironmentsApiController : ControllerBase
2022-11-26 01:35:47 +03:00
{
private ApplicationUserManager _userManager;
private ILogger<SitesApiController> _logger;
2022-11-26 01:35:47 +03:00
private readonly SiteDbContext _siteDbContext;
public EnvironmentsApiController(ApplicationUserManager userManager, ILogger<SitesApiController> logger, SiteDbContext siteDbContext)
2022-11-26 01:35:47 +03:00
{
_logger = logger;
_userManager = userManager;
_siteDbContext = siteDbContext;
}
[HttpGet("/api/environments")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult> GetEnvironments(
2022-11-26 01:35:47 +03:00
int skip = 0, int take = 50, int orderDir = 1)
{
var sites = _siteDbContext.Environments.Skip(skip).Take(take).ToListAsync();
2022-11-26 01:35:47 +03:00
return Ok(sites);
}
2022-12-02 05:31:11 +03:00
[HttpPost("/api/environments")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
2022-11-26 01:35:47 +03:00
public async Task<ActionResult> AddEnvironment(SiteEnvironmentCreate siteEnvironmentCreate)
2022-12-02 05:31:11 +03:00
{
var newEnvironment= new SiteEnvironment();
newEnvironment.Name = siteEnvironmentCreate.Name;
_siteDbContext.Environments.Add(newEnvironment);
2022-12-02 05:31:11 +03:00
var success = await _siteDbContext.SaveChangesAsync() >= 0;
2022-12-02 05:31:11 +03:00
if (success)
{
return Ok(newEnvironment);
2022-12-02 05:31:11 +03:00
}
return BadRequest();
}
2022-11-26 01:35:47 +03:00
/// <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)
2022-11-26 01:35:47 +03:00
{
var environment = await _siteDbContext.Environments.Where(x => x.Id == siteId).FirstOrDefaultAsync();
2022-11-26 01:35:47 +03:00
if (environment != null)
2022-11-26 01:35:47 +03:00
{
return Ok(environment);
}
return new NotFoundResult();
}
}
}