-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V14: Blueprint CRUD endpoints (#15947)
* Get blueprint by key * Renaming * Implementing DeleteBlueprintAsync * Fixing tests to use the new method * Making ContentControllerBase abstract * Cleanup * Implementing Delete blueprint endpoint * Revert obsoletion in ContentService.cs * More reverting * Remove usings * Introducing IContentBlueprintEditingService * Refactor Get and Delete blueprint endpoints to use the new IContentBlueprintEditingService * Fix base inheritance case in SchemaIdSelector * Creating RequestModelBase for UpdateDocument to be reused in both document and blueprint models * Creating DocumentResponseModelBase to be reused in both document and blueprint models * Renamed blueprint response model for item endpoint to be aligned with the rest of the item models * More renaming changes of the DocumentBlueprintItemResponseModel * Refactor ByKeyDocumentBlueprintController to make use of the new blueprint models * New blueprint models and mapping * Adding UpdateAsync to ContentBlueprintEditingService * Adding IDocumentBlueprintEditingPresentationFactory.cs * Adding UpdateDocumentBlueprintController.cs * Adding methods required from the base * Fixing bug in document type mapping - mapping incorrect key * Cleanup * Fix item endpoint * Adding MapCreateModel * Adding create model * Creating request model base + related classes * Another request model * Blueprint editing service * Adding create controllers * Adding DuplicateName operation status for blueprints and handling it * Updating OpenApi.json * Fix comment * Fix mapping * Adding comments * Passing in id for create blueprint from document model * Mapping default state to Draft - no need to calculate it, it will always be that for blueprints * Cleanup * Update OpenApi.json * Review comments * Fix policies * More policy updates
- Loading branch information
Showing
37 changed files
with
1,305 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...raco.Cms.Api.Management/Controllers/DocumentBlueprint/ByKeyDocumentBlueprintController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Umbraco.Cms.Api.Management.ViewModels.DocumentBlueprint; | ||
using Umbraco.Cms.Core.Mapping; | ||
using Umbraco.Cms.Core.Models; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Cms.Web.Common.Authorization; | ||
|
||
namespace Umbraco.Cms.Api.Management.Controllers.DocumentBlueprint; | ||
|
||
[ApiVersion("1.0")] | ||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocumentsOrDocumentTypes)] | ||
public class ByKeyDocumentBlueprintController : DocumentBlueprintControllerBase | ||
{ | ||
private readonly IContentBlueprintEditingService _contentBlueprintEditingService; | ||
private readonly IUmbracoMapper _umbracoMapper; | ||
|
||
public ByKeyDocumentBlueprintController(IContentBlueprintEditingService contentBlueprintEditingService, IUmbracoMapper umbracoMapper) | ||
{ | ||
_contentBlueprintEditingService = contentBlueprintEditingService; | ||
_umbracoMapper = umbracoMapper; | ||
} | ||
|
||
[HttpGet("{id:guid}")] | ||
[MapToApiVersion("1.0")] | ||
[ProducesResponseType(typeof(DocumentBlueprintResponseModel), StatusCodes.Status200OK)] | ||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> ByKey(Guid id) | ||
{ | ||
IContent? blueprint = await _contentBlueprintEditingService.GetAsync(id); | ||
if (blueprint == null) | ||
{ | ||
return DocumentBlueprintNotFound(); | ||
} | ||
|
||
return Ok(_umbracoMapper.Map<DocumentBlueprintResponseModel>(blueprint)); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...aco.Cms.Api.Management/Controllers/DocumentBlueprint/CreateDocumentBlueprintController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Umbraco.Cms.Api.Management.Factories; | ||
using Umbraco.Cms.Api.Management.ViewModels.DocumentBlueprint; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Models.ContentEditing; | ||
using Umbraco.Cms.Core.Security; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Cms.Core.Services.OperationStatus; | ||
using Umbraco.Cms.Web.Common.Authorization; | ||
|
||
namespace Umbraco.Cms.Api.Management.Controllers.DocumentBlueprint; | ||
|
||
[ApiVersion("1.0")] | ||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocumentTypes)] | ||
public class CreateDocumentBlueprintController : DocumentBlueprintControllerBase | ||
{ | ||
private readonly IDocumentBlueprintEditingPresentationFactory _blueprintEditingPresentationFactory; | ||
private readonly IContentBlueprintEditingService _contentBlueprintEditingService; | ||
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; | ||
|
||
public CreateDocumentBlueprintController( | ||
IDocumentBlueprintEditingPresentationFactory blueprintEditingPresentationFactory, | ||
IContentBlueprintEditingService contentBlueprintEditingService, | ||
IBackOfficeSecurityAccessor backOfficeSecurityAccessor) | ||
{ | ||
_blueprintEditingPresentationFactory = blueprintEditingPresentationFactory; | ||
_contentBlueprintEditingService = contentBlueprintEditingService; | ||
_backOfficeSecurityAccessor = backOfficeSecurityAccessor; | ||
} | ||
|
||
[HttpPost] | ||
[MapToApiVersion("1.0")] | ||
[ProducesResponseType(StatusCodes.Status201Created)] | ||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> Create(CreateDocumentBlueprintRequestModel requestModel) | ||
{ | ||
ContentBlueprintCreateModel model = _blueprintEditingPresentationFactory.MapCreateModel(requestModel); | ||
|
||
// We don't need to validate user access because we "only" require access to the Settings section to create new blueprints from scratch | ||
Attempt<ContentCreateResult, ContentEditingOperationStatus> result = await _contentBlueprintEditingService.CreateAsync(model, CurrentUserKey(_backOfficeSecurityAccessor)); | ||
|
||
return result.Success | ||
? CreatedAtId<ByKeyDocumentBlueprintController>(controller => nameof(controller.ByKey), result.Result.Content!.Key) | ||
: ContentEditingOperationStatusResult(result.Status); | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
...Management/Controllers/DocumentBlueprint/CreateDocumentBlueprintFromDocumentController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Umbraco.Cms.Api.Management.ViewModels.DocumentBlueprint; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Actions; | ||
using Umbraco.Cms.Core.Models.ContentEditing; | ||
using Umbraco.Cms.Core.Security; | ||
using Umbraco.Cms.Core.Security.Authorization; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Cms.Core.Services.OperationStatus; | ||
using Umbraco.Cms.Web.Common.Authorization; | ||
using Umbraco.Extensions; | ||
|
||
namespace Umbraco.Cms.Api.Management.Controllers.DocumentBlueprint; | ||
|
||
[ApiVersion("1.0")] | ||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocuments)] | ||
public class CreateDocumentBlueprintFromDocumentController : DocumentBlueprintControllerBase | ||
{ | ||
private readonly IAuthorizationService _authorizationService; | ||
private readonly IContentBlueprintEditingService _contentBlueprintEditingService; | ||
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; | ||
|
||
public CreateDocumentBlueprintFromDocumentController( | ||
IAuthorizationService authorizationService, | ||
IContentBlueprintEditingService contentBlueprintEditingService, | ||
IBackOfficeSecurityAccessor backOfficeSecurityAccessor) | ||
{ | ||
_authorizationService = authorizationService; | ||
_contentBlueprintEditingService = contentBlueprintEditingService; | ||
_backOfficeSecurityAccessor = backOfficeSecurityAccessor; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a blueprint from a content item. | ||
/// </summary> | ||
[HttpPost("from-document")] | ||
[MapToApiVersion("1.0")] | ||
[ProducesResponseType(StatusCodes.Status201Created)] | ||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> CreateFromDocument(CreateDocumentBlueprintFromDocumentRequestModel fromDocumentRequestModel) | ||
{ | ||
AuthorizationResult authorizationResult = await _authorizationService.AuthorizeResourceAsync( | ||
User, | ||
ContentPermissionResource.WithKeys(ActionCreateBlueprintFromContent.ActionLetter, fromDocumentRequestModel.Document.Id), | ||
AuthorizationPolicies.ContentPermissionByResource); | ||
|
||
if (!authorizationResult.Succeeded) | ||
{ | ||
return Forbidden(); | ||
} | ||
|
||
Attempt<ContentCreateResult, ContentEditingOperationStatus> result = | ||
await _contentBlueprintEditingService.CreateFromContentAsync( | ||
fromDocumentRequestModel.Document.Id, | ||
fromDocumentRequestModel.Name, | ||
fromDocumentRequestModel.Id, | ||
CurrentUserKey(_backOfficeSecurityAccessor)); | ||
|
||
return result.Success | ||
? CreatedAtId<ByKeyDocumentBlueprintController>(controller => nameof(controller.ByKey), result.Result.Content!.Key) | ||
: ContentEditingOperationStatusResult(result.Status); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...aco.Cms.Api.Management/Controllers/DocumentBlueprint/DeleteDocumentBlueprintController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Models; | ||
using Umbraco.Cms.Core.Security; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Cms.Core.Services.OperationStatus; | ||
using Umbraco.Cms.Web.Common.Authorization; | ||
|
||
namespace Umbraco.Cms.Api.Management.Controllers.DocumentBlueprint; | ||
|
||
[ApiVersion("1.0")] | ||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocumentTypes)] | ||
public class DeleteDocumentBlueprintController : DocumentBlueprintControllerBase | ||
{ | ||
private readonly IContentBlueprintEditingService _contentBlueprintEditingService; | ||
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; | ||
|
||
public DeleteDocumentBlueprintController(IContentBlueprintEditingService contentBlueprintEditingService, IBackOfficeSecurityAccessor backOfficeSecurityAccessor) | ||
{ | ||
_contentBlueprintEditingService = contentBlueprintEditingService; | ||
_backOfficeSecurityAccessor = backOfficeSecurityAccessor; | ||
} | ||
|
||
[HttpDelete("{id:guid}")] | ||
[MapToApiVersion("1.0")] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
[ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound)] | ||
public async Task<IActionResult> Delete(Guid id) | ||
{ | ||
Attempt<IContent?, ContentEditingOperationStatus> result = await _contentBlueprintEditingService.DeleteAsync(id, CurrentUserKey(_backOfficeSecurityAccessor)); | ||
|
||
return result.Success | ||
? Ok() | ||
: ContentEditingOperationStatusResult(result.Status); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...braco.Cms.Api.Management/Controllers/DocumentBlueprint/DocumentBlueprintControllerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.AspNetCore.Mvc; | ||
using Umbraco.Cms.Api.Management.Controllers.Content; | ||
using Umbraco.Cms.Api.Management.Routing; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
||
namespace Umbraco.Cms.Api.Management.Controllers.DocumentBlueprint; | ||
|
||
[VersionedApiBackOfficeRoute(Constants.UdiEntityType.DocumentBlueprint)] | ||
[ApiExplorerSettings(GroupName = "Document Blueprint")] | ||
public abstract class DocumentBlueprintControllerBase : ContentControllerBase | ||
{ | ||
protected IActionResult DocumentBlueprintNotFound() | ||
=> OperationStatusResult(ContentEditingOperationStatus.NotFound, problemDetailsBuilder | ||
=> NotFound(problemDetailsBuilder | ||
.WithTitle("The document blueprint could not be found") | ||
.Build())); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
....Api.Management/Controllers/DocumentBlueprint/Tree/RootDocumentBlueprintTreeController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...aco.Cms.Api.Management/Controllers/DocumentBlueprint/UpdateDocumentBlueprintController.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Asp.Versioning; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Umbraco.Cms.Api.Management.Factories; | ||
using Umbraco.Cms.Api.Management.ViewModels.Document; | ||
using Umbraco.Cms.Core; | ||
using Umbraco.Cms.Core.Models.ContentEditing; | ||
using Umbraco.Cms.Core.Security; | ||
using Umbraco.Cms.Core.Services; | ||
using Umbraco.Cms.Core.Services.OperationStatus; | ||
using Umbraco.Cms.Web.Common.Authorization; | ||
|
||
namespace Umbraco.Cms.Api.Management.Controllers.DocumentBlueprint; | ||
|
||
[ApiVersion("1.0")] | ||
[Authorize(Policy = AuthorizationPolicies.TreeAccessDocumentTypes)] | ||
public class UpdateDocumentBlueprintController : DocumentBlueprintControllerBase | ||
{ | ||
private readonly IDocumentBlueprintEditingPresentationFactory _blueprintEditingPresentationFactory; | ||
private readonly IContentBlueprintEditingService _contentBlueprintEditingService; | ||
private readonly IBackOfficeSecurityAccessor _backOfficeSecurityAccessor; | ||
|
||
public UpdateDocumentBlueprintController( | ||
IDocumentBlueprintEditingPresentationFactory blueprintEditingPresentationFactory, | ||
IContentBlueprintEditingService contentBlueprintEditingService, | ||
IBackOfficeSecurityAccessor backOfficeSecurityAccessor) | ||
{ | ||
_blueprintEditingPresentationFactory = blueprintEditingPresentationFactory; | ||
_contentBlueprintEditingService = contentBlueprintEditingService; | ||
_backOfficeSecurityAccessor = backOfficeSecurityAccessor; | ||
} | ||
|
||
[HttpPut("{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, UpdateDocumentBlueprintRequestModel requestModel) | ||
{ | ||
ContentBlueprintUpdateModel model = _blueprintEditingPresentationFactory.MapUpdateModel(requestModel); | ||
|
||
// We don't need to validate user access because we "only" require access to the Settings section to update blueprints | ||
Attempt<ContentUpdateResult, ContentEditingOperationStatus> result = await _contentBlueprintEditingService.UpdateAsync(id, model, CurrentUserKey(_backOfficeSecurityAccessor)); | ||
|
||
return result.Success | ||
? Ok() | ||
: ContentEditingOperationStatusResult(result.Status); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Umbraco.Cms.Api.Management/Factories/DocumentBlueprintEditingPresentationFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
using Umbraco.Cms.Api.Management.ViewModels.Document; | ||
using Umbraco.Cms.Api.Management.ViewModels.DocumentBlueprint; | ||
using Umbraco.Cms.Core.Models.ContentEditing; | ||
|
||
namespace Umbraco.Cms.Api.Management.Factories; | ||
|
||
internal sealed class DocumentBlueprintEditingPresentationFactory : ContentEditingPresentationFactory<DocumentValueModel, DocumentVariantRequestModel>, IDocumentBlueprintEditingPresentationFactory | ||
{ | ||
public ContentBlueprintCreateModel MapCreateModel(CreateDocumentBlueprintRequestModel requestModel) | ||
{ | ||
ContentBlueprintCreateModel model = MapContentEditingModel<ContentBlueprintCreateModel>(requestModel); | ||
model.Key = requestModel.Id; | ||
model.ContentTypeKey = requestModel.DocumentType.Id; | ||
model.ParentKey = requestModel.Parent?.Id; | ||
|
||
return model; | ||
} | ||
|
||
public ContentBlueprintUpdateModel MapUpdateModel(UpdateDocumentBlueprintRequestModel requestModel) | ||
{ | ||
ContentBlueprintUpdateModel model = MapContentEditingModel<ContentBlueprintUpdateModel>(requestModel); | ||
return model; | ||
} | ||
} |
Oops, something went wrong.