Skip to content

Commit

Permalink
✨ feat(server): add swagger
Browse files Browse the repository at this point in the history
add swagger, swagger ui and file yaml for documenting the api
  • Loading branch information
EvandroCalado committed Feb 24, 2024
1 parent c7da781 commit 84a76d1
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 15 deletions.
9 changes: 6 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"cSpell.words": [
"jobify"
]
"cSpell.words": [
"Calado",
"Evandro",
"jobify",
"openapi"
]
}
8 changes: 4 additions & 4 deletions controllers/jobController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ export const createJob = async (req, res) => {
};

export const updateJob = async (req, res) => {
const updatedJob = await Job.findByIdAndUpdate(req.params.id, req.body, {
await Job.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});

res.status(StatusCodes.OK).json({ msg: "job updated", job: updatedJob });
res.status(StatusCodes.OK).json({ msg: "job updated" });
};

export const deleteJob = async (req, res) => {
const deletedJob = await Job.findByIdAndDelete(req.params.id);
await Job.findByIdAndDelete(req.params.id);

res.status(StatusCodes.OK).json({ msg: "job deleted", job: deletedJob });
res.status(StatusCodes.OK).json({ msg: "job deleted" });
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"mongoose": "^8.1.3",
"morgan": "^1.10.0",
"multer": "1.4.5-lts.1",
"nodemon": "^3.0.3"
"nodemon": "^3.0.3",
"swagger-ui-express": "^5.0.0",
"yaml": "^2.3.4"
}
}
4 changes: 2 additions & 2 deletions routes/jobRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
router.get("/", getAllJobs);
router.get("/:id", validateIdParam, getOneJob);
router.post("/", validateJobInput, createJob);
router.patch("/:id", validateJobInput, validateIdParam, updateJob);
router.delete("/:id", validateIdParam, deleteJob);
router.patch("/update/:id", validateJobInput, validateIdParam, updateJob);
router.delete("/delete/:id", validateIdParam, deleteJob);

export default router;
12 changes: 7 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import cookieParser from "cookie-parser";
import * as dotenv from "dotenv";
import express from "express";
import "express-async-errors";
import { StatusCodes } from "http-status-codes";
import fs from "fs";
import mongoose from "mongoose";
import morgan from "morgan";
import swaggerUi from "swagger-ui-express";
import YAML from "yaml";

// middlewares
import authenticateUser from "./middlewares/authMiddleware.js";
Expand All @@ -24,10 +26,10 @@ if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}

// index
app.post("/", (req, res) => {
res.status(StatusCodes.OK).json({ message: "Welcome Jobify" });
});
// swagger
const swagger = fs.readFileSync("./swagger.yaml", "utf8");
const swaggerDocument = YAML.parse(swagger);
app.use("/api/v1/docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));

app.use("/api/v1/jobs", authenticateUser, jobRouter);
app.use("/api/v1/users", authenticateUser, userRouter);
Expand Down
Loading

0 comments on commit 84a76d1

Please sign in to comment.