oliverbooth.dev/OliverBooth/Services/CodeSnippetService.cs
Oliver Booth 6ec4103a3a
refactor: separate Markdig extensions from project
Also introduces .Common project to house common references and types
2024-05-05 02:18:20 +01:00

51 lines
1.6 KiB
C#

using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using OliverBooth.Common.Data.Web;
using OliverBooth.Common.Services;
using OliverBooth.Data.Web;
namespace OliverBooth.Services;
/// <inheritdoc />
internal sealed class CodeSnippetService : ICodeSnippetService
{
private readonly IDbContextFactory<WebContext> _dbContextFactory;
/// <summary>
/// Initializes a new instance of the <see cref="CodeSnippetService" /> class.
/// </summary>
/// <param name="dbContextFactory">The <see cref="WebContext" /> factory.</param>
public CodeSnippetService(IDbContextFactory<WebContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
/// <inheritdoc />
public IReadOnlyList<string> GetLanguagesForSnippet(int id)
{
var languages = new HashSet<string>();
using WebContext context = _dbContextFactory.CreateDbContext();
foreach (CodeSnippet snippet in context.CodeSnippets.Where(s => s.Id == id))
{
languages.Add(snippet.Language);
}
return languages.ToArray();
}
/// <inheritdoc />
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<CodeSnippet> snippets = context.CodeSnippets.Where(s => s.Id == id);
snippet = snippets.FirstOrDefault(s => s.Language == language);
return snippet is not null;
}
}