diff --git a/YaeBlog.Core/Services/EssayContentService.cs b/YaeBlog.Core/Services/EssayContentService.cs index 472126b..5ae56b5 100644 --- a/YaeBlog.Core/Services/EssayContentService.cs +++ b/YaeBlog.Core/Services/EssayContentService.cs @@ -7,6 +7,8 @@ public class EssayContentService { private readonly ConcurrentDictionary _essays = new(); + private readonly Dictionary> _tags = []; + public bool TryGet(string key, out BlogEssay? essay) => _essays.TryGetValue(key, out essay); @@ -15,4 +17,35 @@ public class EssayContentService public IEnumerable> Essays => _essays; public int Count => _essays.Count; + + public void RefreshTags() + { + foreach (BlogEssay essay in _essays.Values) + { + foreach (string tag in essay.Tags) + { + if (_tags.TryGetValue(tag, out var list)) + { + list.Add(essay); + } + else + { + _tags[tag] = [essay]; + } + } + } + } + + public IEnumerable> Tags => from item in _tags + select KeyValuePair.Create(item.Key, item.Value.Count); + + public IEnumerable GetTag(string tag) + { + if (_tags.TryGetValue(tag, out var list)) + { + return list; + } + + throw new KeyNotFoundException("Selected tag not found."); + } } diff --git a/YaeBlog.Core/Services/RendererService.cs b/YaeBlog.Core/Services/RendererService.cs index 20c377e..e5fde22 100644 --- a/YaeBlog.Core/Services/RendererService.cs +++ b/YaeBlog.Core/Services/RendererService.cs @@ -66,6 +66,7 @@ public class RendererService(ILogger logger, }); await PostProcess(postProcessEssays); + essayContentService.RefreshTags(); _stopwatch.Stop(); logger.LogInformation("Render finished, consuming {} s.",