add Blog Routes and controllers
This commit is contained in:
parent
a65e35aef6
commit
76166c1995
4
app.js
4
app.js
@ -161,6 +161,8 @@ import SeoRoute from "./resources/SEO&Analytics/SEORouter.js";
|
|||||||
|
|
||||||
//Affiliate Routes
|
//Affiliate Routes
|
||||||
import AffiliateRoute from "./resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js";
|
import AffiliateRoute from "./resources/Affiliate&Coupon/Affiliate/AffiliateRoute.js";
|
||||||
|
//Blog Routes
|
||||||
|
import BlogRoute from "./resources/Blog/BlogRoute.js";
|
||||||
//Coupon Routes
|
//Coupon Routes
|
||||||
import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js";
|
import CouponRoute from "./resources/Affiliate&Coupon/Coupon/CouponRoute.js";
|
||||||
//short urls
|
//short urls
|
||||||
@ -218,6 +220,8 @@ app.use("/api/v1/affiliate", AffiliateRoute);
|
|||||||
|
|
||||||
//Coupons
|
//Coupons
|
||||||
app.use("/api/v1/coupon", CouponRoute);
|
app.use("/api/v1/coupon", CouponRoute);
|
||||||
|
//Blog
|
||||||
|
app.use("/api/v1/blog", BlogRoute);
|
||||||
//config specialty
|
//config specialty
|
||||||
// app.use("/api/config/specialty", SpecialtiesRouter);
|
// app.use("/api/config/specialty", SpecialtiesRouter);
|
||||||
//specialties
|
//specialties
|
||||||
|
63
resources/Blog/BlogController.js
Normal file
63
resources/Blog/BlogController.js
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import Blog from "./BlogModel.js";
|
||||||
|
|
||||||
|
export const createBlog = async (req, res) => {
|
||||||
|
const { title, tags, image, blog_content } = req.body;
|
||||||
|
console.log(req.body);
|
||||||
|
|
||||||
|
// Checking Fields
|
||||||
|
if (!title || !tags || !image || !blog_content) {
|
||||||
|
return res.status(400).json({
|
||||||
|
success: false,
|
||||||
|
message: "All fields are mandatory",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
let images = [];
|
||||||
|
let Allfiles = req.files.image;
|
||||||
|
console.log(Allfiles);
|
||||||
|
// if (!Array.isArray(Allfiles)) {
|
||||||
|
// Allfiles = [Allfiles]; // Convert to array if it's not already
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Allfiles.forEach((file) => {
|
||||||
|
// if (typeof file.tempFilePath === "string") {
|
||||||
|
// let filepath = file.tempFilePath;
|
||||||
|
// images.push(filepath);
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
|
||||||
|
// const newBlog = await Blog.create({
|
||||||
|
// title,
|
||||||
|
// tags,
|
||||||
|
// image: images, // Assign the array of image file paths
|
||||||
|
// blog_content,
|
||||||
|
// });
|
||||||
|
|
||||||
|
res.status(201).json({
|
||||||
|
success: true,
|
||||||
|
message: "Blog created successfully",
|
||||||
|
data: images,
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error creating blog:", error);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: error.message ? error.message : "Internal server error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const getAllBlog = async (req, res) => {
|
||||||
|
try {
|
||||||
|
const saveData = await Blog.find();
|
||||||
|
res.status(200).json({
|
||||||
|
success: true,
|
||||||
|
message: saveData,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
message: "Internal server error",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
28
resources/Blog/BlogModel.js
Normal file
28
resources/Blog/BlogModel.js
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import mongoose from "mongoose";
|
||||||
|
|
||||||
|
const { Schema, model } = mongoose;
|
||||||
|
|
||||||
|
const blogSchema = new Schema(
|
||||||
|
{
|
||||||
|
title: {
|
||||||
|
type: String,
|
||||||
|
required: [true, "Title is required"],
|
||||||
|
},
|
||||||
|
tags: {
|
||||||
|
type: [String],
|
||||||
|
required: [true, "Tags are required"],
|
||||||
|
},
|
||||||
|
image: {
|
||||||
|
type: String,
|
||||||
|
},
|
||||||
|
blog_content: {
|
||||||
|
type: Object,
|
||||||
|
required: [true, "Content is required"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ timestamps: true }
|
||||||
|
);
|
||||||
|
|
||||||
|
const Blog = model("Blog", blogSchema);
|
||||||
|
|
||||||
|
export default Blog;
|
11
resources/Blog/BlogRoute.js
Normal file
11
resources/Blog/BlogRoute.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import express from "express";
|
||||||
|
|
||||||
|
import { createBlog, getAllBlog } from "./BlogController.js";
|
||||||
|
import { isAuthenticatedUser, authorizeRoles } from "../../middlewares/auth.js";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
router.post("/create", createBlog);
|
||||||
|
router.get("/getallblog", getAllBlog);
|
||||||
|
|
||||||
|
export default router;
|
5
resources/Blog/dummy.json
Normal file
5
resources/Blog/dummy.json
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[
|
||||||
|
{
|
||||||
|
"title": "String0"
|
||||||
|
}
|
||||||
|
]
|
Loading…
Reference in New Issue
Block a user