Files
Umbraco-CMS/src/Umbraco.Cms.Api.Management/Controllers/Dictionary/UpdateDictionaryController.cs
Bjarke Berg 45033437a2 Updated all 404 and 500 responses to use problem details model (#14634)
* Updated all 404 and 500 responses to use problem details

* Updated OpenApi.json

* Add missing ProducesResponseType

* Updated OpenApi.json
2023-08-04 10:51:20 +02:00

54 lines
2.2 KiB
C#

using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.Factories;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Api.Management.ViewModels.Dictionary;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Core.Services.OperationStatus;
namespace Umbraco.Cms.Api.Management.Controllers.Dictionary;
[ApiVersion("1.0")]
public class UpdateDictionaryController : DictionaryControllerBase
{
private readonly IDictionaryItemService _dictionaryItemService;
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor;
private readonly IDictionaryPresentationFactory _dictionaryPresentationFactory;
public UpdateDictionaryController(
IDictionaryItemService dictionaryItemService,
IBackOfficeSecurityAccessor backOfficeSecurityAccessor,
IDictionaryPresentationFactory dictionaryPresentationFactory)
{
_dictionaryItemService = dictionaryItemService;
_backOfficeSecurityAccessor = backOfficeSecurityAccessor;
_dictionaryPresentationFactory = dictionaryPresentationFactory;
}
[HttpPut($"{{{nameof(id)}:guid}}")]
[MapToApiVersion("1.0")]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)]
public async Task<IActionResult> Update(Guid id, UpdateDictionaryItemRequestModel updateDictionaryItemRequestModel)
{
IDictionaryItem? current = await _dictionaryItemService.GetAsync(id);
if (current == null)
{
return DictionaryNotFound();
}
IDictionaryItem updated = await _dictionaryPresentationFactory.MapUpdateModelToDictionaryItemAsync(current, updateDictionaryItemRequestModel);
Attempt<IDictionaryItem, DictionaryItemOperationStatus> result =
await _dictionaryItemService.UpdateAsync(updated, CurrentUserKey(_backOfficeSecurityAccessor));
return result.Success
? Ok()
: DictionaryItemOperationStatusResult(result.Status);
}
}