/* This file is part of NKK. NKK is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. NKK is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with NKK. If not, see . */ using System.Collections.Concurrent; namespace NKK; public class PostStore { // dict by Type (T : IPostPayload) to another dict // *that* dict is of string (post fullId's) to instances of the aforementioned T private readonly ConcurrentDictionary> _posts = new(); /// /// Get all posts of a certain type /// /// /// Implementor of /// /// /// of posts /// public IEnumerable GetAll() where T : class, IPostPayload { if (!this._posts.ContainsKey(typeof(T))) return []; return this._posts[typeof(T)].Values.Cast(); } /// /// Try to get a post by its /// /// /// Full match for some post's /// /// /// Implementor of /// /// /// Corresponding post if found, or null /// public T? Get(PostId id) where T : class, IPostPayload { if (!this._posts.ContainsKey(typeof(T))) return null; if (!this._posts[typeof(T)].TryGetValue(id.FullId, out var post)) return null; return (T)post; } /// /// Add (or update) a post in the store by /// /// /// ID bundle. Updates only on full match, otherwise adds /// /// /// IPostPayload to add/update /// /// /// Implementor of /// public void AddOrUpdate(PostId id, T post) where T : class, IPostPayload { if (!this._posts.ContainsKey(typeof(T))) this._posts[typeof(T)] = new ConcurrentDictionary(); this._posts[typeof(T)][id.FullId] = post; } /// /// Remove a post from the store by . /// /// /// Full match for some post's . Will accept anything, /// this method does not error if there is no such post by that ID. /// /// /// Implementor of /// public void Remove(PostId id) where T : class, IPostPayload { if (this._posts.ContainsKey(typeof(T))) this._posts[typeof(T)].Remove(id.FullId, out _); } }