Skip to content

Commit

Permalink
WIP: Connected Routes to the App.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ChuksJoshuaa committed Oct 31, 2022
1 parent c0b33be commit d99b0ad
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import errorHandlerMiddleware from "./middleware/error-handler.js";

//Routes
import userRoutes from "./routes/User.js";
import blogRoutes from "./routes/Blog.js";

const app = express();

Expand All @@ -33,8 +34,12 @@ app.get("/", (req, res) => {
res.send("Blog Api working perfectly");
});

//Authentication Route
app.use("/user", userRoutes);

//Blog routes
app.use("/blog", blogRoutes);

//Error routes
app.use(notFound);
app.use(errorHandlerMiddleware);
Expand Down
52 changes: 52 additions & 0 deletions controllers/Blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import Blog from "../models/Blog.js";
import StatusCodes from "http-status-codes";
import Error from "../errors/index.js";

//Get all published blogs
export const getAllPublishedBlogs = async (req, res) => {
try {
const publishedBlog = await Blog.find({})
.where("state")
.equals("published");
res.status(StatusCodes.OK).json({ data: publishedBlog });
} catch (error) {
res.status(StatusCodes.BAD_REQUEST).json({ msg: "No published blogs" });
}
};

//Get all draft blogs
export const getAllDraftBlogs = async (req, res) => {
try {
const draftBlog = await Blog.find({}).where("state").equals("draft");
res.status(StatusCodes.OK).json({ data: draftBlog });
} catch (error) {
res
.status(StatusCodes.BAD_REQUEST)
.json({ msg: "No draft blogs available yet" });
}
};

// create blog
export const createBlog = async (req, res) => {
const blog = req.body;

const { title, description, body } = blog;

try {
if (title === "" || description === "" || body === "") {
res.status(StatusCodes.BAD_REQUEST).json({ msg: "Incomplete fields" });
} else {
const newBlog = new Blog({
...blog,
author: req.userId,
createdAt: new Date().toISOString(),
});
await newBlog.save();
res.status(StatusCodes.CREATED).json({ newBlog });
}
} catch (error) {
res.status(StatusCodes.CONFLICT).json({ msg: error.msg });
}
};

// Update blog from draft state to published
5 changes: 4 additions & 1 deletion models/Blog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ const BlogSchema = new mongoose.Schema(
type: [String],
default: [],
},
reading_time: String,
reading_time: {
type: String,
default: "5",
},
tags: [String],
body: {
type: String,
Expand Down
14 changes: 14 additions & 0 deletions routes/Blog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from "express";
import {
getAllPublishedBlogs,
getAllDraftBlogs,
createBlog,
} from "../controllers/Blog.js";

const router = express();

router.get("/published", getAllPublishedBlogs);
router.get("/draft", getAllDraftBlogs);
router.post("/create", createBlog);

export default router;

0 comments on commit d99b0ad

Please sign in to comment.