-
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
[Backend] article-rating interlink #78
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import { Article } from "../models/article"; | ||
import { Rating } from "../models/rating"; | ||
|
||
export const getAllRatings = (req: any, res: any) => { | ||
|
@@ -49,9 +50,15 @@ export const getAllRatingsForAGivenArticleAndUser = async (req: any, res: any) = | |
|
||
export const createRating = async (req: any, res: any) => { | ||
try { | ||
// const article = await Article.findById(req.body.articleID) | ||
// article?.addRating(req.body.rating) | ||
// await article?.save() | ||
// console.log(article) | ||
const ratedBefore = await Rating.findOne({ articleID: req.body.articleID, userID: req.body.userID }); | ||
|
||
if (ratedBefore) { | ||
ratedBefore.rating = req.body.rating | ||
|
||
await ratedBefore.save() | ||
return res.send(ratedBefore) | ||
} | ||
|
@@ -61,6 +68,8 @@ export const createRating = async (req: any, res: any) => { | |
rating: req.body.rating, | ||
}); | ||
await newRating.save(); | ||
const article = await Article.findById(req.body.articleID) | ||
article?.addRating(req.body.rating) | ||
return res.status(201).send(newRating) | ||
} | ||
catch (error) { | ||
|
@@ -72,6 +81,10 @@ export const updateRating = async (req: any, res: any) => { | |
try { | ||
const rating = await Rating.findById({ _id: req.params.id }) | ||
if (!rating) return res.status(404).send("Rating not found to be updated") | ||
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. Change response message since this would be shown to users on the frontend. like.. "Rating not found!" |
||
const article = await Article.findOne({_id:rating.articleID}) | ||
article?.updateRate(rating.rating,req.body.rating); | ||
await article?.save() | ||
|
||
rating.rating = req.body.rating || rating.rating; | ||
await rating.save() | ||
return res.send(rating) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ import app from "./app" | |
import mongoose from 'mongoose'; | ||
|
||
const PORT = process.env.PORT || 8000 | ||
const DB_URI = process.env.MONGO_URI || "mongodb://localhost:27017/test"; | ||
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. No need to reveal database strings here. use env variables. |
||
const DB_URI = process.env.mongodb_url || "mongodb://localhost:27017/rating"; | ||
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. make the variable naming better and readable.. like MONGO_URI and MONGO_URI_TEST bla bla :) |
||
|
||
|
||
mongoose.connect(DB_URI, { | ||
|
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 status codes and response messages on all responses.