Skip to content

Commit

Permalink
Add endpoint for retrieveing the configured default language (#16086)
Browse files Browse the repository at this point in the history
* Add endpoint for retrieveing the configured default language

* Update OpenApi.json
  • Loading branch information
kjac authored Apr 25, 2024
1 parent c7b3175 commit 0cac0f8
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Umbraco.Cms.Api.Management.ViewModels.Language.Item;
using Umbraco.Cms.Core.Mapping;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.OperationStatus;

namespace Umbraco.Cms.Api.Management.Controllers.Language.Item;

[ApiVersion("1.0")]
public class DefaultLanguageEntityController : LanguageEntityControllerBase

Check failure on line 13 in src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The type or namespace name 'LanguageEntityControllerBase' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs

View workflow job for this annotation

GitHub Actions / CodeQL-Build

The type or namespace name 'LanguageEntityControllerBase' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs

View check run for this annotation

Azure Pipelines / Umbraco CMS 9+ (Build Build Umbraco CMS)

src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs#L13

src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs(13,48): Error CS0246: The type or namespace name 'LanguageEntityControllerBase' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs

View check run for this annotation

Azure Pipelines / Umbraco CMS 9+

src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs#L13

src/Umbraco.Cms.Api.Management/Controllers/Language/Item/DefaultLanguageEntityController.cs(13,48): Error CS0246: The type or namespace name 'LanguageEntityControllerBase' could not be found (are you missing a using directive or an assembly reference?)
{
private readonly ILanguageService _languageService;
private readonly IUmbracoMapper _mapper;

public DefaultLanguageEntityController(ILanguageService languageService, IUmbracoMapper mapper)
{
_languageService = languageService;
_mapper = mapper;
}

[HttpGet("default")]
[MapToApiVersion("1.0")]
[ProducesResponseType(typeof(LanguageItemResponseModel), StatusCodes.Status200OK)]
public async Task<IActionResult> Default(CancellationToken cancellationToken)
{
ILanguage? language = await _languageService.GetDefaultLanguageAsync();
return language is not null
? Ok(_mapper.Map<ILanguage, LanguageItemResponseModel>(language))
: OperationStatusResult(
LanguageOperationStatus.NotFound,
problemDetailsBuilder => NotFound(problemDetailsBuilder.WithTitle("The default language could not be found.")));
}
}
35 changes: 35 additions & 0 deletions src/Umbraco.Cms.Api.Management/OpenApi.json
Original file line number Diff line number Diff line change
Expand Up @@ -11419,6 +11419,41 @@
]
}
},
"/umbraco/management/api/v1/item/language/default": {
"get": {
"tags": [
"Language"
],
"operationId": "GetItemLanguageDefault",
"responses": {
"200": {
"description": "Success",
"content": {
"application/json": {
"schema": {
"oneOf": [
{
"$ref": "#/components/schemas/LanguageItemResponseModel"
}
]
}
}
}
},
"401": {
"description": "The resource is protected and requires an authentication token"
},
"403": {
"description": "The authenticated user do not have access to this resource"
}
},
"security": [
{
"Backoffice User": [ ]
}
]
}
},
"/umbraco/management/api/v1/language": {
"get": {
"tags": [
Expand Down
8 changes: 8 additions & 0 deletions src/Umbraco.Core/Services/ILanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ public interface ILanguageService
/// </returns>
Task<ILanguage?> GetAsync(string isoCode);

/// <summary>
/// Gets the default <see cref="ILanguage" />
/// </summary>
/// <returns>
/// <see cref="ILanguage" />
/// </returns>
Task<ILanguage?> GetDefaultLanguageAsync();

/// <summary>
/// Gets the default language ISO code.
/// </summary>
Expand Down
9 changes: 9 additions & 0 deletions src/Umbraco.Core/Services/LanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ public LanguageService(
}
}

/// <inheritdoc />
public async Task<ILanguage?> GetDefaultLanguageAsync()
{
using (ScopeProvider.CreateCoreScope(autoComplete: true))
{
return await Task.FromResult(_languageRepository.GetByIsoCode(_languageRepository.GetDefaultIsoCode()));
}
}

/// <inheritdoc />
public async Task<string> GetDefaultIsoCodeAsync()
{
Expand Down

0 comments on commit 0cac0f8

Please sign in to comment.