Skip to content

Commit 78f4caa

Browse files
AndyButlandCopilotkjac
authored
Refactoring: Add extension method for retrieval of language ISO codes if that's all we need (#20324)
* Retrieve only ISO codes from the database rather than full language objects if that's all we need. * Apply suggestions from code review Co-authored-by: Copilot <[email protected]> * Removed repository updates and migrated the new service method to an extension method. * Fixed issue after merge. * Removed left-over using --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Kenn Jacobsen <[email protected]>
1 parent 3f0428c commit 78f4caa

File tree

15 files changed

+64
-35
lines changed

15 files changed

+64
-35
lines changed

src/Umbraco.Cms.Api.Management/Factories/DictionaryPresentationFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using Umbraco.Cms.Core.Mapping;
1+
using Umbraco.Cms.Core.Extensions;
2+
using Umbraco.Cms.Core.Mapping;
23
using Umbraco.Cms.Core.Models;
34
using Umbraco.Cms.Core.Services;
45
using Umbraco.Cms.Api.Management.Models;
@@ -23,8 +24,7 @@ public async Task<DictionaryItemResponseModel> CreateDictionaryItemViewModelAsyn
2324
{
2425
DictionaryItemResponseModel dictionaryResponseModel = _umbracoMapper.Map<DictionaryItemResponseModel>(dictionaryItem)!;
2526

26-
var validLanguageIsoCodes = (await _languageService.GetAllAsync())
27-
.Select(language => language.IsoCode)
27+
var validLanguageIsoCodes = (await _languageService.GetAllIsoCodesAsync())
2828
.ToArray();
2929
IDictionaryTranslation[] validTranslations = dictionaryItem.Translations
3030
.Where(t => validLanguageIsoCodes.Contains(t.LanguageIsoCode))

src/Umbraco.Cms.Api.Management/Factories/UserGroupPresentationFactory.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,7 @@ private static string CleanUserGroupNameOrAliasForXss(string input)
211211

212212
private async Task<Attempt<IEnumerable<string>, UserGroupOperationStatus>> MapLanguageIdsToIsoCodeAsync(IEnumerable<int> ids)
213213
{
214-
IEnumerable<ILanguage> languages = await _languageService.GetAllAsync();
215-
string[] isoCodes = languages
216-
.Where(x => ids.Contains(x.Id))
217-
.Select(x => x.IsoCode)
218-
.ToArray();
214+
string[] isoCodes = await _languageService.GetIsoCodesByIdsAsync(ids.ToArray());
219215

220216
// if a language id does not exist, it simply not returned.
221217
// We do this so we don't have to clean up user group data when deleting languages and to make it easier to restore accidentally removed languages
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Umbraco.Cms.Core.Services;
2+
3+
namespace Umbraco.Cms.Core.Extensions;
4+
5+
/// <summary>
6+
/// Provides extension methods for <see cref="ILanguageService"/>.
7+
/// </summary>
8+
public static class LanguageServiceExtensions
9+
{
10+
/// <summary>
11+
/// Retrieves all ISO codes of all languages.
12+
/// </summary>
13+
/// <param name="service">The <see cref="ILanguageService"/>.</param>
14+
/// <returns>A collection of language ISO codes.</returns>
15+
public static async Task<IEnumerable<string>> GetAllIsoCodesAsync(this ILanguageService service) => (await service.GetAllAsync()).Select(x => x.IsoCode);
16+
}

src/Umbraco.Core/Persistence/Repositories/ILanguageRepository.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,11 @@ public interface ILanguageRepository : IReadWriteQueryRepository<int, ILanguage>
3838
/// </remarks>
3939
int? GetDefaultId();
4040

41+
/// <summary>
42+
/// Gets multiple language ISO codes from the provided Ids.
43+
/// </summary>
44+
/// <param name="ids">The language Ids.</param>
45+
/// <param name="throwOnNotFound">Indicates whether to throw an exception if the provided Id is not found as a language.</param>
46+
/// <returns></returns>
4147
string[] GetIsoCodesByIds(ICollection<int> ids, bool throwOnNotFound = true);
4248
}

src/Umbraco.Core/Routing/PublishedUrlInfoProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.Extensions.Logging;
2+
using Umbraco.Cms.Core.Extensions;
23
using Umbraco.Cms.Core.Models;
34
using Umbraco.Cms.Core.Models.PublishedContent;
45
using Umbraco.Cms.Core.Services;
@@ -42,7 +43,7 @@ public PublishedUrlInfoProvider(
4243
public async Task<ISet<UrlInfo>> GetAllAsync(IContent content)
4344
{
4445
HashSet<UrlInfo> urlInfos = [];
45-
var cultures = (await _languageService.GetAllAsync()).Select(x => x.IsoCode).ToArray();
46+
IEnumerable<string> cultures = await _languageService.GetAllIsoCodesAsync();
4647

4748
// First we get the urls of all cultures, using the published router, meaning we respect any extensions.
4849
foreach (var culture in cultures)

src/Umbraco.Core/Routing/UrlProviderExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Logging;
33
using Umbraco.Cms.Core;
44
using Umbraco.Cms.Core.DependencyInjection;
5+
using Umbraco.Cms.Core.Extensions;
56
using Umbraco.Cms.Core.Models;
67
using Umbraco.Cms.Core.Models.PublishedContent;
78
using Umbraco.Cms.Core.PublishedCache;
@@ -118,7 +119,7 @@ public static async Task<IEnumerable<UrlInfo>> GetContentUrlsAsync(
118119
// and, not only for those assigned to domains in the branch, because we want
119120
// to show what GetUrl() would return, for every culture.
120121
var urls = new HashSet<UrlInfo>();
121-
var cultures = (await languageService.GetAllAsync()).Select(x => x.IsoCode).ToList();
122+
IEnumerable<string> cultures = await languageService.GetAllIsoCodesAsync();
122123

123124
// get all URLs for all cultures
124125
// in a HashSet, so de-duplicates too

src/Umbraco.Core/Services/ContentEditingService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.Extensions.Logging;
22
using Microsoft.Extensions.Options;
33
using Umbraco.Cms.Core.Configuration.Models;
4+
using Umbraco.Cms.Core.Extensions;
45
using Umbraco.Cms.Core.Models;
56
using Umbraco.Cms.Core.Models.ContentEditing;
67
using Umbraco.Cms.Core.Models.Membership;
@@ -104,8 +105,8 @@ public async Task<Attempt<ContentValidationResult, ContentEditingOperationStatus
104105
{
105106
// If no cultures are provided, we are asking to validate all cultures. But if the user doesn't have access to all, we
106107
// should only validate the ones they do.
107-
var allCultures = (await _languageService.GetAllAsync()).Select(x => x.IsoCode).ToList();
108-
return allowedCultures.Count == allCultures.Count ? null : allowedCultures;
108+
IEnumerable<string> allCultures = await _languageService.GetAllIsoCodesAsync();
109+
return allowedCultures.Count == allCultures.Count() ? null : allowedCultures;
109110
}
110111

111112
// If explicit cultures are provided, we should only validate the ones the user has access to.

src/Umbraco.Core/Services/ContentPublishingService.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Microsoft.Extensions.Options;
33
using Umbraco.Cms.Core.Configuration.Models;
44
using Umbraco.Cms.Core.Events;
5+
using Umbraco.Cms.Core.Extensions;
56
using Umbraco.Cms.Core.Models;
67
using Umbraco.Cms.Core.Models.ContentEditing;
78
using Umbraco.Cms.Core.Models.ContentPublishing;
@@ -165,7 +166,7 @@ public async Task<Attempt<ContentPublishingResult, ContentPublishingOperationSta
165166
return Attempt.FailWithStatus(ContentPublishingOperationStatus.CannotPublishInvariantWhenVariant, new ContentPublishingResult());
166167
}
167168

168-
IEnumerable<string> validCultures = (await _languageService.GetAllAsync()).Select(x => x.IsoCode);
169+
IEnumerable<string> validCultures = await _languageService.GetAllIsoCodesAsync();
169170
if (validCultures.ContainsAll(cultures) is false)
170171
{
171172
scope.Complete();
@@ -488,7 +489,7 @@ private async Task<Attempt<ContentPublishingOperationStatus>> UnpublishMultipleC
488489
return Attempt.Fail(ContentPublishingOperationStatus.CannotPublishVariantWhenNotVariant);
489490
}
490491

491-
var validCultures = (await _languageService.GetAllAsync()).Select(x => x.IsoCode).ToArray();
492+
var validCultures = (await _languageService.GetAllIsoCodesAsync()).ToArray();
492493

493494
foreach (var culture in cultures)
494495
{

src/Umbraco.Core/Services/ContentValidationServiceBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System.ComponentModel.DataAnnotations;
2+
using Umbraco.Cms.Core.Extensions;
23
using Umbraco.Cms.Core.Models;
34
using Umbraco.Cms.Core.Models.ContentEditing;
45
using Umbraco.Cms.Core.Models.ContentEditing.Validation;
@@ -137,7 +138,7 @@ public async Task<bool> ValidateCulturesAsync(ContentEditingModelBase contentEdi
137138
return invalidCultures.IsCollectionEmpty();
138139
}
139140

140-
private async Task<string[]> GetCultureCodes() => (await _languageService.GetAllAsync()).Select(language => language.IsoCode).ToArray();
141+
private async Task<string[]> GetCultureCodes() => (await _languageService.GetAllIsoCodesAsync()).ToArray();
141142

142143
private IEnumerable<PropertyValidationError> ValidateProperty(IPropertyType propertyType, PropertyValueModel? propertyValueModel, PropertyValidationContext validationContext)
143144
{

src/Umbraco.Core/Services/DocumentUrlService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ public async Task<IEnumerable<UrlInfo>> ListUrlsAsync(Guid contentKey)
856856
.Concat(_contentService.GetAncestors(documentIdAttempt.Result).Select(x => x.Key).Reverse());
857857

858858
IEnumerable<ILanguage> languages = await _languageService.GetAllAsync();
859-
var cultures = languages.ToDictionary(x=>x.IsoCode);
859+
var cultures = languages.ToDictionary(x => x.IsoCode);
860860

861861
Guid[] ancestorsOrSelfKeysArray = ancestorsOrSelfKeys as Guid[] ?? ancestorsOrSelfKeys.ToArray();
862862
Dictionary<Guid, Task<ILookup<string, Domain>>> ancestorOrSelfKeyToDomains = ancestorsOrSelfKeysArray

0 commit comments

Comments
 (0)