New backoffice: Tag controller (#14072)
* Implement ByQueryEndpoint * Implement new async methods on tag service * dont have nullable query * Formatting * Add default implementation to interface * Allow nullable query in api --------- Co-authored-by: Zeegaan <nge@umbraco.dk> Co-authored-by: Nikolaj <nikolajlauridsen@protonmail.ch>
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Api.Common.ViewModels.Pagination;
|
||||
using Umbraco.Cms.Api.Management.ViewModels.Tag;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Services;
|
||||
|
||||
namespace Umbraco.Cms.Api.Management.Controllers.Tag;
|
||||
|
||||
public class ByQueryTagController : TagControllerBase
|
||||
{
|
||||
private readonly ITagService _tagService;
|
||||
private readonly IUmbracoMapper _mapper;
|
||||
|
||||
public ByQueryTagController(ITagService tagService, IUmbracoMapper mapper)
|
||||
{
|
||||
_tagService = tagService;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
[HttpGet]
|
||||
[MapToApiVersion("1.0")]
|
||||
[ProducesResponseType(typeof(PagedViewModel<TagResponseModel>), StatusCodes.Status200OK)]
|
||||
public async Task<ActionResult<PagedViewModel<TagResponseModel>>> ByQuery(string? query, string? tagGroup, string? culture, int skip = 0, int take = 100)
|
||||
{
|
||||
IEnumerable<ITag> result = await _tagService.GetByQueryAsync(query ?? string.Empty, tagGroup, culture);
|
||||
|
||||
List<TagResponseModel> responseModels = _mapper.MapEnumerable<ITag, TagResponseModel>(result);
|
||||
|
||||
var pagedViewModel = new PagedViewModel<TagResponseModel>
|
||||
{
|
||||
Items = responseModels.Skip(skip).Take(take),
|
||||
Total = responseModels.Count,
|
||||
};
|
||||
|
||||
return await Task.FromResult(Ok(pagedViewModel));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Api.Management.Routing;
|
||||
|
||||
namespace Umbraco.Cms.Api.Management.Controllers.Tag;
|
||||
|
||||
[ApiController]
|
||||
[VersionedApiBackOfficeRoute("tag")]
|
||||
[ApiExplorerSettings(GroupName = "Tag")]
|
||||
[ApiVersion("1.0")]
|
||||
public class TagControllerBase : ManagementApiControllerBase
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using Umbraco.Cms.Api.Management.Mapping.Tag;
|
||||
using Umbraco.Cms.Core.DependencyInjection;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
|
||||
namespace Umbraco.Cms.Api.Management.DependencyInjection;
|
||||
|
||||
public static class TagBuilderExtensions
|
||||
{
|
||||
internal static IUmbracoBuilder AddTags(this IUmbracoBuilder builder)
|
||||
{
|
||||
builder.WithCollectionBuilder<MapDefinitionCollectionBuilder>()
|
||||
.Add<TagResponseModelMapDefinition>();
|
||||
|
||||
return builder;
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,7 @@ public class ManagementApiComposer : IComposer
|
||||
.AddHealthChecks()
|
||||
.AddModelsBuilder()
|
||||
.AddRedirectUrl()
|
||||
.AddTags()
|
||||
.AddTrackedReferences()
|
||||
.AddTemporaryFiles()
|
||||
.AddDataTypes()
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
using Umbraco.Cms.Api.Management.ViewModels.Tag;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
|
||||
namespace Umbraco.Cms.Api.Management.Mapping.Tag;
|
||||
|
||||
public class TagResponseModelMapDefinition : IMapDefinition
|
||||
{
|
||||
public void DefineMaps(IUmbracoMapper mapper) =>
|
||||
mapper.Define<ITag, TagResponseModel>((_, _) => new TagResponseModel(), Map);
|
||||
|
||||
// Umbraco.Code.MapAll
|
||||
private void Map(ITag source, TagResponseModel target, MapperContext context)
|
||||
{
|
||||
target.Group = source.Group;
|
||||
target.Id = source.Key;
|
||||
target.NodeCount = source.NodeCount;
|
||||
target.Text = source.Text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace Umbraco.Cms.Api.Management.ViewModels.Tag;
|
||||
|
||||
public class TagResponseModel
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string? Text { get; set; }
|
||||
|
||||
public string? Group { get; set; }
|
||||
|
||||
public int NodeCount { get; set; }
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.Services;
|
||||
|
||||
@@ -58,6 +59,19 @@ public interface ITagService : IService
|
||||
/// </summary>
|
||||
IEnumerable<ITag> GetAllTags(string? group = null, string? culture = null);
|
||||
|
||||
Task<IEnumerable<ITag>> GetAllAsync(string? group = null, string? culture = null)
|
||||
{
|
||||
if (culture == string.Empty)
|
||||
{
|
||||
culture = null;
|
||||
}
|
||||
|
||||
return Task.FromResult(GetAllTags(group, culture));
|
||||
}
|
||||
|
||||
Task<IEnumerable<ITag>> GetByQueryAsync(string query, string? group = null, string? culture = null)
|
||||
=> Task.FromResult(GetAllAsync(group, culture).GetAwaiter().GetResult().Where(x => x.Text.InvariantContains(query)));
|
||||
|
||||
/// <summary>
|
||||
/// Gets all document tags.
|
||||
/// </summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using Umbraco.Cms.Core.Events;
|
||||
using Umbraco.Cms.Core.Models;
|
||||
using Umbraco.Cms.Core.Persistence.Repositories;
|
||||
using Umbraco.Cms.Core.Scoping;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Core.Services;
|
||||
|
||||
@@ -102,6 +103,18 @@ public class TagService : RepositoryService, ITagService
|
||||
}
|
||||
}
|
||||
|
||||
public Task<IEnumerable<ITag>> GetAllAsync(string? group = null, string? culture = null)
|
||||
{
|
||||
if (culture == string.Empty)
|
||||
{
|
||||
culture = null;
|
||||
}
|
||||
|
||||
return Task.FromResult(GetAllTags(group, culture));
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<ITag>> GetByQueryAsync(string query, string? group = null, string? culture = null) => (await GetAllAsync(group, culture)).Where(x => x.Text.InvariantContains(query));
|
||||
|
||||
/// <inheritdoc />
|
||||
public IEnumerable<ITag> GetAllContentTags(string? group = null, string? culture = null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user