From c69f09217b163b2567d746f685c6b535806ce8cf Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Wed, 14 Aug 2024 22:26:43 -0600 Subject: [PATCH 1/7] servicios de posts creados --- apps/api/src/main.ts | 2 + apps/api/src/routes/posts.ts | 150 +++++++++++++++++++++++++++++++++++ 2 files changed, 152 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..907d5d12 --- /dev/null +++ b/apps/api/src/routes/posts.ts @@ -0,0 +1,150 @@ +import express from 'express'; + +export const getPost = (id: string) => { + return posts.find((p) => p.id === id); +}; + +export const getPostbyCategory = (category: string) => { + return posts.find((p) => p.category === category); +}; + +const router = express.Router(); + +const posts = []; + +// Get all posts +router.get('/', (req, res) => { + // Return all the posts with a 200 status code + res.status(200).json(posts); +}); + +// Get post by category +router.get('/category/:category', (req, res) => { + const { category } = req.params; + + const post = getPostbyCategory(category); + + if (!post) { + return res.status(404).json({ message: 'Post not found by category' }); + } + + res.status(200).json(post); +}); + +// Get post by id +router.get('/:id', (req, res) => { + const { id } = req.params; + + const post = getPost(id); + + if (!post) { + return res.status(404).json({ message: 'Post not found' }); + } + + res.status(200).json(post); +}); + +// Create post +router.post('/', (req, res) => { + const { title, image, description, category } = req.body; + + if (!title) { + return res.status(400).json({ message: 'The title is required.' }); + } + if (!image) { + return res.status(400).json({ message: 'The image is required.' }); + } + if (!description) { + return res.status(400).json({ message: 'The description is required.' }); + } + if (!category) { + return res.status(400).json({ message: 'The category is required.' }); + } + + // Generate a new post + const newPosts = { + id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint + title, + image, + description, + category + }; + // Add the new post to our array + posts.push(newPosts); + + // Return the created post with a 201 status code + res.status(201).json(newPosts); +}); + +// Create comments +router.post('/:id/comments', (req, res) => { + const { id } = req.params + const { author, content } = req.body; + + if (!author) { + return res.status(400).json({ message: 'The author is required.' }); + } + if (!content) { + return res.status(400).json({ message: 'The content is required.' }); + } + + // Generate a new comment + const addComment = { + id, + author, + content + }; + // Add the new comment to our array + posts.push(addComment); + + // Return the created comment with a 201 status code + res.status(201).json(addComment); +}); + +// Update posts by id +router.patch('/:id', (req, res) => { + const { id } = req.params + + const postbyId = posts.findIndex((p) => p.id === id); + + if (postbyId === -1) { + // If we don't find the posts return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Generate a copy of our posts + const updatedPost = { ...posts[postbyId] }; + const { title } = req.body; + + if (title) { + updatedPost.title = title; + } + + /// Update the posts in our array + posts[postbyId] = updatedPost; + + // Return the updated post with a 201 status code + res.status(201).json(updatedPost); +}); + +// Delete post by id +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 post 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); + + // Return a 204 status code + res.status(204).send(); +}); + +export default router; From 05b795a1186538cabfb436c5c1db1a213cafa35b Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Mon, 19 Aug 2024 16:39:35 -0600 Subject: [PATCH 2/7] post controller --- apps/api/src/controllers/post.ts | 154 +++++++++++++++++++++++++++++++ apps/api/src/routes/posts.ts | 138 ++------------------------- 2 files changed, 162 insertions(+), 130 deletions(-) create mode 100644 apps/api/src/controllers/post.ts diff --git a/apps/api/src/controllers/post.ts b/apps/api/src/controllers/post.ts new file mode 100644 index 00000000..d9d5e7fa --- /dev/null +++ b/apps/api/src/controllers/post.ts @@ -0,0 +1,154 @@ +const posts = []; + +export const getByPost = (id: string) => { + return posts.find((p) => p.id === id); +}; + +export const getPostbyCategory = (category: string) => { + return posts.find((p) => p.category === category); +}; + +// Get all posts +const getAllPosts = (req, res) => { + // Return all the posts with a 200 status code + res.status(200).json(posts); +}; + +// Get post by category +const getPostByCategory = (req, res) => { + const { category } = req.params; + + const post = getPostbyCategory(category); + + if (!post) { + return res.status(404).json({ message: 'Post not found by category' }); + } + + res.status(200).json(post); +}; + +// Get post by id +const getPostById = (req, res) => { + const { id } = req.params; + + const post = getByPost(id); + + if (!post) { + return res.status(404).json({ message: 'Post not found' }); + } + + res.status(200).json(post); +}; + +// Create post +const createPost = (req, res) => { + const { title, image, description, category } = req.body; + + if (!title) { + return res.status(400).json({ message: 'The title is required.' }); + } + if (!image) { + return res.status(400).json({ message: 'The image is required.' }); + } + if (!description) { + return res.status(400).json({ message: 'The description is required.' }); + } + if (!category) { + return res.status(400).json({ message: 'The category is required.' }); + } + + // Generate a new post + const newPosts = { + id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint + title, + image, + description, + category + }; + // Add the new post to our array + posts.push(newPosts); + + // Return the created post with a 201 status code + res.status(201).json(newPosts); +}; + +// Create comments +const createComments = (req, res) => { + const { id } = req.params + const { author, content } = req.body; + + if (!author) { + return res.status(400).json({ message: 'The author is required.' }); + } + if (!content) { + return res.status(400).json({ message: 'The content is required.' }); + } + + // Generate a new comment + const addComment = { + id, + author, + content + }; + // Add the new comment to our array + posts.push(addComment); + + // Return the created comment with a 201 status code + res.status(201).json(addComment); +}; + +// Update posts by id +const updatePostById = (req, res) => { + const { id } = req.params + + const postbyId = posts.findIndex((p) => p.id === id); + + if (postbyId === -1) { + // If we don't find the posts return a 404 status code with a message + return res.status(404).json({ message: 'Post not found' }); + } + + // Generate a copy of our posts + const updatedPost = { ...posts[postbyId] }; + const { title } = req.body; + + if (title) { + updatedPost.title = title; + } + + /// Update the posts in our array + posts[postbyId] = updatedPost; + + // Return the updated post with a 201 status code + res.status(201).json(updatedPost); +}; + +// Delete post by id +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 post 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); + + // Return a 204 status code + res.status(204).send(); +}; + +export default { + getAllPosts, + getPostByCategory, + getPostById, + createPost, + createComments, + updatePostById, + deletePost +}; diff --git a/apps/api/src/routes/posts.ts b/apps/api/src/routes/posts.ts index 907d5d12..f0876617 100644 --- a/apps/api/src/routes/posts.ts +++ b/apps/api/src/routes/posts.ts @@ -1,150 +1,28 @@ import express from 'express'; -export const getPost = (id: string) => { - return posts.find((p) => p.id === id); -}; - -export const getPostbyCategory = (category: string) => { - return posts.find((p) => p.category === category); -}; +import postController from '../controllers/post'; const router = express.Router(); -const posts = []; - // Get all posts -router.get('/', (req, res) => { - // Return all the posts with a 200 status code - res.status(200).json(posts); -}); +router.get('/', postController.getAllPosts); // Get post by category -router.get('/category/:category', (req, res) => { - const { category } = req.params; - - const post = getPostbyCategory(category); - - if (!post) { - return res.status(404).json({ message: 'Post not found by category' }); - } - - res.status(200).json(post); -}); +router.get('/category/:category', postController.getPostByCategory); // Get post by id -router.get('/:id', (req, res) => { - const { id } = req.params; - - const post = getPost(id); - - if (!post) { - return res.status(404).json({ message: 'Post not found' }); - } - - res.status(200).json(post); -}); +router.get('/:id', postController.getPostById); // Create post -router.post('/', (req, res) => { - const { title, image, description, category } = req.body; - - if (!title) { - return res.status(400).json({ message: 'The title is required.' }); - } - if (!image) { - return res.status(400).json({ message: 'The image is required.' }); - } - if (!description) { - return res.status(400).json({ message: 'The description is required.' }); - } - if (!category) { - return res.status(400).json({ message: 'The category is required.' }); - } - - // Generate a new post - const newPosts = { - id: Date.now().toString(), // Convert id to string to match the value in get by id endpoint - title, - image, - description, - category - }; - // Add the new post to our array - posts.push(newPosts); - - // Return the created post with a 201 status code - res.status(201).json(newPosts); -}); +router.post('/', postController.createPost); // Create comments -router.post('/:id/comments', (req, res) => { - const { id } = req.params - const { author, content } = req.body; - - if (!author) { - return res.status(400).json({ message: 'The author is required.' }); - } - if (!content) { - return res.status(400).json({ message: 'The content is required.' }); - } - - // Generate a new comment - const addComment = { - id, - author, - content - }; - // Add the new comment to our array - posts.push(addComment); - - // Return the created comment with a 201 status code - res.status(201).json(addComment); -}); +router.post('/:id/comments', postController.createComments); // Update posts by id -router.patch('/:id', (req, res) => { - const { id } = req.params - - const postbyId = posts.findIndex((p) => p.id === id); - - if (postbyId === -1) { - // If we don't find the posts return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - } - - // Generate a copy of our posts - const updatedPost = { ...posts[postbyId] }; - const { title } = req.body; - - if (title) { - updatedPost.title = title; - } - - /// Update the posts in our array - posts[postbyId] = updatedPost; - - // Return the updated post with a 201 status code - res.status(201).json(updatedPost); -}); +router.patch('/:id', postController.updatePostById); // Delete post by id -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 post 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); - - // Return a 204 status code - res.status(204).send(); -}); +router.delete('/:id', postController.deletePost); export default router; From 27ad597887869c28513dea48ffcc70db25ed360e Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Mon, 19 Aug 2024 17:48:16 -0600 Subject: [PATCH 3/7] add comment --- apps/api/src/controllers/post.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/apps/api/src/controllers/post.ts b/apps/api/src/controllers/post.ts index d9d5e7fa..b2acbc6c 100644 --- a/apps/api/src/controllers/post.ts +++ b/apps/api/src/controllers/post.ts @@ -1,4 +1,5 @@ const posts = []; +const comments = []; export const getByPost = (id: string) => { return posts.find((p) => p.id === id); @@ -63,7 +64,8 @@ const createPost = (req, res) => { title, image, description, - category + category, + comments: [] }; // Add the new post to our array posts.push(newPosts); @@ -76,6 +78,8 @@ const createPost = (req, res) => { const createComments = (req, res) => { const { id } = req.params const { author, content } = req.body; + const postID = getByPost(id); + const addCommentOnPost = { ...posts[postID] } if (!author) { return res.status(400).json({ message: 'The author is required.' }); @@ -91,7 +95,10 @@ const createComments = (req, res) => { content }; // Add the new comment to our array - posts.push(addComment); + addCommentOnPost.comments.push(addComment.id) + comments.push(addComment); + + posts[postID] = addCommentOnPost; // Return the created comment with a 201 status code res.status(201).json(addComment); From af6ed0dd2687c084588dbfef62bd3816588b4f56 Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Thu, 29 Aug 2024 15:41:40 -0600 Subject: [PATCH 4/7] agrega modelos y ajusta controller de comentarios --- apps/api/src/controllers/post.ts | 42 +++++++++++++++----------------- apps/api/src/models/posts.ts | 14 +++++++++++ 2 files changed, 34 insertions(+), 22 deletions(-) create mode 100644 apps/api/src/models/posts.ts diff --git a/apps/api/src/controllers/post.ts b/apps/api/src/controllers/post.ts index b2acbc6c..6ee76f90 100644 --- a/apps/api/src/controllers/post.ts +++ b/apps/api/src/controllers/post.ts @@ -1,5 +1,8 @@ -const posts = []; -const comments = []; +import { RequestHandler } from 'express'; +import { Post, Comment } from '../models/posts'; + +const posts: Post[] = []; +const comments: Comment[] = []; export const getByPost = (id: string) => { return posts.find((p) => p.id === id); @@ -10,13 +13,13 @@ export const getPostbyCategory = (category: string) => { }; // Get all posts -const getAllPosts = (req, res) => { +const getAllPosts: RequestHandler = (req, res) => { // Return all the posts with a 200 status code res.status(200).json(posts); }; // Get post by category -const getPostByCategory = (req, res) => { +const getPostByCategory: RequestHandler = (req, res) => { const { category } = req.params; const post = getPostbyCategory(category); @@ -29,7 +32,7 @@ const getPostByCategory = (req, res) => { }; // Get post by id -const getPostById = (req, res) => { +const getPostById: RequestHandler = (req, res) => { const { id } = req.params; const post = getByPost(id); @@ -42,7 +45,7 @@ const getPostById = (req, res) => { }; // Create post -const createPost = (req, res) => { +const createPost: RequestHandler = (req, res) => { const { title, image, description, category } = req.body; if (!title) { @@ -75,11 +78,9 @@ const createPost = (req, res) => { }; // Create comments -const createComments = (req, res) => { +const createComments: RequestHandler = (req, res) => { const { id } = req.params const { author, content } = req.body; - const postID = getByPost(id); - const addCommentOnPost = { ...posts[postID] } if (!author) { return res.status(400).json({ message: 'The author is required.' }); @@ -88,24 +89,21 @@ const createComments = (req, res) => { return res.status(400).json({ message: 'The content is required.' }); } - // Generate a new comment - const addComment = { - id, - author, - content - }; - // Add the new comment to our array - addCommentOnPost.comments.push(addComment.id) - comments.push(addComment); + const newComment: Comment = { + id: id, + author: author, + content: content + } - posts[postID] = addCommentOnPost; + // Add the new comment to our array + posts[id].comments.push(newComment) // Return the created comment with a 201 status code - res.status(201).json(addComment); + res.status(201).json(newComment); }; // Update posts by id -const updatePostById = (req, res) => { +const updatePostById: RequestHandler = (req, res) => { const { id } = req.params const postbyId = posts.findIndex((p) => p.id === id); @@ -131,7 +129,7 @@ const updatePostById = (req, res) => { }; // Delete post by id -const deletePost = (req, res) => { +const deletePost: RequestHandler = (req, res) => { // Retrieve the id from the route params const { id } = req.params; // Retrieve the index of the post in the array diff --git a/apps/api/src/models/posts.ts b/apps/api/src/models/posts.ts new file mode 100644 index 00000000..5bf6d31e --- /dev/null +++ b/apps/api/src/models/posts.ts @@ -0,0 +1,14 @@ +export type Comment = { + id: string; + author: string; + content: string; +}; + +export type Post = { + id: string; + title: string; + image: string; + description: string; + category: string; + comments: Comment[]; +}; From a5c77ab5bf08fc72f8f2b5b7d27e53147d970817 Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Thu, 29 Aug 2024 15:43:06 -0600 Subject: [PATCH 5/7] comentar comment --- apps/api/src/controllers/post.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/src/controllers/post.ts b/apps/api/src/controllers/post.ts index 6ee76f90..379d1d84 100644 --- a/apps/api/src/controllers/post.ts +++ b/apps/api/src/controllers/post.ts @@ -2,7 +2,7 @@ import { RequestHandler } from 'express'; import { Post, Comment } from '../models/posts'; const posts: Post[] = []; -const comments: Comment[] = []; +// const comments: Comment[] = []; export const getByPost = (id: string) => { return posts.find((p) => p.id === id); From 497d4f23e3a3c3580efdb9f50f9b7beb8b5a83b7 Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Thu, 29 Aug 2024 16:44:13 -0600 Subject: [PATCH 6/7] sea agrega lo del mongo --- apps/api/.env | 2 +- apps/api/src/controllers/post.ts | 180 +++++++++++-------------------- apps/api/src/models/comment.ts | 26 +++++ apps/api/src/models/posts.ts | 50 +++++++-- 4 files changed, 129 insertions(+), 129 deletions(-) create mode 100644 apps/api/src/models/comment.ts diff --git a/apps/api/.env b/apps/api/.env index ecaf5a7c..ddeb6216 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://jesypame321:dWDVZGFQjLRAAyAA@cluster0.bcndr.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 diff --git a/apps/api/src/controllers/post.ts b/apps/api/src/controllers/post.ts index 379d1d84..b798d40a 100644 --- a/apps/api/src/controllers/post.ts +++ b/apps/api/src/controllers/post.ts @@ -1,151 +1,95 @@ import { RequestHandler } from 'express'; -import { Post, Comment } from '../models/posts'; - -const posts: Post[] = []; -// const comments: Comment[] = []; - -export const getByPost = (id: string) => { - return posts.find((p) => p.id === id); -}; - -export const getPostbyCategory = (category: string) => { - return posts.find((p) => p.category === category); -}; +import Post from '../models/posts'; +import Comment from '../models/comment'; // Get all posts -const getAllPosts: RequestHandler = (req, res) => { +const getAllPosts: RequestHandler = async (req, res) => { // Return all the posts with a 200 status code - res.status(200).json(posts); + try { + const posts = await Post.find(); + return res.status(200).json(posts); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); + } }; // Get post by category -const getPostByCategory: RequestHandler = (req, res) => { - const { category } = req.params; - - const post = getPostbyCategory(category); - - if (!post) { - return res.status(404).json({ message: 'Post not found by category' }); +const getPostByCategory: RequestHandler = async (req, res) => { + const { categoryId } = req.params; + try { + const posts = await Post.find({ category: categoryId }); + return res.status(200).json(posts); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); } - - res.status(200).json(post); }; // Get post by id -const getPostById: RequestHandler = (req, res) => { +const getPostById: RequestHandler = async (req, res) => { const { id } = req.params; - - const post = getByPost(id); - - if (!post) { - return res.status(404).json({ message: 'Post not found' }); + try { + const post = await Post.findById(id); + return res.status(200).json(post); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); } - - res.status(200).json(post); }; // Create post -const createPost: RequestHandler = (req, res) => { - const { title, image, description, category } = req.body; - - if (!title) { - return res.status(400).json({ message: 'The title is required.' }); - } - if (!image) { - return res.status(400).json({ message: 'The image is required.' }); +const createPost: RequestHandler = async (req, res) => { + const postData = req.body; + try { + const newPost = await Post.create(postData); + return res.status(201).json(newPost); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); } - if (!description) { - return res.status(400).json({ message: 'The description is required.' }); - } - if (!category) { - return res.status(400).json({ message: 'The category is required.' }); - } - - // Generate a new post - const newPosts = { - 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(newPosts); - - // Return the created post with a 201 status code - res.status(201).json(newPosts); }; // Create comments -const createComments: RequestHandler = (req, res) => { - const { id } = req.params - const { author, content } = req.body; - - if (!author) { - return res.status(400).json({ message: 'The author is required.' }); - } - if (!content) { - return res.status(400).json({ message: 'The content is required.' }); - } - - const newComment: Comment = { - id: id, - author: author, - content: content +const createComments: RequestHandler = async (req, res) => { + const { id } = req.params; + const commentData = req.body; + try { + const post = await Post.findById(id); + const comment = await Comment.create(commentData); + post.comments.push(comment._id); + await post.save(); + return res.status(201).json(comment); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); } - - // Add the new comment to our array - posts[id].comments.push(newComment) - - // Return the created comment with a 201 status code - res.status(201).json(newComment); }; // Update posts by id -const updatePostById: RequestHandler = (req, res) => { - const { id } = req.params - - const postbyId = posts.findIndex((p) => p.id === id); - - if (postbyId === -1) { - // If we don't find the posts return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); - } - - // Generate a copy of our posts - const updatedPost = { ...posts[postbyId] }; - const { title } = req.body; - - if (title) { - updatedPost.title = title; +const updatePostById: RequestHandler = async (req, res) => { + const { id } = req.params; + const newPostData = req.body; + try { + const modifiedPost = await Post.findByIdAndUpdate(id, newPostData, { new: true, runValidators: true }); + return res.status(200).json(modifiedPost); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); } - - /// Update the posts in our array - posts[postbyId] = updatedPost; - - // Return the updated post with a 201 status code - res.status(201).json(updatedPost); }; // Delete post by id -const deletePost: RequestHandler = (req, res) => { - // Retrieve the id from the route params +const deletePost: RequestHandler = async (req, res) => { 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 post return a 404 status code with a message - return res.status(404).json({ message: 'Post not found' }); + try { + const deletedPost = await Post.findByIdAndDelete(id, {}); + await Comment.deleteMany({ _id: { $in: deletedPost.comments } }); + return res.status(200).json(deletedPost); + } catch (error) { + const { message } = error; + return res.status(500).json({ message }); } - - // Remove the post from the array - posts.splice(postIndex, 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..83c87bba --- /dev/null +++ b/apps/api/src/models/comment.ts @@ -0,0 +1,26 @@ +import mongoose, { Document, Schema } from 'mongoose'; + +interface IComment extends Document { + author: string; + content: string; +} + +export const commentSchema = new Schema( + { + author: { + type: String, + required: [true, 'Author is required'] + }, + content: { + type: String, + required: [true, 'Content is missing'] + } + }, + { + timestamps: true + } +); + +const Comment = mongoose.model('Comment', commentSchema); + +export default Comment; diff --git a/apps/api/src/models/posts.ts b/apps/api/src/models/posts.ts index 5bf6d31e..e743c2d2 100644 --- a/apps/api/src/models/posts.ts +++ b/apps/api/src/models/posts.ts @@ -1,14 +1,44 @@ -export type Comment = { - id: string; - author: string; - content: string; -}; - -export type Post = { - id: string; +import mongoose, { Document, Schema } from 'mongoose'; + +interface IPost extends Document { title: string; image: string; description: string; category: string; - comments: Comment[]; -}; + comments: string[]; +} + +export const postSchema = new Schema( + { + title: { + type: String, + required: [true, 'Title is required'], + maxlength: [256, 'title should be no greater than 256 characters'] + }, + image: { + type: String, + required: [true, 'image URL is missing'] + }, + description: { + type: String, + required: [true, 'Description is required'] + }, + category: { + type: String, + required: [true, 'Category Id is missing'] + }, + comments: [ + { + type: mongoose.Schema.Types.ObjectId, + ref: 'Comment' + } + ] + }, + { + timestamps: true + } +); + +const Post = mongoose.model('Post', postSchema); + +export default Post; From 578c17dc03df800107fd9fe192616893e1a6b62d Mon Sep 17 00:00:00 2001 From: Jessica Perez Date: Thu, 29 Aug 2024 17:02:43 -0600 Subject: [PATCH 7/7] ocultar credenciales por mensaje en Git --- apps/api/.env | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/api/.env b/apps/api/.env index ddeb6216..fd64c678 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://jesypame321:dWDVZGFQjLRAAyAA@cluster0.bcndr.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 +MONGO_URL=[YOUR_MONGO_CONNECTION_STRING_HERE]