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

@@ -8,35 +8,99 @@ const sendSchema = z.object({
const getSchema = z.object({
forum: z.string(),
number: z.number()
by: z.string(),
authorId: z.number()
})
.strict()
const deleteSchema = z.object({
forum: z.string(),
id: z.number()
})
.strict()
const putSchema = z.object({
forum: z.string(),
id: z.number(),
text: z.string()
})
.strict()
export default class ForumHandler {
static handleSend(msg, ws) {
static async handleSend(msg, ws) {
try {
global.db.posts.add(msg.text, msg.forum, ws.userEmail)
global.Socket.broadcast({event: "new-post", app: "FORUM", forum: msg.forum, msg: this.handleGet({forum: msg.forum, number: 100})})
return {success: true}
let postId = await global.db.posts.add(msg.text, msg.forum, ws.userEmail)
let newPost = global.db.posts.getByID(postId)
if (newPost) {
global.Socket.broadcast({
event: "new-post",
app: "FORUM",
forum: msg.forum,
msg: newPost
})
return {success: true}
} else {
return {success: false}
}
} catch(e) {
console.error(e)
}
}
static handleGet(msg) {
let data = global.db.posts.get(msg.forum, msg.number)
let data = global.db.posts.get(msg.forum, msg.by, msg.authorId)
return data
}
static handle(operation, msg, ws) {
static async handleDelete(msg, ws) {
try {
await global.db.posts.delete(msg.id, msg.forum, ws.userEmail)
global.Socket.broadcast({
event: "deleted-post",
app: "FORUM",
forum: msg.forum,
msg: msg.id
})
return {success: true}
} catch(e) {
console.error(e)
return {success: false}
}
}
static async handlePut(msg, ws) {
try {
let editedPost = await global.db.posts.edit(msg.id, msg.text, ws.userEmail)
console.log(editedPost)
if (editedPost) {
global.Socket.broadcast({
event: "edited-post",
app: "FORUM",
forum: msg.forum,
msg: editedPost
})
}
return {success: true}
} catch(e) {
console.error(e)
return {success: false}
}
}
static async handle(operation, msg, ws) {
switch(operation) {
case "SEND":
if(!sendSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
return this.handleSend(msg, ws)
return await this.handleSend(msg, ws)
case "GET":
if(!getSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
return this.handleGet(msg)
case "DELETE":
if(!deleteSchema.safeParse(msg).success) throw new Error("Incorrectly formatted Forum ws message!")
return this.handleDelete(msg, ws)
case "PUT":
if (!putSchema.safeParse(msg).success) throw new Error("Incorrectly formatted ws message!")
return this.handlePut(msg, ws)
}
}