using CodeLiturgy.Data.Auth.Context.Users; using CodeLiturgy.Data.Application; using CodeLiturgy.Domain; using CodeLiturgy.Views.Augeas; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; namespace CodeLiturgy.Views.Controllers { /// /// Controller responsible for managing websites. /// [Route("api/sites")] [ApiController] [Authorize] public class SitesController : ControllerBase { private ApplicationUserManager _userManager; private ILogger _logger; private readonly SiteDbContext _siteDbContext; private readonly AugeasManager _augeasManager; /// /// Sites controller constructor. /// /// /// /// /// public SitesController( ApplicationUserManager userManager, ILogger logger, SiteDbContext siteDbContext, AugeasManager augeasManager) { _logger = logger; _userManager = userManager; _siteDbContext = siteDbContext; _augeasManager = augeasManager; } /// /// Gets the sites stored in the database. /// /// /// /// /// [HttpGet] [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status401Unauthorized)] public ActionResult GetSites( int skip = 0, int take = 50, int orderDir = 1) { var (success, sites) = _siteDbContext.GetSites(skip, take, orderDir); if (!success) return new NotFoundResult(); return Ok(sites); } /// /// Gets a site by Id. /// /// Site id. /// [ProducesResponseType(StatusCodes.Status200OK)] [ProducesResponseType(StatusCodes.Status404NotFound)] [ProducesResponseType(StatusCodes.Status403Forbidden)] [HttpGet("{siteId}", Name = nameof(GetSiteById))] public ActionResult GetSiteById(string siteId) { var (success, site) = _siteDbContext.GetOneSiteById(siteId); if (success) { return Ok(site); } return new NotFoundResult(); } /// /// Adds a site to the database. /// /// /// [ProducesResponseType(StatusCodes.Status201Created)] [ProducesResponseType(StatusCodes.Status406NotAcceptable)] [HttpPost] public ActionResult AddSite(SiteCreate siteToCreate) { var (success, site) = _siteDbContext.AddSite(siteToCreate); if (!success) return new BadRequestResult(); return CreatedAtRoute(nameof(GetSiteById), new {countryId = site.Id}, site); } } }