-
Notifications
You must be signed in to change notification settings - Fork 53
Node/jesus.saucedo #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jSaucedoOjeda
wants to merge
2
commits into
gus-code:node/jesus.saucedo
Choose a base branch
from
jSaucedoOjeda:node/jesus.saucedo
base: node/jesus.saucedo
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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] | ||
| MONGO_URL=mongodb+srv://jesussaucedo:1234@cluster0.pbp95rs.mongodb.net/?retryWrites=true&w=majority&appName=Cluster0 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -98,4 +98,4 @@ export default { | |
| createCategory, | ||
| updateCategory, | ||
| deleteCategory | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import Post from '../models/post'; | ||
| import Comment from '../models/comment'; | ||
|
|
||
| const getPosts = async (req, res) => { | ||
| try { | ||
| const posts = await Post.find().populate('category').exec(); | ||
| res.status(200).json(posts); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| const getPostsByCategory = async (req, res) => { | ||
| const { category } = req.params; | ||
| try { | ||
| const posts = await Post.find({ category }).populate('category').exec(); | ||
| res.status(200).json(posts); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| const getPostById = async (req, res) => { | ||
| const { id } = req.params; | ||
| try { | ||
| const post = await Post.findById(id).populate('category').populate('comments').exec(); | ||
| if (!post) { | ||
| return res.status(404).send({ message: 'Post not found' }); | ||
| } | ||
| res.status(200).json(post); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| const createPost = async (req, res) => { | ||
| try { | ||
| const post = await Post.create(req.body); | ||
| res.status(201).json(post); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| const createPostComment = async (req, res) => { | ||
| const { id } = req.params; | ||
| try { | ||
| const post = await Post.findById(id); | ||
|
|
||
| if (!post) { | ||
| return res.status(404).send({ message: 'Post not found' }); | ||
| } | ||
| const newComment = await Comment.create(req.body); | ||
| post.comments.push(newComment._id); | ||
| await post.save(); | ||
| res.status(201).json(newComment); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| const updatePost = async (req, res) => { | ||
| const { id } = req.params; | ||
| try { | ||
| const post = await Post.findByIdAndUpdate(id, req.body, { new: true }); | ||
| if (!post) { | ||
| return res.status(404).send({ message: 'Post not found' }); | ||
| } | ||
| res.status(200).json(post); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| const deletePost = async (req, res) => { | ||
| const { id } = req.params; | ||
| try { | ||
| const post = await Post.findByIdAndDelete(id); | ||
| if (!post) { | ||
| return res.status(404).send({ message: 'Post not found' }); | ||
| } | ||
| res.status(204).json(post); | ||
| } catch (error) { | ||
| const { message } = error; | ||
| res.status(500).json({ message }); | ||
| } | ||
| }; | ||
|
|
||
| export default { | ||
| getPosts, | ||
| getPostsByCategory, | ||
| getPostById, | ||
| createPost, | ||
| createPostComment, | ||
| updatePost, | ||
| deletePost | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,4 +21,4 @@ export const verifyToken = (req, res, next) => { | |
|
|
||
| export default { | ||
| verifyToken | ||
| }; | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import mongoose, { Document, Schema } from 'mongoose'; | ||
|
|
||
| interface IComment extends Document { | ||
| author: string; | ||
| content: string; | ||
| } | ||
|
|
||
| const commentSchema = new Schema<IComment>( | ||
| { | ||
| author: String, | ||
| content: String | ||
| }, | ||
| { | ||
| timestamps: true | ||
| } | ||
| ); | ||
|
|
||
| const Comment = mongoose.model<IComment>('Comment', commentSchema); | ||
|
|
||
| export default Comment; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import mongoose, { Document, Schema } from 'mongoose'; | ||
|
|
||
| import Comment from './comment'; | ||
|
|
||
| interface IPost extends Document { | ||
| title: string; | ||
| image: string; | ||
| description: string; | ||
| category: mongoose.Types.ObjectId; | ||
| comments: mongoose.Types.ObjectId[]; | ||
| } | ||
|
|
||
| const postSchema = new Schema<IPost>( | ||
| { | ||
| 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: Schema.Types.ObjectId, | ||
| ref: 'Category', | ||
| required: [true, 'Property is required'] | ||
| }, | ||
| comments: [ | ||
| { | ||
| type: Schema.Types.ObjectId, | ||
| ref: 'Comment' | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| timestamps: true | ||
| } | ||
| ); | ||
|
|
||
| postSchema.pre('findOneAndDelete', async function (next) { | ||
| try { | ||
| const postToDelete = await this.model.findById(this.getFilter()); | ||
| await Comment.deleteMany({ _id: { $in: postToDelete.comments } }); | ||
| next(); | ||
| } catch (error) { | ||
| console.log(error, 'ERROR!!!'); | ||
| next(error); | ||
| } | ||
| }); | ||
|
|
||
| const Post = mongoose.model<IPost>('Post', postSchema); | ||
|
|
||
| export default Post; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,26 @@ | ||
| export type User = { | ||
| import mongoose, { Document, Schema } from 'mongoose'; | ||
|
|
||
| interface IUser extends Document { | ||
| username: string; | ||
| password: string; | ||
| }; | ||
| } | ||
|
|
||
| const userSchema = new Schema<IUser>( | ||
| { | ||
| username: { | ||
| type: String, | ||
| required: [true, 'Property is required'] | ||
| }, | ||
| password: { | ||
| type: String, | ||
| required: [true, 'Property is required'] | ||
| } | ||
| }, | ||
| { | ||
| timestamps: true | ||
| } | ||
| ); | ||
|
|
||
| const User = mongoose.model<IUser>('User', userSchema); | ||
|
|
||
| export default User; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import express from 'express'; | ||
| import postController from '../controllers/post'; | ||
|
|
||
| const router = express.Router(); | ||
|
|
||
| router.get('/', postController.getPosts); | ||
|
|
||
| router.get('/category/:category', postController.getPostsByCategory); | ||
|
|
||
| router.get('/:id', postController.getPostById); | ||
|
|
||
| router.post('/', postController.createPost); | ||
|
|
||
| router.post('/:id/comments', postController.createPostComment); | ||
|
|
||
| router.patch('/:id', postController.updatePost); | ||
|
|
||
| router.delete('/:id', postController.deletePost); | ||
|
|
||
| export default router; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to remove this from the PR