-
Notifications
You must be signed in to change notification settings - Fork 7
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
interlink comment with user #59
Open
Feruz2
wants to merge
5
commits into
master
Choose a base branch
from
feruz.comment-user.interlink
base: master
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 1 commit
Commits
Show all changes
5 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 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,27 +1,27 @@ | ||
|
||
import express, {Application, Request, Response, NextFunction, json} from 'express'; | ||
import dotenv from 'dotenv'; | ||
import { router } from './routes/routes'; | ||
import ratingRoute from "./routes/rating" | ||
import article from './routes/article'; | ||
import { commentRoute } from './routes/comment'; | ||
import { userRoute } from './routes/user'; | ||
import { indexRoute } from './routes/index'; | ||
|
||
|
||
dotenv.config(); | ||
|
||
const app: Application = express(); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
app.use("/api/user-profiles", router) | ||
app.use('/api/articles', article); | ||
app.use("/comment", commentRoute); | ||
app.use("/ratings", ratingRoute); | ||
app.use('/users', userRoute); | ||
app.get('/', (req:Request, res:Response) => { | ||
res.send('Welcome to our Blog App...'); | ||
}); | ||
app.use("/", indexRoute); | ||
|
||
export default app | ||
import express, {Application, Request, Response, NextFunction, json} from 'express'; | ||
import dotenv from 'dotenv'; | ||
import { router } from './routes/routes'; | ||
import ratingRoute from "./routes/rating" | ||
import article from './routes/article'; | ||
import { commentRoute } from './routes/comment'; | ||
import { userRoute } from './routes/user'; | ||
import { indexRoute } from './routes/index'; | ||
dotenv.config(); | ||
const app: Application = express(); | ||
app.use(express.urlencoded({ extended: true })); | ||
app.use(express.json()); | ||
app.use("/api/user-profiles", router) | ||
app.use('/api/articles', article); | ||
app.use("/comment", commentRoute); | ||
app.use("/ratings", ratingRoute); | ||
app.use('/users', userRoute); | ||
app.get('/', (req:Request, res:Response) => { | ||
res.send('Welcome to our Blog App...'); | ||
}); | ||
app.use("/", indexRoute); | ||
export default app |
This file contains 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,66 +1,66 @@ | ||
import mongoose from 'mongoose'; | ||
import {Request, Response} from 'express'; | ||
import {Article} from '../models/article'; | ||
|
||
|
||
export const getMany = async (req: Request, res: Response) => { | ||
const article = await Article.find(); | ||
res.send(article); | ||
} | ||
|
||
export const getOne = async (req: Request, res: Response) => { | ||
if (!mongoose.Types.ObjectId.isValid(req.params.id)) | ||
return res.status(404).send('Invalid ID'); | ||
|
||
const article = await Article.findById(req.params.id); | ||
if (!article) return res.status(404).send('Article not found'); | ||
|
||
res.send(article); | ||
} | ||
|
||
export const createArticle = async(req: Request, res: Response) => { | ||
try{ | ||
let article = new Article({ | ||
author: req.body.author, | ||
content: req.body.content, | ||
rating: req.body.rating, | ||
comment: req.body.comment | ||
}); | ||
const result = await article.save().catch((err) => res.status(400).send("Bad request")); | ||
res.json(result); | ||
}catch(err){ | ||
return res.status(404); | ||
} | ||
} | ||
|
||
export const updateOne = async (req: Request, res: Response) => { | ||
if (!mongoose.Types.ObjectId.isValid(req.params.id)) | ||
return res.status(404).send('Invalid ID'); | ||
|
||
let article = await Article.findById(req.params.id); | ||
if (!article) return res.status(404).send('Article not found'); | ||
|
||
article.set({ | ||
author: req.body.author, | ||
content: req.body.content, | ||
rating: req.body.rating, | ||
comment: req.body.comment, | ||
postdate: req.body.postdate | ||
}); | ||
|
||
await article.save() | ||
.then(() => res.status(200).send(article)) | ||
.catch((err: Error) => res.status(400).send(err.message)); | ||
|
||
} | ||
|
||
export const deleteOne = async (req: Request, res: Response) => { | ||
if (!mongoose.Types.ObjectId.isValid(req.params.id)) | ||
return res.status(404).send('Invalid ID'); | ||
|
||
const article = await Article.findByIdAndRemove(req.params.id); | ||
if (!article) return res.status(404).send('Article not found'); | ||
|
||
res.send(article); | ||
} | ||
|
||
import mongoose from 'mongoose'; | ||
import {Request, Response} from 'express'; | ||
import {Article} from '../models/article'; | ||
export const getMany = async (req: Request, res: Response) => { | ||
const article = await Article.find(); | ||
res.send(article); | ||
} | ||
export const getOne = async (req: Request, res: Response) => { | ||
if (!mongoose.Types.ObjectId.isValid(req.params.id)) | ||
return res.status(404).send('Invalid ID'); | ||
const article = await Article.findById(req.params.id); | ||
if (!article) return res.status(404).send('Article not found'); | ||
res.send(article); | ||
} | ||
export const createArticle = async(req: Request, res: Response) => { | ||
try{ | ||
let article = new Article({ | ||
author: req.body.author, | ||
content: req.body.content, | ||
rating: req.body.rating, | ||
comment: req.body.comment | ||
}); | ||
const result = await article.save().catch((err) => res.status(400).send("Bad request")); | ||
res.json(result); | ||
}catch(err){ | ||
return res.status(404); | ||
} | ||
} | ||
export const updateOne = async (req: Request, res: Response) => { | ||
if (!mongoose.Types.ObjectId.isValid(req.params.id)) | ||
return res.status(404).send('Invalid ID'); | ||
let article = await Article.findById(req.params.id); | ||
if (!article) return res.status(404).send('Article not found'); | ||
article.set({ | ||
author: req.body.author, | ||
content: req.body.content, | ||
rating: req.body.rating, | ||
comment: req.body.comment, | ||
postdate: req.body.postdate | ||
}); | ||
await article.save() | ||
.then(() => res.status(200).send(article)) | ||
.catch((err: Error) => res.status(400).send(err.message)); | ||
} | ||
export const deleteOne = async (req: Request, res: Response) => { | ||
if (!mongoose.Types.ObjectId.isValid(req.params.id)) | ||
return res.status(404).send('Invalid ID'); | ||
const article = await Article.findByIdAndRemove(req.params.id); | ||
if (!article) return res.status(404).send('Article not found'); | ||
res.send(article); | ||
} | ||
This file contains 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 |
---|---|---|
|
@@ -7,9 +7,13 @@ import {Article} from'../models/article'; | |
|
||
export async function getAllComments(req: Request, res: Response){ | ||
try{ | ||
const comment = await Comment.find(); | ||
const comment = await Comment.find().populate({ | ||
path: 'author', | ||
}); | ||
|
||
// const ans = await comment; | ||
res.status(200).json(comment); | ||
|
||
// console.log(comment) | ||
}catch(err){ | ||
res.status(404).send("Error"); | ||
} | ||
|
@@ -18,20 +22,35 @@ export async function getAllComments(req: Request, res: Response){ | |
export async function getCommentById(req: Request,res: Response){ | ||
|
||
try{ | ||
const comment = await Comment.findById(req.params.commentId); | ||
res.status(200).json(comment); | ||
|
||
const comment = await Comment.findById(req.params.commentId) | ||
.populate([ | ||
{ | ||
path: 'author ', | ||
select: 'name -_id' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we need to populate picture of a user also |
||
}, | ||
{ | ||
path :'article', | ||
select: 'content -_id' | ||
} | ||
]); | ||
// console.log(comment) | ||
// const ans = await comment.populate('author'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove debugging statments |
||
|
||
res.status(200).send(comment); | ||
}catch(err){ | ||
|
||
res.status(404).send("Error"); | ||
} | ||
} | ||
|
||
export async function addComment(req: Request,res:Response){ | ||
|
||
const user = await userModel.findById(req.params.userId); | ||
if (!user) return res.status(404).send("User not found"); | ||
|
||
const article = await Article.findById(req.params.articleId); | ||
if (!article) return res.status(404).send("Article not found"); | ||
|
||
|
||
const comment = new Comment({ | ||
author: req.params.userId, | ||
|
@@ -54,8 +73,15 @@ export async function deleteCommentById(req:Request,res:Response){ | |
if (!article) return res.status(404).send("Article not found"); | ||
|
||
try{ | ||
const deletedComment = await Comment.remove({ _id:req.params.commentId}); | ||
res.status(200).json(deletedComment); | ||
const comment = await Comment.findById(req.params.commentId); | ||
|
||
if (comment.author == req.params.userId){ | ||
const deletedComment = await Comment.remove({ _id:req.params.commentId}); | ||
res.status(200).json(deletedComment); | ||
} | ||
else{ | ||
res.status(401).send("You are not alowed to delete this comment"); | ||
} | ||
}catch(err){ | ||
res.status(404).send("Error"); | ||
} | ||
|
@@ -68,12 +94,18 @@ export async function updateCommentById(req:Request,res:Response){ | |
if (!article) return res.status(404).send("Article not found"); | ||
|
||
try{ | ||
const comment = await Comment.findById(req.params.commentId); | ||
|
||
const updatedComment = await Comment.updateOne({ _id:req.params.commentId}, | ||
{ $set:{ | ||
description: req.body.description | ||
}}); | ||
res.status(200).json(updatedComment); | ||
if (comment.author == req.params.userId){ | ||
const updatedComment = await Comment.updateOne({ _id:req.params.commentId}, | ||
{ $set:{ | ||
description: req.body.description | ||
}}); | ||
res.status(200).json(updatedComment); | ||
} | ||
else{ | ||
res.status(401).send("You are not alowed to edit this comment"); | ||
} | ||
}catch(err){ | ||
|
||
res.status(404).send(err); | ||
|
Oops, something went wrong.
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.
remove debugging statements, plus pull the master branch to resolve conflicts.