V14: Update template controllers (#14326)
* Add alias to document item response * Add master template key to detailed model * Add mater template key as optiona parameter to Scaffolding * Check for duplicate alias when creating templates directly * Clean * Ensure integration tests creates templates with unique aliases * Perform mapping in presentation factory
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
using Asp.Versioning;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Api.Management.Factories;
|
||||
using Umbraco.Cms.Api.Management.ViewModels.Template;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
|
||||
@@ -12,12 +12,14 @@ namespace Umbraco.Cms.Api.Management.Controllers.Template;
|
||||
public class ByKeyTemplateController : TemplateControllerBase
|
||||
{
|
||||
private readonly ITemplateService _templateService;
|
||||
private readonly IUmbracoMapper _umbracoMapper;
|
||||
private readonly ITemplatePresentationFactory _templatePresentationFactory;
|
||||
|
||||
public ByKeyTemplateController(ITemplateService templateService, IUmbracoMapper umbracoMapper)
|
||||
public ByKeyTemplateController(
|
||||
ITemplateService templateService,
|
||||
ITemplatePresentationFactory templatePresentationFactory)
|
||||
{
|
||||
_templateService = templateService;
|
||||
_umbracoMapper = umbracoMapper;
|
||||
_templatePresentationFactory = templatePresentationFactory;
|
||||
}
|
||||
|
||||
[HttpGet("{id:guid}")]
|
||||
@@ -29,6 +31,6 @@ public class ByKeyTemplateController : TemplateControllerBase
|
||||
ITemplate? template = await _templateService.GetAsync(id);
|
||||
return template == null
|
||||
? NotFound()
|
||||
: Ok(_umbracoMapper.Map<TemplateResponseModel>(template));
|
||||
: Ok(await _templatePresentationFactory.CreateTemplateResponseModelAsync(template));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,13 +12,13 @@ namespace Umbraco.Cms.Api.Management.Controllers.Template.Item;
|
||||
[ApiVersion("1.0")]
|
||||
public class ItemTemplateItemController : TemplateItemControllerBase
|
||||
{
|
||||
private readonly IEntityService _entityService;
|
||||
private readonly IUmbracoMapper _mapper;
|
||||
private readonly ITemplateService _templateService;
|
||||
|
||||
public ItemTemplateItemController(IEntityService entityService, IUmbracoMapper mapper)
|
||||
public ItemTemplateItemController(IUmbracoMapper mapper, ITemplateService templateService)
|
||||
{
|
||||
_entityService = entityService;
|
||||
_mapper = mapper;
|
||||
_templateService = templateService;
|
||||
}
|
||||
|
||||
[HttpGet("item")]
|
||||
@@ -26,8 +26,10 @@ public class ItemTemplateItemController : TemplateItemControllerBase
|
||||
[ProducesResponseType(typeof(IEnumerable<TemplateItemResponseModel>), StatusCodes.Status200OK)]
|
||||
public async Task<IActionResult> Item([FromQuery(Name = "id")] SortedSet<Guid> ids)
|
||||
{
|
||||
IEnumerable<IEntitySlim> templates = _entityService.GetAll(UmbracoObjectTypes.Template, ids.ToArray());
|
||||
List<TemplateItemResponseModel> responseModels = _mapper.MapEnumerable<IEntitySlim, TemplateItemResponseModel>(templates);
|
||||
// This is far from ideal, that we pick out the entire model, however, we must do this to get the alias.
|
||||
// This is (for one) needed for when specifying master template, since alias + .cshtml
|
||||
IEnumerable<ITemplate> templates = await _templateService.GetAllAsync(ids.ToArray());
|
||||
List<TemplateItemResponseModel> responseModels = _mapper.MapEnumerable<ITemplate, TemplateItemResponseModel>(templates);
|
||||
return Ok(responseModels);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,26 +3,27 @@ using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Api.Management.ViewModels.Template;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
|
||||
namespace Umbraco.Cms.Api.Management.Controllers.Template;
|
||||
|
||||
[ApiVersion("1.0")]
|
||||
public class ScaffoldTemplateController : TemplateControllerBase
|
||||
{
|
||||
private readonly IDefaultViewContentProvider _defaultViewContentProvider;
|
||||
private readonly ITemplateService _templateService;
|
||||
|
||||
public ScaffoldTemplateController(IDefaultViewContentProvider defaultViewContentProvider)
|
||||
=> _defaultViewContentProvider = defaultViewContentProvider;
|
||||
public ScaffoldTemplateController(ITemplateService templateService) => _templateService = templateService;
|
||||
|
||||
[HttpGet("scaffold")]
|
||||
[MapToApiVersion("1.0")]
|
||||
[ProducesResponseType(typeof(TemplateScaffoldResponseModel), StatusCodes.Status200OK)]
|
||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||
public async Task<ActionResult<TemplateScaffoldResponseModel>> Scaffold()
|
||||
public async Task<ActionResult<TemplateScaffoldResponseModel>> Scaffold([FromQuery(Name = "masterTemplateId")] Guid? masterTemplateId)
|
||||
{
|
||||
var scaffoldViewModel = new TemplateScaffoldResponseModel
|
||||
{
|
||||
Content = _defaultViewContentProvider.GetDefaultFileContent()
|
||||
Content = await _templateService.GetScaffoldAsync(masterTemplateId),
|
||||
};
|
||||
|
||||
return await Task.FromResult(Ok(scaffoldViewModel));
|
||||
|
||||
@@ -25,6 +25,10 @@ public class TemplateControllerBase : ManagementApiControllerBase
|
||||
.WithTitle("Cancelled by notification")
|
||||
.WithDetail("A notification handler prevented the template operation.")
|
||||
.Build()),
|
||||
TemplateOperationStatus.DuplicateAlias => BadRequest(new ProblemDetailsBuilder()
|
||||
.WithTitle("Duplicate alias")
|
||||
.WithDetail("A template with that alias already exists.")
|
||||
.Build()),
|
||||
_ => StatusCode(StatusCodes.Status500InternalServerError, "Unknown template operation status")
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user