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
59 changes: 59 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,62 @@ 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.query.courseUUID;
const userUUID = req.query.userUUID;
if (!courseUUID || !userUUID) {
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);
let userObj = await user.getUserById(userUUID);
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.query.courseUUID;
const userUUID = req.query.userUUID;
if (!courseUUID || !userUUID) {
res.status(422).json({
status: 422,
error: "Missing parameter: courseUUID or userUUID"
});
return;
};

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

if (courseObj.getInstructorList().indexOf(userObj.getUUID()) != -1) {
res.status(200).send("Instructor"); // send something else??
Copy link
Collaborator

Choose a reason for hiding this comment

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

(nitpick) Delete the comments

} else if (courseObj.getStudentList().indexOf(userObj.getUUID()) != -1) {
res.status(200).send("Student"); // send something else??
} else {
res.status(200).send("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
40 changes: 40 additions & 0 deletions 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?
Copy link
Collaborator

Choose a reason for hiding this comment

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

await shouldn't be necessary UNLESS you want to wait for the "this.push()" to finish being executed in the incrementScore method. I don't think it'll be necessary though since it'll be faster if we let the two updates to Firebase happen at the same time (feel free to delete the comment as well)

Copy link
Collaborator

Choose a reason for hiding this comment

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

Unrelated to implementation but what do you think about having each post collect the users that like a post (I believe we could do this implementation wise by adding a user_list to post) but do you think we should do that or is it better to make likes anonymous (maybe we can ask the team about this too)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think collecting the users that like a post would be a good idea. It's easy to implement and offers us more options in the future so I think it's a good idea

await this.push();
}

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

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

removeLikedComment = async (commentId) => {
this.props.likedCommentList.splice(likedCommentList.indexOf(commentId), 1);
let comment = await getCommentById(commentId);
comment.decrementScore(); // await?
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,8 @@ class User {
postList: this.props.postList,
commentList: this.props.commentList,
followingList: this.props.followingList,
likedPostList: this.props.likedPostList,
likedCommentList: this.props.likedCommentList,
icon: this.props.icon
});
}
Expand All @@ -109,6 +147,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 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