CRUD operations for Posts

- added all CRUD operations for Post model
- add() also creates POST_BY_MEMBER and POST_FROM_NETWORK edges
- delete() also deletes associated edges
- delete() currently does not reallocate node/edge indices after deleting
- will write the data to the db.json file and global.db.nodes
This commit is contained in:
2026-02-08 22:34:18 -05:00
parent d6520bf007
commit aaf9d56b1b
10 changed files with 342 additions and 35 deletions

View File

@@ -1,6 +1,7 @@
import { z } from 'zod';
export default class Post {
prefix = "POST"
indices = null
constructor(indices) {
@@ -8,13 +9,16 @@ export default class Post {
}
schema = z.object({
id: z.number(),
text: z.string(),
time: z.string(),
sentBy: z.string()
sentBy: z.string(),
edited: z.boolean()
})
makeID(forum, number) {
return `POST-${forum}-${number}`
// return `POST-${forum}-${number}`
return `POST-${number}`
}
save(post, id) {
@@ -32,30 +36,130 @@ export default class Post {
}
}
get(forum, number) {
get(forum, by, authorId = null) {
let result = []
let limit = Math.min(number, this.entries.length)
for(let i=1; i<=limit; i++) {
let id = this.makeID(forum, i)
let post = this.entries[this.ids[id]]
let {firstName, lastName} = global.db.members.get(post.sentBy)
let seededObj = {
...post
}
seededObj.sentByID = post.sentBy
seededObj.sentBy = firstName + " " + lastName
result.push(seededObj)
if (by === "member" && authorId) {
result = this.getByMember(authorId)
} else { // network
let { id: networkId } = global.db.networks.getByAbbreviation(forum)
result = this.getByNetwork(networkId)
}
return result
}
getByID(id) {
if(typeof id === 'string') {
id = id.split("-")[1]
}
return {
id,
authorId: this.getAuthor(id),
...global.db.nodes[this.indices[0] + (id - 1)]
}
}
getAuthor(postId) {
return db.POST_BY_MEMBER.getAuthorId(`${this.prefix}-${postId}`);
}
getByNetwork(id) {
let connections = db.POST_FROM_NETWORK.getByNetwork(id)
let posts = []
connections.forEach((conn) => {
posts.push(this.getByID(conn.from))
})
return posts
}
getByMember(stringID) {
let connections = db.POST_BY_MEMBER.getByMember(stringID)
let posts = []
connections.forEach((conn) => {
posts.push(this.getByID(conn.from))
})
return posts
}
async edit(id, text, userEmail) {
try {
let userId = global.db.members.getByEmail(userEmail).id
let postToEdit = this.getByID(id)
if (postToEdit.authorId !== userId) {
throw new global.ServerError(401, "Not authorized to delete post!")
}
postToEdit.text = text
postToEdit.time = global.currentTime()
postToEdit.edited = true;
let { authorId, ...rest } = postToEdit
global.db.editNode(this.prefix, id, rest)
return postToEdit
} catch(e) {
console.error(e)
throw new global.ServerError(400, "Failed to delete post!")
}
}
async delete(id, forum, userEmail) {
try {
let userId = global.db.members.getByEmail(userEmail).id
let network = global.db.networks.getByAbbreviation(forum)
if (this.getAuthor(id) !== userId) {
throw new global.ServerError(401, "Not authorized to delete post!")
}
global.db.deleteNode(this.prefix, id)
if(network) {
global.db.edge.delete({
type: "FROM",
from: `${this.prefix}-${id}`,
to: `NETWORK-${network.id}`
})
}
global.db.edge.delete({
type: "BY",
from: `${this.prefix}-${id}`,
to: `MEMBER-${userId}`
})
} catch(e) {
console.error(e)
throw new global.ServerError(400, "Failed to delete post!")
}
}
async add(text, forum, userEmail) {
let newPost = {}
newPost.text = text
newPost.sentBy = db.members.getIDFromEmail(userEmail)
newPost.time = global.currentTime()
let sender = global.db.members.getByEmail(userEmail)
let network = global.db.networks.getByAbbreviation(forum)
let idNumber = this.entries.length+1
super.add(this.makeID(forum, idNumber), newPost)
newPost.text = text
newPost.time = global.currentTime()
try {
newPost.sentBy = `${sender.firstName} ${sender.lastName}`
global.db.addNode(this.prefix, newPost)
if(network) {
global.db.edge.add({
type: "FROM",
from: `${this.prefix}-${global.db.getCurrentIndex(this)}`,
to: `NETWORK-${network.id}`
})
}
global.db.edge.add({
type: "BY",
from: `${this.prefix}-${global.db.getCurrentIndex(this)}`,
to: `MEMBER-${sender.id}`
})
return `${this.prefix}-${global.db.getCurrentIndex(this)}`
} catch(e) {
console.error(e)
throw new global.ServerError(400, "Failed to add member!");
}
}
}