mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
2.0 KiB
53 lines
2.0 KiB
using System; |
|
using System.Collections.Generic; |
|
using System.Globalization; |
|
using System.Linq; |
|
using System.Threading; |
|
using System.Threading.Tasks; |
|
using ErsatzTV.Core.Interfaces.Repositories; |
|
using ErsatzTV.Infrastructure.Data; |
|
using ErsatzTV.Infrastructure.Extensions; |
|
using LanguageExt; |
|
using MediatR; |
|
using Microsoft.EntityFrameworkCore; |
|
|
|
namespace ErsatzTV.Application.MediaItems.Queries |
|
{ |
|
public class GetAllLanguageCodesHandler : IRequestHandler<GetAllLanguageCodes, List<CultureInfo>> |
|
{ |
|
private readonly IDbContextFactory<TvContext> _dbContextFactory; |
|
private readonly IMediaItemRepository _mediaItemRepository; |
|
|
|
public GetAllLanguageCodesHandler( |
|
IDbContextFactory<TvContext> dbContextFactory, |
|
IMediaItemRepository mediaItemRepository) |
|
{ |
|
_dbContextFactory = dbContextFactory; |
|
_mediaItemRepository = mediaItemRepository; |
|
} |
|
|
|
public async Task<List<CultureInfo>> Handle(GetAllLanguageCodes request, CancellationToken cancellationToken) |
|
{ |
|
await using TvContext dbContext = _dbContextFactory.CreateDbContext(); |
|
|
|
var result = new System.Collections.Generic.HashSet<CultureInfo>(); |
|
|
|
CultureInfo[] allCultures = CultureInfo.GetCultures(CultureTypes.NeutralCultures); |
|
List<string> mediaCodes = await _mediaItemRepository.GetAllLanguageCodes(); |
|
foreach (string mediaCode in mediaCodes) |
|
{ |
|
foreach (string code in await dbContext.LanguageCodes.GetAllLanguageCodes(mediaCode)) |
|
{ |
|
Option<CultureInfo> maybeCulture = allCultures.Find( |
|
c => string.Equals(code, c.ThreeLetterISOLanguageName, StringComparison.OrdinalIgnoreCase)); |
|
foreach (CultureInfo culture in maybeCulture) |
|
{ |
|
result.Add(culture); |
|
} |
|
} |
|
} |
|
|
|
return result.ToList(); |
|
} |
|
} |
|
}
|
|
|