CodeLiturgy.Dashboard/CodeLiturgy.Views/Controllers/SitesController.cs

109 lines
3.4 KiB
C#
Raw Normal View History

2022-11-18 03:15:53 +03:00
using CodeLiturgy.Data.Auth.Context.Users;
2022-11-18 01:39:36 +03:00
using CodeLiturgy.Data.Application;
using CodeLiturgy.Domain;
2022-12-09 03:27:00 +03:00
using CodeLiturgy.Views.Augeas;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace CodeLiturgy.Views.Controllers
{
2022-12-09 03:27:00 +03:00
/// <summary>
/// Controller responsible for managing websites.
/// </summary>
[Route("api/sites")]
[ApiController]
[Authorize]
public class SitesController : ControllerBase
{
private ApplicationUserManager _userManager;
2022-11-22 18:05:35 +03:00
private ILogger<SitesController> _logger;
private readonly SiteDbContext _siteDbContext;
2022-12-09 03:27:00 +03:00
private readonly AugeasManager _augeasManager;
2022-12-09 03:27:00 +03:00
/// <summary>
/// Sites controller constructor.
/// </summary>
/// <param name="userManager"></param>
/// <param name="logger"></param>
/// <param name="siteDbContext"></param>
/// <param name="augeasManager"></param>
public SitesController(
ApplicationUserManager userManager,
ILogger<SitesController> logger,
SiteDbContext siteDbContext,
AugeasManager augeasManager)
{
_logger = logger;
_userManager = userManager;
_siteDbContext = siteDbContext;
2022-12-09 03:27:00 +03:00
_augeasManager = augeasManager;
}
2022-12-09 03:27:00 +03:00
/// <summary>
/// Gets the sites stored in the database.
/// </summary>
/// <param name="skip"></param>
/// <param name="take"></param>
/// <param name="orderDir"></param>
/// <returns></returns>
[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);
}
2022-12-09 03:27:00 +03:00
/// <summary>
2022-12-09 03:27:00 +03:00
/// Gets a site by Id.
/// </summary>
2022-12-09 03:27:00 +03:00
/// <param name="siteId">Site id.</param>
/// <returns></returns>
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
2022-11-26 01:35:47 +03:00
[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();
}
2022-12-09 03:27:00 +03:00
/// <summary>
/// Adds a site to the database.
/// </summary>
/// <param name="siteToCreate"></param>
/// <returns></returns>
[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);
2022-12-09 03:27:00 +03:00
}
}
}