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;
|
2022-11-17 01:17:37 +03:00
|
|
|
using CodeLiturgy.Domain;
|
2022-12-09 03:27:00 +03:00
|
|
|
using CodeLiturgy.Views.Augeas;
|
2022-11-17 01:17:37 +03:00
|
|
|
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>
|
2022-11-17 01:17:37 +03:00
|
|
|
[Route("api/sites")]
|
|
|
|
[ApiController]
|
|
|
|
[Authorize]
|
|
|
|
|
|
|
|
public class SitesController : ControllerBase
|
|
|
|
{
|
|
|
|
private ApplicationUserManager _userManager;
|
2022-11-22 18:05:35 +03:00
|
|
|
private ILogger<SitesController> _logger;
|
2022-11-17 01:17:37 +03:00
|
|
|
private readonly SiteDbContext _siteDbContext;
|
2022-12-09 03:27:00 +03:00
|
|
|
private readonly AugeasManager _augeasManager;
|
2022-11-17 01:17:37 +03:00
|
|
|
|
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)
|
2022-11-17 01:17:37 +03:00
|
|
|
{
|
|
|
|
_logger = logger;
|
|
|
|
_userManager = userManager;
|
|
|
|
_siteDbContext = siteDbContext;
|
2022-12-09 03:27:00 +03:00
|
|
|
_augeasManager = augeasManager;
|
2022-11-17 01:17:37 +03:00
|
|
|
}
|
|
|
|
|
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>
|
2022-11-17 01:17:37 +03:00
|
|
|
[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
|
|
|
|
2022-11-17 01:17:37 +03:00
|
|
|
/// <summary>
|
2022-12-09 03:27:00 +03:00
|
|
|
/// Gets a site by Id.
|
2022-11-17 01:17:37 +03:00
|
|
|
/// </summary>
|
2022-12-09 03:27:00 +03:00
|
|
|
/// <param name="siteId">Site id.</param>
|
2022-11-17 01:17:37 +03:00
|
|
|
/// <returns></returns>
|
|
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
2022-11-26 01:35:47 +03:00
|
|
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
|
|
|
|
2022-11-17 01:17:37 +03:00
|
|
|
[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>
|
2022-11-17 01:17:37 +03:00
|
|
|
[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
|
|
|
}
|
|
|
|
|
2022-11-17 01:17:37 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|