42 lines
No EOL
1.3 KiB
JavaScript
42 lines
No EOL
1.3 KiB
JavaScript
import *as dotenv from "dotenv"
|
|
import { v2 as cloudinary} from "cloudinary"
|
|
import Post from "../models/Post.js";
|
|
import {createError} from "../error.js"
|
|
|
|
dotenv.config();
|
|
|
|
cloudinary.config({
|
|
cloud_name: process.env.CLOUD_NAME,
|
|
api_key: process.env.CLOUD_API_KEY,
|
|
api_secret: process.env.CLOUD_SECRET
|
|
});
|
|
|
|
export const getAllPosts = async(req, res, next) => {
|
|
try {
|
|
const posts = await Post.find({});
|
|
return res.status(200).json({success: true, data: posts})
|
|
} catch (error) {
|
|
next(createError(error.status, error?.response?.data?.error?.message || error?.message))
|
|
}
|
|
}
|
|
|
|
export const createPost = async (req, res, next) => {
|
|
try {
|
|
const {name, prompt, photo} = req.body;
|
|
const fixed_photo = photo.replace(/ /g, ""); //because the data that they send back have space so we use following command to remove it
|
|
const photoUrl = await cloudinary.uploader.upload(fixed_photo);
|
|
const newPost = await Post.create(
|
|
{
|
|
name,
|
|
prompt,
|
|
photo: photoUrl?.secure_url,
|
|
}
|
|
)
|
|
return res.status(201).json({success: true, data: newPost})
|
|
|
|
} catch (error) {
|
|
console.log(error)
|
|
//next(createError(error.status, error?.response?.data?.error?.message || error?.message))
|
|
|
|
}
|
|
} |