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 _logger; private readonly SiteDbContext _siteDbContext; public EnvironmentsApiController(ApplicationUserManager userManager, ILogger logger, SiteDbContext siteDbContext) { _logger = logger; _userManager = userManager; _siteDbContext = siteDbContext; } [HttpGet("/api/environments")] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public async Task 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 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(); } /// /// Get Country by Id /// /// ISO 3166-1 countryId numeric code /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [HttpGet("/api/environments/{environmentId}", Name = nameof(GetEnvironmentById))] public async Task GetEnvironmentById(string siteId) { var environment = await _siteDbContext.Environments.Where(x => x.Id == siteId).FirstOrDefaultAsync(); if (environment != null) { return Ok(environment); } return new NotFoundResult(); } } }