Skip to content
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
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions starter-project-backend-g31/src/app.ts
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
132 changes: 66 additions & 66 deletions starter-project-backend-g31/src/controllers/article.ts
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);
}
56 changes: 44 additions & 12 deletions starter-project-backend-g31/src/controllers/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

@RiyadHassen RiyadHassen Aug 4, 2022

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.

}catch(err){
res.status(404).send("Error");
}
Expand All @@ -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'
Copy link
Collaborator

Choose a reason for hiding this comment

The 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');
Copy link
Collaborator

Choose a reason for hiding this comment

The 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,
Expand All @@ -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");
}
Expand All @@ -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);
Expand Down
Loading