Skip to content
This repository has been archived by the owner on Aug 14, 2020. It is now read-only.

Pramith/backend routes #61

Merged
merged 8 commits into from
May 20, 2020
65 changes: 65 additions & 0 deletions backend/controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const user = require("../models/User");
const course = require("../models/Course");

exports.addUser = async (req, res) => {
// Check that required data is given
Expand Down Expand Up @@ -48,4 +49,68 @@ exports.updateUser = async (req, res) => {
// Object of fields and values to update in the user object
const updateParams = req.body;
res.status(200).send("Updated user.");
};

// DEFAULTS TO ADDING USER AS A STUDENT
exports.addUserToCourse = async (req, res) => {
const courseUUID = req.params.courseId;
let userObj = req.user;
if (!courseUUID || !userObj) {
res.status(422).json({
status: 422,
error: "Missing parameter: courseUUID or userUUID"
});
return;
};


// Adds the user to the course as a student. If fails, responds with an error. Only works with users we've manually made
try {
let courseObj = await course.getCourseById(courseUUID);
await courseObj.addStudent(userObj.getUUID());
await userObj.addStudentCourse(courseObj.getUUID());
res.status(200).send("Added user to course.");
} catch (e) {
res.status(410).json({
status: 410,
error: e
});
};
};

exports.getUserType = async (req, res) => {
const courseUUID = req.params.courseId;
const userObj = req.user;

if (!courseUUID || !userObj) {
res.status(422).json({
status: 422,
error: "Missing parameter: courseUUID or user"
});
return;
};

// Grabs the user's type based on the courseUUID. If fails, responds with an error.
try {
const courseObj = await course.getCourseById(courseUUID);

if (courseObj.getInstructorList().indexOf(userObj.getUUID()) != -1) {
res.status(200).json({
type: "Instructor"
});
} else if (courseObj.getStudentList().indexOf(userObj.getUUID()) != -1) {
res.status(200).json({
type: "Student"
});
} else {
res.status(200).json({
error: "User not in this class"
});
}
} catch (e) {
res.status(410).json({
status: 410,
error: e
});
};
};
5 changes: 5 additions & 0 deletions backend/models/Comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ class Comment {
await this.push();
}

decrementScore = async () => {
this.props.score--;
await this.push();
}

setEndorsed = async (newValue) => {
this.props.isEndorsed = newValue;
await this.push();
Expand Down
2 changes: 1 addition & 1 deletion backend/models/Course.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Course {
}

getInstructorList() {
return this.props.instructorList;
return this.props.instructorList.slice(1, this.props.instructorList.length);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What was the purpose of this change?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rohith said that he and Pawan were going to initialize each list with a dummy string element to make sure Javascript knew that the list was a list of Strings. They wanted the list getters implemented this way to ignore that first dummy string

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh interesting okay I must not have caught that in a previous PR. Honestly that shouldn't be necessary but that could also be a refactor for later. Let's make a comment or something about that in the code to prevent future confusion.

}

getUUID() {
Expand Down
5 changes: 5 additions & 0 deletions backend/models/Post.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ class Post {
await this.push();
}

decrementScore = async () => {
this.props.score--;
await this.push();
}

setAnnouncement = async (newValue) => {
this.props.isAnnouncement = newValue;
await this.push();
Expand Down
42 changes: 41 additions & 1 deletion backend/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,34 @@ class User {
await this.push();
}

addLikedPost = async (postId) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future PR let's write tests for these to make sure that they work

this.props.likedPostList.push(postId);
let post = await getPostById(postId);
post.incrementScore();
await this.push();
}

removeLikedPost = async (postId) => {
this.props.likedPostList.splice(likedPostList.indexOf(postId), 1);
let post = await getPostById(postId);
post.decrementScore();
await this.push();
}

addLikedComment = async (commentId) => {
this.props.likedCommentList.push(commentId);
let comment = await getCommentById(commentId);
comment.incrementScore();
await this.push();
}

removeLikedComment = async (commentId) => {
this.props.likedCommentList.splice(likedCommentList.indexOf(commentId), 1);
let comment = await getCommentById(commentId);
comment.decrementScore();
await this.push();
}

getName() {
return this.props.name;
}
Expand Down Expand Up @@ -67,6 +95,14 @@ class User {
return this.props.followingList.slice(1, this.props.followingList.length);
}

getLikedPostList() {
return this.props.likedPostList.slice(1, this.props.likedPostList.length);
}

getLikedCommentList() {
return this.props.likedCommentList.slice(1, this.props.likedCommentList.length);
}

getIcon() {
return this.props.icon;
}
Expand All @@ -88,6 +124,9 @@ class User {
postList: this.props.postList,
commentList: this.props.commentList,
followingList: this.props.followingList,
// Add this ternary for new fields to prevent old data from crashing
likedPostList: this.props.likedPostList ? this.props.likedPostList : [],
likedCommentList: this.props.likedCommentList ? this.props.likedCommentList : [],
icon: this.props.icon
});
}
Expand All @@ -109,6 +148,8 @@ module.exports.pushUserToFirebase = (updateParams) => {
postList: ["dummy_post_id"],
commentList: ["dummy_comment_id"],
followingList: ["dummy_post_id"],
likedPostList: ["dummy_post_id"],
likedCommentList: ["dummy_comment_id"],
icon: "anonymous.jpg"
});
resolve("Everything worked");
Expand All @@ -128,7 +169,6 @@ getUserById = async (uuid) => {
return new Promise((resolve, reject) => {
ref.once("value", function(snapshot) {
const r = new User(snapshot.val());
//console.log(r.props.name);
resolve(r);
}, function (errorObject) {
reject(errorObject);
Expand Down
4 changes: 4 additions & 0 deletions backend/routes/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ router.route("/")
.get(userController.getUser) // Read
.put(userController.updateUser) // Update

// Matches with "/api/user/courseId"
router.route("/:courseId") // regex? how do we format the courseID
.post(userController.addUserToCourse)
.get(userController.getUserType)
module.exports = router;
4 changes: 2 additions & 2 deletions backend/test/courseTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('course', () => {
name: "cse110",
term: "sp20",
uuid: "course1",
instructorList: "user2",
studentList: "user1",
instructorList: ["user2"],
studentList: ["user1"],
tagList: ["dummy_tag"],
postList: ["dummy_post"],
}
Expand Down