jackfiled
ca4f6449d3
All checks were successful
Build blog docker image / Build-Blog-Image (push) Successful in 1m15s
Reviewed-on: #2
58 lines
1.5 KiB
C#
58 lines
1.5 KiB
C#
using System.Collections.Concurrent;
|
|
using YaeBlog.Core.Models;
|
|
|
|
namespace YaeBlog.Core.Services;
|
|
|
|
public class EssayContentService
|
|
{
|
|
private readonly ConcurrentDictionary<string, BlogEssay> _essays = new();
|
|
|
|
private readonly Dictionary<string, List<BlogEssay>> _tags = [];
|
|
|
|
public bool TryGet(string key, out BlogEssay? essay)
|
|
=> _essays.TryGetValue(key, out essay);
|
|
|
|
public bool TryAdd(string key, BlogEssay essay) => _essays.TryAdd(key, essay);
|
|
|
|
public IDictionary<string, BlogEssay> Essays => _essays;
|
|
|
|
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];
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (KeyValuePair<string,List<BlogEssay>> pair in _tags)
|
|
{
|
|
pair.Value.Sort();
|
|
}
|
|
}
|
|
|
|
public IEnumerable<KeyValuePair<string, int>> Tags => from item in _tags
|
|
orderby item.Value.Count descending
|
|
select KeyValuePair.Create(item.Key, item.Value.Count);
|
|
|
|
public int TagCount => _tags.Count;
|
|
|
|
public IEnumerable<BlogEssay> GetTag(string tag)
|
|
{
|
|
if (_tags.TryGetValue(tag, out var list))
|
|
{
|
|
return list;
|
|
}
|
|
|
|
throw new KeyNotFoundException("Selected tag not found.");
|
|
}
|
|
}
|