120 lines
3.7 KiB
C#
120 lines
3.7 KiB
C#
|
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;
|
||
|
using Microsoft.EntityFrameworkCore;
|
||
|
|
||
|
|
||
|
namespace CodeLiturgy.Views.Controllers
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Controller responsible for managing websites.
|
||
|
/// </summary>
|
||
|
[Route("api/sites")]
|
||
|
[ApiController]
|
||
|
[Authorize]
|
||
|
|
||
|
public class SitesApiController : ControllerBase
|
||
|
{
|
||
|
private ApplicationUserManager _userManager;
|
||
|
private ILogger<SitesApiController> _logger;
|
||
|
private readonly SiteDbContext _siteDbContext;
|
||
|
private readonly AugeasManager _augeasManager;
|
||
|
|
||
|
/// <summary>
|
||
|
/// Sites controller constructor.
|
||
|
/// </summary>
|
||
|
/// <param name="userManager"></param>
|
||
|
/// <param name="logger"></param>
|
||
|
/// <param name="siteDbContext"></param>
|
||
|
/// <param name="augeasManager"></param>
|
||
|
public SitesApiController(
|
||
|
ApplicationUserManager userManager,
|
||
|
ILogger<SitesApiController> logger,
|
||
|
SiteDbContext siteDbContext,
|
||
|
AugeasManager augeasManager)
|
||
|
{
|
||
|
_logger = logger;
|
||
|
_userManager = userManager;
|
||
|
_siteDbContext = siteDbContext;
|
||
|
_augeasManager = augeasManager;
|
||
|
}
|
||
|
|
||
|
/// <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.Status401Unauthorized)]
|
||
|
|
||
|
public async Task<ActionResult> GetSites(
|
||
|
|
||
|
int skip = 0, int take = 50, int orderDir = 1)
|
||
|
{
|
||
|
var result = await _siteDbContext
|
||
|
.Sites.Skip(skip)
|
||
|
.Take(take)
|
||
|
.ToListAsync();
|
||
|
|
||
|
return Ok(result);
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets a site by Id.
|
||
|
/// </summary>
|
||
|
/// <param name="siteId">Site id.</param>
|
||
|
/// <returns></returns>
|
||
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
||
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||
|
[ProducesResponseType(StatusCodes.Status403Forbidden)]
|
||
|
|
||
|
[HttpGet("{siteId}", Name = nameof(GetSiteById))]
|
||
|
public async Task<ActionResult> GetSiteById(string siteId)
|
||
|
{
|
||
|
var site = await _siteDbContext.Sites.Where(x => x.Id == siteId).FirstOrDefaultAsync();
|
||
|
|
||
|
if (site != null)
|
||
|
{
|
||
|
return Ok(site);
|
||
|
}
|
||
|
|
||
|
return new NotFoundResult();
|
||
|
}
|
||
|
|
||
|
|
||
|
/// <summary>
|
||
|
/// Adds a site to the database.
|
||
|
/// </summary>
|
||
|
/// <param name="siteToCreate"></param>
|
||
|
/// <returns></returns>
|
||
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
||
|
[ProducesResponseType(StatusCodes.Status406NotAcceptable)]
|
||
|
[HttpPost]
|
||
|
public async Task<ActionResult> AddSite(SiteCreate siteToCreate)
|
||
|
{
|
||
|
var site = new Site()
|
||
|
{
|
||
|
Domain = siteToCreate.Domain,
|
||
|
CreatedDate = DateTime.Today,
|
||
|
SiteType = siteToCreate.SiteType
|
||
|
|
||
|
};
|
||
|
|
||
|
_siteDbContext.Sites.Add(site);
|
||
|
|
||
|
var success = await _siteDbContext.SaveChangesAsync() >= 0;
|
||
|
if (!success) return new BadRequestResult();
|
||
|
return CreatedAtRoute(nameof(GetSiteById), new {Id = site.Id}, site);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|