Skip to content

Commit

Permalink
Add delete functionality to message controller and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
xuelink committed Apr 4, 2024
1 parent 2cf9886 commit c61c767
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/controllers/message.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,60 @@ export default class MessageController {
});
}
}

// this.router.delete("/:id", this.controller.delete);
// delete a message
async delete(req: Request, res: Response) {
try {
throwIfMissing(req.headers, ["x-appwrite-user-id", "x-appwrite-jwt"]);
throwIfMissing(req.params, ["id"]);

const sender: string = req.headers["x-appwrite-user-id"] as string;
const jwt: string = req.headers["x-appwrite-jwt"] as string;
const messageId: string = req.params.id;
console.log(messageId);

// Check JWT
const verifyUser = new Client()
.setEndpoint(env.APP_ENDPOINT)
.setProject(env.APP_PROJECT)
.setJWT(jwt);

const account = new Account(verifyUser);
const user = await account.get();

if (user.$id !== sender) {
return res.status(400).json({ ok: false, error: "jwt is invalid" });
}

const client = new Client()
.setEndpoint(env.APP_ENDPOINT)
.setProject(env.APP_PROJECT)
.setKey(env.API_KEY);

const database = new Databases(client);

// Delete the message
let message = await database.updateDocument(
env.APP_DATABASE,
env.MESSAGES_COLLECTION,
messageId,
{ body: "_deleted_" }
);
console.log(message);

if (message) {
console.log("message deleted");
res.status(200).json(message);
} else {
console.log("message not deleted");
res.status(304).json({ ok: false, message: "Not Modified" });
}
} catch (err) {
res.status(500).json({
message: "Internal Server Error!",
err: err,
});
}
}
}
1 change: 1 addition & 0 deletions src/routes/message.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class MessageRoutes {
intializeRoutes() {
this.router.post("/", this.controller.create);
this.router.patch("/", this.controller.update);
this.router.delete("/:id", this.controller.delete);
}
}

Expand Down

0 comments on commit c61c767

Please sign in to comment.