basic structure for memory stored forum
This commit is contained in:
parent
8ef50cc5cd
commit
5d202e9c36
|
@ -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<string, Post>();
|
||||||
|
private citations = new Map<string, Citation>();
|
||||||
|
private members = new Map<string, Member>();
|
||||||
|
|
||||||
|
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<string, Vertex> { return this.posts; }
|
||||||
|
public get edges(): Map<string, Edge> { 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue