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

[Backend] article-rating interlink #78

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions starter-project-backend-g31/src/controllers/rating.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Article } from "../models/article";
import { Rating } from "../models/rating";

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.

export const getAllRatings = (req: any, res: any) => {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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) {
Expand All @@ -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")

Choose a reason for hiding this comment

The 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)
Expand Down
61 changes: 52 additions & 9 deletions starter-project-backend-g31/src/models/article.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import mongoose , { Schema, Document } from 'mongoose';

export interface IRatingScore{
[details: number]:number;
1:number;
2:number;
3:number;
4:number;
5:number;
};
export interface IArticle extends Document{
author:string,
content:string,
comment:string,
rating:string,
postdate:Date
rating:IRatingScore,
average:Number,
postdate:Date,
addRating(val:Number):void,
updateRate(prev:Number,current:Number):void
}


Expand All @@ -22,11 +32,20 @@ const articleSchema: Schema<IArticle> = new mongoose.Schema({
maxLength: 10000,
required: true,
},
average:{
type:Number,
default:0
},
rating: {
type: Number,
min: 0,
max: 5,
required: true
type:Object,
default:
{
1:0,
2:0,
3:0,
4:0,
5:0
},
},
comment: {
type: [ String ],
Expand All @@ -39,6 +58,30 @@ const articleSchema: Schema<IArticle> = new mongoose.Schema({
}
});



articleSchema.methods.addRating = function(val:number){
this.rating[val]+=1
this.average = averageRating(this)
this.markModified("rating")
this.save()
}
articleSchema.methods.updateRate = function(prev:number,current:number){
this.rating[prev]--;
this.rating[current]++;
this.average= averageRating(this)
this.markModified("rating")
this.save()
}
const averageRating = (article:IArticle)=>{
const average = (article.rating[1] * 1
+ article.rating[2] * 2
+ article.rating[3] * 3
+article.rating[4] * 4
+article.rating[5] * 5
) / (article.rating[1]
+article.rating[2]
+article.rating[3]
+article.rating[4]
+article.rating[5])
return average
}
export const Article = mongoose.model<IArticle>('Article', articleSchema);
2 changes: 1 addition & 1 deletion starter-project-backend-g31/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Choose a reason for hiding this comment

The 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";

Choose a reason for hiding this comment

The 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, {
Expand Down