-
Notifications
You must be signed in to change notification settings - Fork 2.9k
V14/feature/all segment endpoint #16054
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
src/Umbraco.Cms.Api.Management/Controllers/Segment/AllSegmentController.cs
This file contains hidden or 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.Common.ViewModels.Pagination; | ||
| using Umbraco.Cms.Api.Management.ViewModels.Segment; | ||
| using Umbraco.Cms.Core; | ||
| using Umbraco.Cms.Core.Mapping; | ||
| using Umbraco.Cms.Core.Models; | ||
| using Umbraco.Cms.Core.Services; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
| using Umbraco.Cms.Web.Common.Authorization; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.Segment; | ||
|
|
||
| [ApiVersion("1.0")] | ||
| [Authorize(Policy = AuthorizationPolicies.SectionAccessSettings)] | ||
| public class AllSegmentController : SegmentControllerBase | ||
| { | ||
| private readonly ISegmentService _segmentService; | ||
| private readonly IUmbracoMapper _umbracoMapper; | ||
|
|
||
| public AllSegmentController(ISegmentService segmentService, IUmbracoMapper umbracoMapper) | ||
| { | ||
| _segmentService = segmentService; | ||
| _umbracoMapper = umbracoMapper; | ||
| } | ||
|
|
||
| [HttpGet] | ||
| [MapToApiVersion("1.0")] | ||
| [ProducesResponseType(typeof(PagedViewModel<SegmentResponseModel>), StatusCodes.Status200OK)] | ||
| [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status400BadRequest)] | ||
| public async Task<IActionResult> GetAll(CancellationToken cancellationToken, int skip = 0, int take = 100) | ||
| { | ||
| Attempt<PagedModel<Core.Models.Segment>?, SegmentOperationStatus> pagedAttempt = await _segmentService.GetPagedSegmentsAsync(skip, take); | ||
|
|
||
| if (pagedAttempt.Success == false) | ||
| { | ||
| return MapFailure(pagedAttempt.Status); | ||
| } | ||
|
|
||
| var viewModel = new PagedViewModel<SegmentResponseModel> | ||
| { | ||
| Items = _umbracoMapper.MapEnumerable<Core.Models.Segment, SegmentResponseModel>(pagedAttempt.Result!.Items), | ||
| Total = pagedAttempt.Result!.Total, | ||
| }; | ||
|
|
||
| return Ok(viewModel); | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
src/Umbraco.Cms.Api.Management/Controllers/Segment/SegmentControllerBase.cs
This file contains hidden or 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,19 @@ | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Umbraco.Cms.Api.Management.Routing; | ||
| using Umbraco.Cms.Core.Services.OperationStatus; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Controllers.Segment; | ||
|
|
||
| [VersionedApiBackOfficeRoute("segment")] | ||
| [ApiExplorerSettings(GroupName = "Segment")] | ||
| public abstract class SegmentControllerBase : ManagementApiControllerBase | ||
| { | ||
| protected IActionResult MapFailure(SegmentOperationStatus status) | ||
| => OperationStatusResult(status, problemDetailsBuilder => status switch | ||
| { | ||
| _ => StatusCode(StatusCodes.Status500InternalServerError, problemDetailsBuilder | ||
| .WithTitle("Unknown segment operation status.") | ||
| .Build()), | ||
| }); | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/Umbraco.Cms.Api.Management/DependencyInjection/SegmentBuilderExtensions.cs
This file contains hidden or 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,15 @@ | ||
| using Umbraco.Cms.Api.Management.Mapping.Segment; | ||
| using Umbraco.Cms.Core.DependencyInjection; | ||
| using Umbraco.Cms.Core.Mapping; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.DependencyInjection; | ||
|
|
||
| internal static class SegmentBuilderExtensions | ||
| { | ||
| internal static IUmbracoBuilder AddSegment(this IUmbracoBuilder builder) | ||
| { | ||
| builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>().Add<SegmentMapDefinition>(); | ||
|
|
||
| return builder; | ||
| } | ||
| } |
This file contains hidden or 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
19 changes: 19 additions & 0 deletions
19
src/Umbraco.Cms.Api.Management/Mapping/Segment/SegmentMapDefinition.cs
This file contains hidden or 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,19 @@ | ||
| using Umbraco.Cms.Api.Management.ViewModels.Segment; | ||
| using Umbraco.Cms.Core.Mapping; | ||
|
|
||
| namespace Umbraco.Cms.Api.Management.Mapping.Segment; | ||
|
|
||
| public class SegmentMapDefinition : IMapDefinition | ||
| { | ||
| public void DefineMaps(IUmbracoMapper mapper) | ||
| { | ||
| mapper.Define<Core.Models.Segment, SegmentResponseModel>((_, _) => new SegmentResponseModel(), Map); | ||
| } | ||
|
|
||
| // Umbraco.Code.MapAll | ||
| private static void Map(Core.Models.Segment source, SegmentResponseModel target, MapperContext context) | ||
| { | ||
| target.Name = source.Name; | ||
| target.Alias = source.Alias; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.