2025-07-26 20:36:14 -05:00
|
|
|
import AWSUtil from '../bin/aws.js'
|
|
|
|
|
import Post from '../models/postModel.js'
|
2025-07-25 16:33:34 -05:00
|
|
|
|
|
|
|
|
export const getAllPosts = async (req, res, next) => {
|
2025-07-26 20:36:14 -05:00
|
|
|
const allPosts = await Post.find({}).exec()
|
2025-07-30 16:24:09 -05:00
|
|
|
allPosts.map((el) => {
|
|
|
|
|
el.photo = process.env.CLOUDFRONT_URL + el.photo
|
|
|
|
|
})
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
|
|
|
|
data: allPosts
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getAllPostsByStatus = async (req, res, next) => {
|
|
|
|
|
console.log(req.params.status)
|
|
|
|
|
const allPosts = await Post.find({ status: req.params.status }).exec()
|
|
|
|
|
allPosts.map((el) => {
|
|
|
|
|
el.photo = process.env.CLOUDFRONT_URL + el.photo
|
|
|
|
|
})
|
2025-07-25 16:33:34 -05:00
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
|
|
|
|
data: allPosts
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getPost = async (req, res, next) => {
|
2025-07-26 20:36:14 -05:00
|
|
|
const post = await Post.findById(req.params.id).exec()
|
2025-07-25 16:33:34 -05:00
|
|
|
if (!post) {
|
2025-07-26 20:36:14 -05:00
|
|
|
return next('No document found with that id', 404)
|
2025-07-25 16:33:34 -05:00
|
|
|
}
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
|
|
|
|
data: post
|
2025-07-26 20:36:14 -05:00
|
|
|
})
|
2025-07-25 16:33:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const getAllPostsByUser = async (req, res, next) => {
|
2025-08-06 13:36:47 -05:00
|
|
|
console.log(req.params)
|
2025-08-06 23:24:43 -05:00
|
|
|
const posts = await Post.find({ clerkUserID: req.params.id }).exec()
|
2025-08-06 13:36:47 -05:00
|
|
|
posts.map((el) => {
|
|
|
|
|
el.photo = process.env.CLOUDFRONT_URL + el.photo
|
|
|
|
|
})
|
|
|
|
|
if (!posts) {
|
2025-07-26 20:36:14 -05:00
|
|
|
return next('No document found with that id', 404)
|
2025-07-25 16:33:34 -05:00
|
|
|
}
|
|
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
2025-08-06 13:36:47 -05:00
|
|
|
data: posts
|
2025-07-26 20:36:14 -05:00
|
|
|
})
|
2025-07-25 16:33:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const createPost = async (req, res, next) => {
|
2025-07-26 20:36:14 -05:00
|
|
|
const aws = new AWSUtil()
|
2025-07-26 13:06:35 -05:00
|
|
|
|
|
|
|
|
// Grab base64 photo from the req body
|
2025-07-30 16:24:09 -05:00
|
|
|
const fileName = await aws.uploadFile(req.body.photo)
|
2025-07-26 13:06:35 -05:00
|
|
|
const payload = {
|
|
|
|
|
...req.body,
|
2025-07-30 16:24:09 -05:00
|
|
|
photo: fileName
|
2025-07-26 13:06:35 -05:00
|
|
|
}
|
|
|
|
|
|
2025-07-30 16:24:09 -05:00
|
|
|
console.log(payload)
|
|
|
|
|
|
2025-07-26 20:36:14 -05:00
|
|
|
Post.create(payload).then((result) => {
|
2025-07-25 16:33:34 -05:00
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
|
|
|
|
data: result
|
2025-07-26 20:36:14 -05:00
|
|
|
})
|
|
|
|
|
})
|
2025-07-25 16:33:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const updatePost = async (req, res, next) => {
|
2025-07-26 20:36:14 -05:00
|
|
|
Post.updateOne({ _id: req.params.id }, { $set: req.body }).then((result) => {
|
2025-07-25 16:33:34 -05:00
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
|
|
|
|
data: result
|
2025-07-26 20:36:14 -05:00
|
|
|
})
|
|
|
|
|
})
|
2025-07-25 16:33:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const deletePost = async (req, res, next) => {
|
2025-07-26 20:36:14 -05:00
|
|
|
Post.deleteOne({ _id: req.params.id }).then((result) => {
|
2025-07-25 16:33:34 -05:00
|
|
|
res.status(200).json({
|
|
|
|
|
status: 'success',
|
|
|
|
|
data: result
|
2025-07-26 20:36:14 -05:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|