using Microsoft.EntityFrameworkCore;
using OliverBooth.Data.Web;
namespace OliverBooth.Services;
///
/// Represents a service which can perform programming language lookup.
///
public interface IProgrammingLanguageService
{
///
/// Returns the human-readable name of a language.
///
/// The alias of the language.
/// The human-readable name, or if the name could not be found.
string GetLanguageName(string alias);
}
///
internal sealed class ProgrammingLanguageService : IProgrammingLanguageService
{
private readonly IDbContextFactory _dbContextFactory;
///
/// Initializes a new instance of the class.
///
/// The factory.
public ProgrammingLanguageService(IDbContextFactory dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
///
public string GetLanguageName(string alias)
{
using WebContext context = _dbContextFactory.CreateDbContext();
ProgrammingLanguage? language = context.ProgrammingLanguages.FirstOrDefault(l => l.Key == alias);
return language?.Name ?? alias;
}
}