Files
Umbraco-CMS/src/Umbraco.Cms.Api.Management/Controllers/Template/UpdateTemplateController.cs
Kenn Jacobsen ac8cfcf634 Align template API with dictionary API (#13714)
* Align the template services and API with the dictionary ones (use attempt pattern)

* A little controller clean-up

* Mimic old file service behavior, make unit tests happy

* Align CreateForContentTypeAsync return value with the rest of the TemplateService API

* Scaffold endpoint should no longer feature master templates

* Update the OpenAPI JSON
2023-01-31 12:20:46 +01:00

51 lines
1.8 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.ViewModels.Template;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;
namespace Umbraco.Cms.Api.Management.Controllers.Template;
public class UpdateTemplateController : TemplateControllerBase
{
private readonly ITemplateService _templateService;
private readonly IUmbracoMapper _umbracoMapper;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
public UpdateTemplateController(
ITemplateService templateService,
IUmbracoMapper umbracoMapper,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor)
{
_templateService = templateService;
_umbracoMapper = umbracoMapper;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
}
[HttpPut("{key:guid}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> Update(Guid key, TemplateUpdateModel updateModel)
{
ITemplate? template = await _templateService.GetAsync(key);
if (template == null)
{
return NotFound();
}
template = _umbracoMapper.Map(updateModel, template);
Attempt<ITemplate, TemplateOperationStatus> result = await _templateService.UpdateAsync(template, CurrentUserId(_backOfficeSecurityAccessor));
return result.Success ?
Ok()
: TemplateOperationStatusResult(result.Status);
}
}