From 8f1bae028d654a38f30318b921a409b5dd3d9f44 Mon Sep 17 00:00:00 2001 From: "ana.vazquez.orihuela" Date: Sat, 14 Sep 2024 12:59:52 -0600 Subject: [PATCH 1/4] Activities Session 01 --- apps/api/src/main.ts | 2 + apps/api/src/routes/posts.ts | 169 +++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 apps/api/src/routes/posts.ts diff --git a/apps/api/src/main.ts b/apps/api/src/main.ts index 7f6b1e10..9862eaea 100644 --- a/apps/api/src/main.ts +++ b/apps/api/src/main.ts @@ -5,6 +5,7 @@ import helmet from 'helmet'; import { corsOptions } from './config/corsConfig'; 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; @@ -16,6 +17,7 @@ app.use(helmet()); app.use(cors(corsOptions)); app.use('/api/categories', categories); +app.use('/api/posts', posts); app.listen(port, host, () => { console.log(`[ ready ] http://${host}:${port}`); diff --git a/apps/api/src/routes/posts.ts b/apps/api/src/routes/posts.ts new file mode 100644 index 00000000..f3f34ddb --- /dev/null +++ b/apps/api/src/routes/posts.ts @@ -0,0 +1,169 @@ +import express from 'express'; +import { getCategory } from '../routes/categories'; + +const router = express.Router(); +// Initialize posts array to save data in memory +const posts = []; +// Initialize comments array to save data in memory +const comments = []; + +// GET /posts Return an array of all the posts with status code 200 +router.get('/', (req, res) => { + res.status(200).json(posts); +}); + +// POST /posts Create a new post and return the created post with status code 201 +router.post('/', (req, res) => { + // Retrieve the values from the request body + const { title, image, description, category } = req.body; + + if (!title || !image || !description || !category) { + // If it is empty or undefined return a 400 status code with a message + return res.status(404).json({ message: 'Missing body data' }); + } + const newPost = { + id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint + title, + image, + description, + category, + comments: [] + }; + // Add the new post to our array + posts.push(newPost); + + res.status(201).json(newPost); +}); + +export const getPost = (id: string) => { + posts.find((p) => p.id === id); + return posts.find((p) => p.id === id); +}; + +// GET /posts/:id Return a post by id with category object and each comment object in the array with status code 200 +router.get('/:id', (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Check if we have a post with that id + const post = getPost(id); + + if (!post) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + // Note: Remember that json method doesn't interrupt the workflow + // therefore is important to add a "return" to break the process + } + + const currentComment = comments.find((p) => p.id === id); + if (currentComment) { + post.comments = comments; + } + + const currentCategory = getCategory(post.category); + if (currentCategory) { + post.category = currentCategory; + } + + res.status(200).json(post); +}); + +//GET /posts/category/:category Return an array of all the posts by category with status code 200 +router.get('/category/:category', (req, res) => { + // Retrieve the category from the route params + const { category } = req.params; + + if (!category) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Category not found' }); + // Note: Remember that json method doesn't interrupt the workflow + // therefore is important to add a "return" to break the process + } + + const postsByCategory = posts.map((p) => { + if (p.category?.id === category) return p; + }); + + res.status(200).json(postsByCategory); +}); + +// POST /posts/:id/comments Create a comment inside the post and return the comment with status code 201 +router.post('/:id/:comments', (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Retrieve the index of the category in the array + const postIndex = posts.findIndex((p) => p.id === id); + + if (postIndex === -1) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Retrieve the values from the request body + const { author, content } = req.body; + const comment = { + id, + author, + content + }; + // Add the new post to our array + comments.push(comment); + // Return the created comment with a 201 status code + res.status(201).json(comment); +}); + +//PATCH /posts/:id Update post information and return the updated post with status code 200 +router.patch('/:id', (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Retrieve the index of the post in the array + const postIndex = posts.findIndex((p) => p.id === id); + + if (postIndex === -1) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Generate a copy of our post + const updatedPost = { ...posts[postIndex] }; + + // Retrieve the values from the request body + const { title } = req.body; + + // Check if we have a title, if so update the property + if (title) { + updatedPost.title = title; + } + + // Update the post in our array + posts[postIndex] = updatedPost; + + // Return the updated post with a 200 status code + res.status(200).json(updatedPost); +}); + +// DELETE /posts/:id Delete the post and return the deleted post with status code 200 or 204 +router.delete('/:id', (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Retrieve the index of the post in the array + const postIndex = posts.findIndex((p) => p.id === id); + + // "findIndex" will return -1 if there is no match + if (postIndex === -1) { + // If we don't find the category return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Remove the post from the array + posts.splice(postIndex, 1); + + // Retrieve the index of the comment in the array + const commentsIndex = comments.findIndex((p) => p.id === id); + // Remove the comment from the array + comments.splice(commentsIndex, 1); + + // Return a 204 status code + res.status(204).send(); +}); + +export default router; From 550724ca02c1f5e338cb5c4c86520bca5e992f4c Mon Sep 17 00:00:00 2001 From: "ana.vazquez.orihuela" Date: Sat, 14 Sep 2024 14:17:09 -0600 Subject: [PATCH 2/4] Activities Session 02 --- apps/api/src/controllers/posts.ts | 174 ++++++++++++++++++++++++++++++ apps/api/src/routes/posts.ts | 161 ++------------------------- 2 files changed, 184 insertions(+), 151 deletions(-) create mode 100644 apps/api/src/controllers/posts.ts diff --git a/apps/api/src/controllers/posts.ts b/apps/api/src/controllers/posts.ts new file mode 100644 index 00000000..4f3e8922 --- /dev/null +++ b/apps/api/src/controllers/posts.ts @@ -0,0 +1,174 @@ +import { getCategory } from '../controllers/category'; +// Initialize posts array to save data in memory +const posts = []; +// Initialize comments array to save data in memory +const comments = []; + +export const getPost = (id: string) => { + posts.find((p) => p.id === id); + return posts.find((p) => p.id === id); +}; + +// GET /posts Return an array of all the posts with status code 200 +const getPosts = (req, res) => { + res.status(200).json(posts); +}; + +// POST /posts Create a new post and return the created post with status code 201 +const createPosts = (req, res) => { + // Retrieve the values from the request body + const { title, image, description, category } = req.body; + + if (!title || !image || !description || !category) { + // If it is empty or undefined return a 400 status code with a message + return res.status(404).json({ message: 'Missing body data' }); + } + const newPost = { + id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint + title, + image, + description, + category, + comments: [] + }; + // Add the new post to our array + posts.push(newPost); + + res.status(201).json(newPost); +}; + +// GET /posts/:id Return a post by id with category object and each comment object in the array with status code 200 +const getPostById = (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Check if we have a post with that id + const post = getPost(id); + + if (!post) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + // Note: Remember that json method doesn't interrupt the workflow + // therefore is important to add a "return" to break the process + } + + const currentComment = comments.find((p) => p.id === id); + if (currentComment) { + post.comments = comments; + } + + const currentCategory = getCategory(post.category); + if (currentCategory) { + post.category = currentCategory; + } + + res.status(200).json(post); +}; + +//GET /posts/category/:category Return an array of all the posts by category with status code 200 +const getPostsByCategory = (req, res) => { + // Retrieve the category from the route params + const { category } = req.params; + + if (!category) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Category not found' }); + // Note: Remember that json method doesn't interrupt the workflow + // therefore is important to add a "return" to break the process + } + + const postsByCategory = posts.map((p) => { + if (p.category?.id === category) return p; + }); + + res.status(200).json(postsByCategory); +}; + +// POST /posts/:id/comments Create a comment inside the post and return the comment with status code 201 +const createPostComment = (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Retrieve the index of the category in the array + const postIndex = posts.findIndex((p) => p.id === id); + + if (postIndex === -1) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Retrieve the values from the request body + const { author, content } = req.body; + const comment = { + id, + author, + content + }; + // Add the new post to our array + comments.push(comment); + // Return the created comment with a 201 status code + res.status(201).json(comment); +}; + +//PATCH /posts/:id Update post information and return the updated post with status code 200 +const updatePost = (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Retrieve the index of the post in the array + const postIndex = posts.findIndex((p) => p.id === id); + + if (postIndex === -1) { + // If we don't find the post return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Generate a copy of our post + const updatedPost = { ...posts[postIndex] }; + + // Retrieve the values from the request body + const { title } = req.body; + + // Check if we have a title, if so update the property + if (title) { + updatedPost.title = title; + } + + // Update the post in our array + posts[postIndex] = updatedPost; + + // Return the updated post with a 200 status code + res.status(200).json(updatedPost); +}; + +// DELETE /posts/:id Delete the post and return the deleted post with status code 200 or 204 +const deletePost = (req, res) => { + // Retrieve the id from the route params + const { id } = req.params; + // Retrieve the index of the post in the array + const postIndex = posts.findIndex((p) => p.id === id); + + // "findIndex" will return -1 if there is no match + if (postIndex === -1) { + // If we don't find the category return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Remove the post from the array + posts.splice(postIndex, 1); + + // Retrieve the index of the comment in the array + const commentsIndex = comments.findIndex((p) => p.id === id); + // Remove the comment from the array + comments.splice(commentsIndex, 1); + + // Return a 204 status code + res.status(204).send(); +}; + +export default { + getPosts, + createPosts, + getPostById, + getPostsByCategory, + createPostComment, + updatePost, + deletePost +}; diff --git a/apps/api/src/routes/posts.ts b/apps/api/src/routes/posts.ts index f3f34ddb..ecaf597b 100644 --- a/apps/api/src/routes/posts.ts +++ b/apps/api/src/routes/posts.ts @@ -1,169 +1,28 @@ import express from 'express'; -import { getCategory } from '../routes/categories'; + +import postController from '../controllers/posts'; const router = express.Router(); -// Initialize posts array to save data in memory -const posts = []; -// Initialize comments array to save data in memory -const comments = []; // GET /posts Return an array of all the posts with status code 200 -router.get('/', (req, res) => { - res.status(200).json(posts); -}); - -// POST /posts Create a new post and return the created post with status code 201 -router.post('/', (req, res) => { - // Retrieve the values from the request body - const { title, image, description, category } = req.body; - - if (!title || !image || !description || !category) { - // If it is empty or undefined return a 400 status code with a message - return res.status(404).json({ message: 'Missing body data' }); - } - const newPost = { - id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint - title, - image, - description, - category, - comments: [] - }; - // Add the new post to our array - posts.push(newPost); +router.get('/', postController.getPosts); - res.status(201).json(newPost); -}); - -export const getPost = (id: string) => { - posts.find((p) => p.id === id); - return posts.find((p) => p.id === id); -}; +// POST /posts Create a new post and return the created post with status code 201 +router.post('/', postController.createPosts); // GET /posts/:id Return a post by id with category object and each comment object in the array with status code 200 -router.get('/:id', (req, res) => { - // Retrieve the id from the route params - const { id } = req.params; - // Check if we have a post with that id - const post = getPost(id); - - if (!post) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - // Note: Remember that json method doesn't interrupt the workflow - // therefore is important to add a "return" to break the process - } - - const currentComment = comments.find((p) => p.id === id); - if (currentComment) { - post.comments = comments; - } - - const currentCategory = getCategory(post.category); - if (currentCategory) { - post.category = currentCategory; - } - - res.status(200).json(post); -}); +router.get('/:id', postController.getPostById); //GET /posts/category/:category Return an array of all the posts by category with status code 200 -router.get('/category/:category', (req, res) => { - // Retrieve the category from the route params - const { category } = req.params; - - if (!category) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Category not found' }); - // Note: Remember that json method doesn't interrupt the workflow - // therefore is important to add a "return" to break the process - } - - const postsByCategory = posts.map((p) => { - if (p.category?.id === category) return p; - }); - - res.status(200).json(postsByCategory); -}); +router.get('/category/:category', postController.getPostsByCategory); // POST /posts/:id/comments Create a comment inside the post and return the comment with status code 201 -router.post('/:id/:comments', (req, res) => { - // Retrieve the id from the route params - const { id } = req.params; - // Retrieve the index of the category in the array - const postIndex = posts.findIndex((p) => p.id === id); - - if (postIndex === -1) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - } - - // Retrieve the values from the request body - const { author, content } = req.body; - const comment = { - id, - author, - content - }; - // Add the new post to our array - comments.push(comment); - // Return the created comment with a 201 status code - res.status(201).json(comment); -}); +router.post('/:id/:comments', postController.createPostComment); //PATCH /posts/:id Update post information and return the updated post with status code 200 -router.patch('/:id', (req, res) => { - // Retrieve the id from the route params - const { id } = req.params; - // Retrieve the index of the post in the array - const postIndex = posts.findIndex((p) => p.id === id); - - if (postIndex === -1) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - } - - // Generate a copy of our post - const updatedPost = { ...posts[postIndex] }; - - // Retrieve the values from the request body - const { title } = req.body; - - // Check if we have a title, if so update the property - if (title) { - updatedPost.title = title; - } - - // Update the post in our array - posts[postIndex] = updatedPost; - - // Return the updated post with a 200 status code - res.status(200).json(updatedPost); -}); +router.patch('/:id', postController.updatePost); // DELETE /posts/:id Delete the post and return the deleted post with status code 200 or 204 -router.delete('/:id', (req, res) => { - // Retrieve the id from the route params - const { id } = req.params; - // Retrieve the index of the post in the array - const postIndex = posts.findIndex((p) => p.id === id); - - // "findIndex" will return -1 if there is no match - if (postIndex === -1) { - // If we don't find the category return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - } - - // Remove the post from the array - posts.splice(postIndex, 1); - - // Retrieve the index of the comment in the array - const commentsIndex = comments.findIndex((p) => p.id === id); - // Remove the comment from the array - comments.splice(commentsIndex, 1); - - // Return a 204 status code - res.status(204).send(); -}); +router.delete('/:id', postController.deletePost); export default router; From ee1b9867b139067d6e1da472cc56cb81f82395e9 Mon Sep 17 00:00:00 2001 From: "ana.vazquez.orihuela" Date: Mon, 16 Sep 2024 15:10:42 -0600 Subject: [PATCH 3/4] Activities Session 05 --- apps/api/.env | 2 +- apps/api/src/controllers/posts.ts | 191 ++++++++++-------------------- apps/api/src/models/comment.ts | 30 +++++ apps/api/src/models/post.ts | 39 ++++++ 4 files changed, 134 insertions(+), 128 deletions(-) create mode 100644 apps/api/src/models/comment.ts create mode 100644 apps/api/src/models/post.ts diff --git a/apps/api/.env b/apps/api/.env index ecaf5a7c..d24f4024 100644 --- a/apps/api/.env +++ b/apps/api/.env @@ -1,3 +1,3 @@ ACCESS_TOKEN_SECRET=[YOUR_ACCESS_TOKEN_SECRET_HERE] REFRESH_TOKEN_SECRET=[YOUR_REFRESH_TOKEN_SECRET_HERE] -MONGO_URL=[YOUR_MONGO_CONNECTION_STRING_HERE] \ No newline at end of file +MONGO_URL=mongodb+srv://anna:AxIiq4HfpFxNTnbL@cluster0.gap9e.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 \ No newline at end of file diff --git a/apps/api/src/controllers/posts.ts b/apps/api/src/controllers/posts.ts index 4f3e8922..1c728490 100644 --- a/apps/api/src/controllers/posts.ts +++ b/apps/api/src/controllers/posts.ts @@ -1,166 +1,103 @@ -import { getCategory } from '../controllers/category'; -// Initialize posts array to save data in memory -const posts = []; -// Initialize comments array to save data in memory -const comments = []; - -export const getPost = (id: string) => { - posts.find((p) => p.id === id); - return posts.find((p) => p.id === id); -}; +import Post from '../models/post'; +import Comment from '../models/comment'; // GET /posts Return an array of all the posts with status code 200 -const getPosts = (req, res) => { - res.status(200).json(posts); +const getPosts = async (req, res, next) => { + try { + const posts = await Post.find(); + res.status(200).json(posts); + } catch (error) { + //handling error middleware - errorHandler + next(error); + } }; // POST /posts Create a new post and return the created post with status code 201 -const createPosts = (req, res) => { +const createPosts = async (req, res, next) => { // Retrieve the values from the request body - const { title, image, description, category } = req.body; - - if (!title || !image || !description || !category) { - // If it is empty or undefined return a 400 status code with a message - return res.status(404).json({ message: 'Missing body data' }); + try { + const post = await Post.create(req.body); + res.status(201).json(post); + } catch (error) { + next(error); } - const newPost = { - id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint - title, - image, - description, - category, - comments: [] - }; - // Add the new post to our array - posts.push(newPost); - - res.status(201).json(newPost); }; // GET /posts/:id Return a post by id with category object and each comment object in the array with status code 200 -const getPostById = (req, res) => { - // Retrieve the id from the route params +const getPostById = async (req, res, next) => { const { id } = req.params; - // Check if we have a post with that id - const post = getPost(id); - - if (!post) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - // Note: Remember that json method doesn't interrupt the workflow - // therefore is important to add a "return" to break the process - } + try { + const post = await Post.findById(id).populate('comments').populate('category'); - const currentComment = comments.find((p) => p.id === id); - if (currentComment) { - post.comments = comments; + return res.status(200).json(post); + } catch (error) { + //handling error middleware - errorHandler + next(error); } - - const currentCategory = getCategory(post.category); - if (currentCategory) { - post.category = currentCategory; - } - - res.status(200).json(post); }; //GET /posts/category/:category Return an array of all the posts by category with status code 200 -const getPostsByCategory = (req, res) => { - // Retrieve the category from the route params +const getPostsByCategory = async (req, res, next) => { const { category } = req.params; + try { + const posts = (await Post.findOne({ category })) ?? []; - if (!category) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Category not found' }); - // Note: Remember that json method doesn't interrupt the workflow - // therefore is important to add a "return" to break the process + res.status(200).json(posts); + } catch (error) { + //handling error middleware - errorHandler + next(error); } - - const postsByCategory = posts.map((p) => { - if (p.category?.id === category) return p; - }); - - res.status(200).json(postsByCategory); }; // POST /posts/:id/comments Create a comment inside the post and return the comment with status code 201 -const createPostComment = (req, res) => { - // Retrieve the id from the route params +const createPostComment = async (req, res, next) => { const { id } = req.params; - // Retrieve the index of the category in the array - const postIndex = posts.findIndex((p) => p.id === id); - - if (postIndex === -1) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); + try { + const comments = await Comment.create({ + author: req.body.author, + content: req.body.content + }); + + //populate function on getPostById need the reference id from the comments + const posts = await Post.findById(id); + await Post.findByIdAndUpdate(id, { comments: [...posts.comments, comments._id] }); + + // Return all the posts with that category with a 200 status code + res.status(201).json(comments); + } catch (error) { + //handling error middleware - errorHandler + next(error); } - - // Retrieve the values from the request body - const { author, content } = req.body; - const comment = { - id, - author, - content - }; - // Add the new post to our array - comments.push(comment); - // Return the created comment with a 201 status code - res.status(201).json(comment); }; //PATCH /posts/:id Update post information and return the updated post with status code 200 -const updatePost = (req, res) => { +const updatePost = async (req, res, next) => { // Retrieve the id from the route params const { id } = req.params; - // Retrieve the index of the post in the array - const postIndex = posts.findIndex((p) => p.id === id); - - if (postIndex === -1) { - // If we don't find the post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - } - - // Generate a copy of our post - const updatedPost = { ...posts[postIndex] }; - - // Retrieve the values from the request body - const { title } = req.body; - - // Check if we have a title, if so update the property - if (title) { - updatedPost.title = title; + try { + const post = await Post.findByIdAndUpdate(id, req.body, { new: true }); + res.status(200).json(post); + } catch (error) { + //handling error middleware - errorHandler + next(error); } - - // Update the post in our array - posts[postIndex] = updatedPost; - - // Return the updated post with a 200 status code - res.status(200).json(updatedPost); }; // DELETE /posts/:id Delete the post and return the deleted post with status code 200 or 204 -const deletePost = (req, res) => { +const deletePost = async (req, res, next) => { // Retrieve the id from the route params const { id } = req.params; - // Retrieve the index of the post in the array - const postIndex = posts.findIndex((p) => p.id === id); - - // "findIndex" will return -1 if there is no match - if (postIndex === -1) { - // If we don't find the category return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); + try { + //Remove post comments from database when you delete the post + const posts = await Post.findById(id); + await Comment.deleteMany({ _id: { $in: posts.comments } }); + //Remove post + const post = await Post.findByIdAndDelete(id); + res.status(204).json(post); + } catch (error) { + //handling error middleware - errorHandler + next(error); } - - // Remove the post from the array - posts.splice(postIndex, 1); - - // Retrieve the index of the comment in the array - const commentsIndex = comments.findIndex((p) => p.id === id); - // Remove the comment from the array - comments.splice(commentsIndex, 1); - - // Return a 204 status code - res.status(204).send(); }; export default { diff --git a/apps/api/src/models/comment.ts b/apps/api/src/models/comment.ts new file mode 100644 index 00000000..5bb38b13 --- /dev/null +++ b/apps/api/src/models/comment.ts @@ -0,0 +1,30 @@ +import mongoose, { Document, Schema } from 'mongoose'; + +interface IComment extends Document { + author: string; + content: string; + // postId: mongoose.Schema.Types.ObjectId; +} + +export const commentSchema = new Schema( + { + author: { + type: String, + required: [true, 'Property is required'] + }, + content: { + type: String, + required: [true, 'Property is required'] + } + // postId: { + // type: mongoose.Schema.Types.ObjectId, + // ref: 'Post' + // } + }, + { + timestamps: true + } +); + +const Comment = mongoose.model('Comment', commentSchema); +export default Comment; diff --git a/apps/api/src/models/post.ts b/apps/api/src/models/post.ts new file mode 100644 index 00000000..cb16a75a --- /dev/null +++ b/apps/api/src/models/post.ts @@ -0,0 +1,39 @@ +import mongoose, { Document, Schema } from 'mongoose'; + +interface IPost extends Document { + title: string; + image: string; + description: string; + category: string; + comments: [{ type: Schema.Types.ObjectId }]; +} + +export const postSchema = new Schema( + { + title: { + type: String, + required: [true, 'Property is required'] + }, + image: { + type: String, + required: [true, 'Property is required'] + }, + description: { + type: String, + required: [true, 'Property is required'] + }, + category: { + type: String, + required: [true, 'Property is required'], + ref: 'Category' + }, + comments: [{ type: Schema.Types.ObjectId, ref: 'Comment' }] + }, + { + timestamps: true + } +); + +const Post = mongoose.model('Post', postSchema); + +export default Post; From 8265cbf57ad40a93490d81461523ddc2d6efd957 Mon Sep 17 00:00:00 2001 From: "ana.vazquez.orihuela" Date: Tue, 17 Sep 2024 11:49:22 -0600 Subject: [PATCH 4/4] fix: removing sensitive information & updating type of category from post model --- apps/api/.env | 2 +- apps/api/src/models/post.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/api/.env b/apps/api/.env index d24f4024..ecaf5a7c 100644 --- a/apps/api/.env +++ b/apps/api/.env @@ -1,3 +1,3 @@ ACCESS_TOKEN_SECRET=[YOUR_ACCESS_TOKEN_SECRET_HERE] REFRESH_TOKEN_SECRET=[YOUR_REFRESH_TOKEN_SECRET_HERE] -MONGO_URL=mongodb+srv://anna:AxIiq4HfpFxNTnbL@cluster0.gap9e.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 \ No newline at end of file +MONGO_URL=[YOUR_MONGO_CONNECTION_STRING_HERE] \ No newline at end of file diff --git a/apps/api/src/models/post.ts b/apps/api/src/models/post.ts index cb16a75a..61bec323 100644 --- a/apps/api/src/models/post.ts +++ b/apps/api/src/models/post.ts @@ -4,7 +4,7 @@ interface IPost extends Document { title: string; image: string; description: string; - category: string; + category: mongoose.Schema.Types.ObjectId; comments: [{ type: Schema.Types.ObjectId }]; } @@ -23,7 +23,7 @@ export const postSchema = new Schema( required: [true, 'Property is required'] }, category: { - type: String, + type: mongoose.Schema.Types.ObjectId, required: [true, 'Property is required'], ref: 'Category' },