From 5d202e9c3638f7c697d4690830a7fbad493834e1 Mon Sep 17 00:00:00 2001 From: Chegele Date: Thu, 6 Jul 2023 17:16:21 -0400 Subject: [PATCH] basic structure for memory stored forum --- src/services/reputation/components/forum.ts | 75 +++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/services/reputation/components/forum.ts diff --git a/src/services/reputation/components/forum.ts b/src/services/reputation/components/forum.ts new file mode 100644 index 0000000..f9b81d9 --- /dev/null +++ b/src/services/reputation/components/forum.ts @@ -0,0 +1,75 @@ +import { Edge, Graph, Vertex } from "../graph.interface"; +import { Citation } from "./citation"; +import { Member } from "./member"; +import { Post } from "./post"; + +export default class Forum implements Graph { + + private posts = new Map(); + private citations = new Map(); + private members = new Map(); + + constructor(posts: Post[], citations: Citation[], members: Member[]) { + posts.forEach(post => this.posts.set(post.id, post)); + citations.forEach(citation => this.citations.set(citation.id, citation)); + members.forEach(member => this.members.set(member.id, member)); + } + + public get vertices(): Map { return this.posts; } + public get edges(): Map { return this.citations; } + + public getAllVertices(): Vertex[] { return this.getAllPosts(); } + public getVertex(id: string): Vertex { return this.getPost(id); } + public addVertex(vertex: Vertex): void { return this.addPost(vertex as Post); } + public deleteVertex(vertex: string | Vertex): void { return this.deletePost(vertex as any); } + + public getAllEdges(): Edge[] { return this.getAllCitations(); } + public getEdge(id: string): Edge { return this.getCitation(id); } + public addEdge(edge: Edge): void { return this.addCitation(edge as Citation); } + public deleteEdge(edge: string | Edge): void { return this.deleteCitation(edge as any); } + + public getAllPosts() { + return Array.from(this.posts.values()); + } + + public getPost(id: string) { + return this.posts.get(id); + } + + public addPost(post: Post) { + this.posts.set(post.id, post); + } + + public deletePost(post: string | Post) { + if (typeof post === "string") this.posts.delete(post); + else for (const [key, val] of this.posts.entries()) { + if (val === post) { + this.posts.delete(key); + break; + } + } + } + + public getAllCitations() { + return Array.from(this.citations.values()); + } + + public getCitation(id: string) { + return this.citations.get(id); + } + + public addCitation(citation: Citation) { + this.citations.set(citation.id, citation); + } + + public deleteCitation(citation: string | Citation) { + if (typeof citation === "string") this.citations.delete(citation); + else for (const [key, val] of this.citations.entries()) { + if (val === citation) { + this.citations.delete(key); + break; + } + } + } + +} \ No newline at end of file