Polling Webs App - Deleting Polls & Updating Options
Progress for this project at the moment is rather incremental, adding only minor functionality that while making it more capable doesn't add any additional features. The biggest addition this update is the ability to update an option by its id. Expanding upon the work I have done previously for polls I have added the ability to delete a poll by its id as well.
Deleting Polls
Deleting Poll Service
In the "services/polls" directory add a new service file named "deletePolls.ts". Similar to update polls the following were imported "eq" from "drizzle-orm", "db" from "drizzle.ts", and "polls" from "schema.ts". With all services so far an export function was created this time called "deletePollById". The function queries the database to locate a poll with a matching provided ID then deletes it, since the schema has cascading configured it will also delete options and responses that match the poll id.
Deleting Poll Controller
In "polls.controller.ts" a controller has to be made for "deletePollsById" function. Start by importing the "deletePollsById" into "polls.controller.ts", then create an export function called "deletePollByIdController". This controller uses a try/catch block. Within the try block, the "pollID" is retrieved from the request's query parameters and validated. If valid, the "deletePollById" service is called to delete the poll, and a success message is returned, while the catch block handles the any errors.
Deleting Poll Router
A new route was made for the server to handle deleting polls, the route's prefix is "/delete-by-id" and the HTTP method is a DELETE. Import the "deletePollByIdController" into "polls.router.ts" and have the newly created route use the imported function.
Updating Options
Updating Options Service
Up to this point options have been directly connected to polls, this will the first option service that can be used independently of polls. Create a new file called "updateOptions.ts" in the "services/options" directory. Standard for our services import "eq" from "drizzle-orm", "db" from "drizzle.ts", and its schema "options" from "schema.ts". Add an export function called "updateOptionById". The function takes arguments "optionID" with a type of any and "updatedOption" with a type of string. Query the database using the "pollingID" and update the option field using the "updatedOption".
import { eq } from "drizzle-orm";
import { db } from "../../db/drizzle";
import { options } from "../../db/schema";
export const updateOptionById = async (optionID: any, updatedOption: string) => {
const updatePoll = await db
.update(options)
.set({
option: updatedOption
})
.where(eq(options.id, optionID))
return updatePoll
}
Updating Options Controller
Building upon the updating Options service, a controller will need to be made. In the "controllers" directory make a new file called "options.controller.ts". In the options controller import "updateOptions.ts" from the services directory. Add an export function "updateOptionByIdController" that uses a try-catch block. Within the try block, destructure the "optionID" and "updatedOption" from the request body, validate them both, and call the "updateOptoinById" function to perform the database update. Once the database update is completed is send a success response, while the catch block handles the any errors.
import { Request, Response } from "express";
import { updateOptionById } from "../services/options/updateOptions";
export const updateOptionByIdController = async (req: Request, res: Response) => {
try {
const { optionID, updatedOption } = req.body;
if (!optionID) {
return res.sendStatus(400);
}
const poll = await updateOptionById(optionID, updatedOption)
return res.status(200).send("Poll was updated");
} catch (error) {
console.error(error);
return res.sendStatus(400);
}
}
Updating Options Router
Next, for the update options is to configure a router for the options. Import "Router" from "express" to setup the router and "updateOptionByIdController" from "options.controller.ts". Create a const called "optionRouter" that calls the Router function. Define a new route with a PATCH method that has a prefix of "update-by-id" and uses the "updateOptionByIdController".
import { Router } from "express";
import { updateOptionByIdController } from "../controllers/options.controller";
export const optionRouter = Router();
optionRouter.patch("update-by-id", updateOptionByIdController);
Creating an Options Route
- Import the options router into app.ts
- Have a new app.use "/options" for the
Lastly, in order to use the router we need to make sure the server is able to use the "options.routes.ts". In the "app.ts" file import the options router. Have a new "app.use" method, and specify the path as "/options" while using the "optionRouter".
import { optionRouter } from './router/options.routes';
app.use("/options", optionRouter);
Conclusion
This was a fairly simple update that built upon my previous work. This was the first update that allowed my app to be able to have options my independent from polls. As this project continues more combined and independent systems will need to be developed, this was, but a brick in the foundation that will continue to grow.
If you would like to check out the code or us it for yourself: Github Link
If this blog was helpful to you please consider subscribing.