Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
Comment thread
Migaroez marked this conversation as resolved.
Outdated

var viewModel = new PagedViewModel<SegmentResponseModel>
{
Items = _umbracoMapper.MapEnumerable<Core.Models.Segment, SegmentResponseModel>(pagedAttempt.Result!.Items),
Total = pagedAttempt.Result!.Total,
};

return Ok(viewModel);
}
}
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()),
});
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public static IUmbracoBuilder AddUmbracoManagementApi(this IUmbracoBuilder build
.AddPreview()
.AddPasswordConfiguration()
.AddSupplemenataryLocalizedTextFileSources()
.AddUserData();
.AddUserData()
.AddSegment();

services
.ConfigureOptions<ConfigureApiBehaviorOptions>()
Expand Down
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;
}
}
Loading