skadegladam
12/05/2024, 8:24 AMKvist
12/05/2024, 10:01 PMProNotion
12/06/2024, 10:47 AMIApiContentResponseBuilder
(implementation inherits ApiContentBuilderBase<IApiContentResponse>
)
- IOutputExpansionStrategy
- IApiPropertyRenderer
Our controllers inherit from ContentApiControllerBase
. We have our own implementations of ByRouteContentApiController
and ByIdContentApiController
which inherit the core ones and extend them where required.
We are trying to utilise as much of the existing Delivery API as we can to avoid reinventing the wheel and causing ourselves headaches further down the line.Sean Thorne
12/06/2024, 11:11 AMcs
public class HomePageController : BasePageController
{
private readonly IHomePageService _homePageService;
public HomePageController(...removed for brevity) : base(...removed for brevity)
{
_homePageService = homePageService;
}
public async Task<ActionResult> HomePage()
{
if (CurrentPage is not HomePage page)
{
return StatusCode((int)HttpStatusCode.NotFound);
}
var responseModel = await TryGetApiResponseModelAsync(()
=> _homePageService.GetApiResponseModelAsync(page));
return StatusCode((int)responseModel.StatusCode, responseModel);
}
}
cs
public class BasePageController : RenderController
{
public BasePageController(...removed for brevity): base(...removed for brevity)
{
_logger = logger;
}
public async Task<ApiResponseViewModel> TryGetApiResponseModelAsync(Func<Task<ApiResponseViewModel>> operation)
{
var result = new ApiResponseViewModel();
try
{
result = await operation.Invoke();
}
catch (Exception? e)
{
result = HandleException(result, e);
}
return result;
}
// rest of base methods
Mikkel Johansen
12/06/2024, 2:02 PMusing Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Core.Services.Navigation;
namespace Custom.Controllers;
[ApiController]
[Route("/umbraco/api/mikkel")]
public class MikkelController(
IDocumentNavigationQueryService documentNavigationQueryService,
IPublishedContentCache publishedContentCache) : Controller
{
[HttpGet("getall")]
public ActionResult<string> GetAll()
{
if (!documentNavigationQueryService.TryGetRootKeys(out var rootKeys))
{
return this.Problem("Error");
}
if (documentNavigationQueryService.TryGetDescendantsKeysOfType(rootKeys.First(), "teamPage", out var descendantsKeys))
{
if (descendantsKeys.Any())
{
var currentNode = publishedContentCache.GetById(descendantsKeys.First());
}
}
var rootNode = publishedContentCache.GetById(rootKeys.First());
return Ok(rootNode.Name);
}
}
Balázs Kerper
12/13/2024, 3:57 PMContentApiControllerBase
) as we wanted to control some of the redirecting/404 resolvers aspects, and also the returned model structures. With keeping as much as possible from the original solution.
Back to the original question a bit, I don't think this is a bad approach though. Quite many things we wanted to do would belong on the controller level, and I don't think making them extensible with all the context they store would be better/more developer friendly in the end than to provide the possibility to roll with your own and then do as you wish there 🙂