using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Common.Data.Web;
using OliverBooth.Common.Services;
using OliverBooth.Data.Web;
namespace OliverBooth.Services;
///
internal sealed class CodeSnippetService : ICodeSnippetService
{
private readonly IDbContextFactory _dbContextFactory;
///
/// Initializes a new instance of the class.
///
/// The factory.
public CodeSnippetService(IDbContextFactory dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
///
public IReadOnlyList GetLanguagesForSnippet(int id)
{
var languages = new HashSet();
using WebContext context = _dbContextFactory.CreateDbContext();
foreach (CodeSnippet snippet in context.CodeSnippets.Where(s => s.Id == id))
{
languages.Add(snippet.Language);
}
return languages.ToArray();
}
///
public bool TryGetCodeSnippetForLanguage(int id, string language, [NotNullWhen(true)] out ICodeSnippet? snippet)
{
if (language is null)
{
throw new ArgumentNullException(nameof(language));
}
using WebContext context = _dbContextFactory.CreateDbContext();
IQueryable snippets = context.CodeSnippets.Where(s => s.Id == id);
snippet = snippets.FirstOrDefault(s => s.Language == language);
return snippet is not null;
}
}