diff --git a/apps/api/src/controllers/post.ts b/apps/api/src/controllers/post.ts new file mode 100644 index 00000000..13a1f129 --- /dev/null +++ b/apps/api/src/controllers/post.ts @@ -0,0 +1,160 @@ +import Post from '../models/post'; +import Comment from '../models/comment'; + +//get all posts +const getPosts = async (req, res) => { + try { + const posts = await Post.find(); + res.status(200).json(posts); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } +} + +// Get post by id +const getPostById = async (req, res) => { + const { id } = req.params; + + try { + // const post = await Post.findById(id).populate({ + // path: "Comment", + // options: {strictPopulate: false}, + // match: { post_id: id }, + // }); + const post = (await Post.findById(id).populate('comments')); + + if (!post) { + return res.status(404).json({ message: 'Ups, Post not found' }); + } + // Return the post with a 200 status code + res.status(200).json(post); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } +} + +// Get posts by category +const getPostsByCategory = async (req, res) => { + const { category } = req.params; + // const postsByCategory = posts.filter((post) => post.category == category ); + + try { + const posts = await Post.find({category: category}); + console.log(posts) + res.status(200).json(posts); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } +} + +//create post +const createPost = async (req, res) => { + // Retrieve the name from the request body + const { title, image, description, category } = req.body; + + /*if (!title || !image || !description || !category) { + // If body values are empty or undefined return a 400 status code with a message + return res.status(400).json({ message: 'all fields are required.' }); + }*/ + + // Generate a new post + const newPost = { + // id: Date.now().toString(), + title: title, + image: image, + description: description, + category: category, + comments: [] + }; + + try { + const post = await Post.create(newPost); + // Return the created post with a 201 status code + res.status(201).json(post); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } + +}; + + +//create post comment +const createComment = async (req, res) => { + const { id } = req.params; + const { author, content} = req.body; + try { + const newComment = { + author: author, + content: content, + post_id: id, + } + const comment = await Comment.create( + newComment + ); + const posts = await Post.findById(id); + await Post.findByIdAndUpdate(id, {comments:[...posts.comments, comment._id]}); + // Return the created comment with a 201 status code + res.status(201).json(comment); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } +} + +//update post +const updatePost = async (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + + try { + const post = await Post.findByIdAndUpdate(id, req.body, {new: true}) + + if (!post) { + return res.status(404).json({ message: 'post not found' }); + } + + // Return the updated post with a 200 status code + res.status(200).json(post); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } +}; + +//delete post +const deletePost = async (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + + try { + const post = await Post.findByIdAndDelete(id); + // If we don't find the post return a 404 status code with a message + if (!post) { + return res.status(404).json({ message: 'Post not found' }); + } + // Return a 200 status code + res.status(200).json(post); + } catch (error) { + const { message } = error; + res.status(500).json({ message }); + } + + +}; + +export default { + getPosts, + getPostById, + getPostsByCategory, + createPost, + createComment, + deletePost, + updatePost +} + + + diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index daf9dc2e..96888e1e 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -8,6 +8,7 @@ import { verifyToken } from './middleware/auth'; import { errorHandler } from './middleware/errorHandler'; import auth from './routes/auth'; import categories from './routes/categories'; +import posts from './routes/posts'; const host = process.env.HOST ?? 'localhost'; const port = process.env.PORT ? Number(process.env.PORT) : 3000; @@ -22,6 +23,7 @@ app.use('/api/auth', auth); app.use(verifyToken); app.use('/api/categories', categories); +app.use('/api/posts', posts); app.use(errorHandler); diff --git a/apps/api/src/models/comment.ts b/apps/api/src/models/comment.ts new file mode 100644 index 00000000..3ca3e347 --- /dev/null +++ b/apps/api/src/models/comment.ts @@ -0,0 +1,25 @@ +import mongoose, { Schema } from 'mongoose'; + +export const commentSchema = new Schema( + { + author: { + type: String, + required: [true, 'Property is required'] + }, + content: { + type: String, + required: [true, 'Property is required'] + }, + post_id: { + type: mongoose.Types.ObjectId, required: true, ref: 'Post' + } + }, + { + timestamps: true + } +); + +const Comment = mongoose.model('Comment', commentSchema); + + +export default Comment; \ No newline at end of file diff --git a/apps/api/src/models/post.ts b/apps/api/src/models/post.ts new file mode 100644 index 00000000..087a25e8 --- /dev/null +++ b/apps/api/src/models/post.ts @@ -0,0 +1,27 @@ +import mongoose, { Schema } from 'mongoose'; + +export const postSchema = new Schema ( + { + + title: { + type: String, + required: [true, 'Property is required'] + }, + image: { + type: String + }, + description: { + type: String + }, + category: { + type: mongoose.Types.ObjectId, required: true, ref: 'Category' + }, + comments: { + type: [{ type: Schema.Types.ObjectId, ref: 'Comment' }] + } + } +); + +const Post = mongoose.model('Post', postSchema); + +export default Post \ No newline at end of file diff --git a/apps/api/src/routes/posts.ts b/apps/api/src/routes/posts.ts new file mode 100644 index 00000000..f6307894 --- /dev/null +++ b/apps/api/src/routes/posts.ts @@ -0,0 +1,28 @@ +import express from 'express'; +import postController from '../controllers/post' + +const router = express.Router(); + +// get all posts +router.get('/', postController.getPosts ); + +// Get post by id +router.get('/:id', postController.getPostById); + +// Get posts by category +router.get('/category/:category', postController.getPostsByCategory ); + +//create post +router.post('/', postController.createPost ); + +//create post comment +router.post('/:id/comments', postController.createComment ); + +//delete post +router.delete('/:id', postController.deletePost); + +//update post +router.patch('/:id', postController.updatePost ); + + +export default router; \ No newline at end of file