Skip to content

Commit

Permalink
on the path to a full server working. almost all endpoints functionin…
Browse files Browse the repository at this point in the history
…g. have to only test put, get, delete for comments.
  • Loading branch information
Alex-Lee-Myers committed Jan 26, 2022
1 parent 14a1178 commit 48c58cd
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 98 deletions.
150 changes: 94 additions & 56 deletions controllers/commentscontroller.js
Original file line number Diff line number Diff line change
@@ -1,101 +1,139 @@
const router = require('express').Router();
const { models } = require('../models');
const Express = require("express");
const router = Express.Router();
let validateJWT = require("../middleware/validate-session");
const { CommentsModel } = require('../models');

//! create a new comment for a specific video when the user submits a comment on the video when the user is logged in + validated
router.post("/:videoID", validateJWT, async (req, res) => {
const {
commentText
} =
req.body.comments;

const videoID = req.params.videoID;
const userId = req.user.id;
const commentDate = new Date();

try {
const { videoID } = req.params;
const { commentText } = req.body;
const commentDate = new Date();
const comment = await models.comments.create({
commentID: uuid.v4(),
commentText,
const commentSuccess = await CommentsModel.create
({
commentText: commentText,
commentDate: commentDate,
adminDelete: false,
badActor: false,
videopostVideoID: videoID,
userUuid: req.user.uuid
userId: userId
});
res.status(201).json({
message: "Comment created!",
comment: commentSuccess,
});
res.status(201).json(comment);
} catch (err) {
console.log(err);
res.status(500).json(err);
res.status(500).json({
messageError: `Error message is: ${err}`,
message: "Failed to create the Comment!",
});
}
});

//! update a comment for a specific video from a specific user when they're validated with ValidateJWT from validate-session.js
router.put("/:videoID/:commentID", validateJWT, async (req, res) => {
const { commentText } = req.body.comments;

const videoID = req.params.videopostVideoID;
const { commentID } = req.params;
const commentDate = new Date();

try {
const { videoID, commentID } = req.params;
const { commentText } = req.body;
const commentDate = new Date();
const comment = await models.comments.update({
commentID: commentID,
commentText,
commentDate: commentDate,
adminDelete: false,
badActor: false,
}, {
const commentSuccess = await CommentsModel.update
({
commentText: commentText,
commentDate: commentDate
},
{
where: {
commentID,
commentID: commentID
}
});
res.status(201).json(comment);
res.status(201).json({
message: "Comment updated!",
comment: commentSuccess,
});
} catch (err) {
console.log(err);
res.status(500).json(err);
res.status(500).json({
messageError: `Error message is: ${err}`,
message: "Failed to update the Comment!",
});
}
});

//! delete a comment for a specific video from a specific user when they're validated with ValidateJWT from validate-session.js
router.delete("/:videoID/:commentID", validateJWT, async (req, res) => {
const { commentID } = req.params;
const videoID = req.params.videopostVideoID;

try {
const { videoID, commentID } = req.params;
const comment = await models.comments.destroy({
const commentSuccess = await CommentsModel.destroy
({
where: {
commentID,
commentID: commentID,
videopostVideoID: videoID
}
});
res.status(201).json(comment);
res.status(201).json({
message: "Comment deleted!",
comment: commentSuccess,
});
} catch (err) {
console.log(err);
res.status(500).json(err);
res.status(500).json({
messageError: `Error message is: ${err}`,
message: "Failed to delete the Comment!",
});
}
});

//! get all comments for a specific video when the user is validated with ValidateJWT from validate-session.js
router.get("/:videoID", validateJWT, async (req, res) => {
const videoID = req.params.videopostVideoID;

try {
const { videoID } = req.params;
const comments = await models.comments.findAll({
const commentsSuccess = await CommentsModel.findAll
({
where: {
commentVideoID: videoID,
adminDelete: false,
badActor: false,
videpostVideoID: videoID
}
});
res.status(201).json(comments);
res.status(201).json({
message: "All comments for a specific video!",
comments: commentsSuccess,
});
} catch (err) {
console.log(err);
res.status(500).json(err);
res.status(500).json({
messageError: `Error message is: ${err}`,
message: "Failed to get all comments for a specific video!",
});
}
});

//! delete all of a users comments if the person deleting them is an admin
// router.delete("/:videoID", validateJWT, async (req, res) => {
// try {
// const { videoID } = req.params;
// const comments = await models.comments.destroy({
// where: {
// commentVideoID: videoID,
// }
// });
// res.status(201).json(comments);
// } catch (err) {
// console.log(err);
// res.status(500).json(err);
// }
// });
router.delete("/:userID", validateJWT, async (req, res) => {
const { userId } = req.params;

try {
const commentsSuccess = await CommentsModel.destroy
({
where: {
userID: userId
}
});
res.status(201).json({
message: "All comments for a specific user!",
comments: commentsSuccess,
});
} catch (err) {
res.status(500).json({
messageError: `Error message is: ${err}`,
message: "Failed to get all comments for a specific user!",
});
}
});

module.exports = router;
54 changes: 24 additions & 30 deletions controllers/usercontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,13 @@ router.post("/login", async (req, res) => {
message: "Failed to log user in",
});
}
});
});

//! GET UserInfo if they are logged in
router.get("userInfo/:id", validateJWT, async (req, res) => {
router.get("/:id", validateJWT, async (req, res) => {
console.log("UserInfo: ", req.user);
const { id } = req.params;

try {
const user = await UserModel.findOne({
where: {
Expand All @@ -122,51 +124,43 @@ router.get("userInfo/:id", validateJWT, async (req, res) => {
});

//TODO 1) update passphrase only if the user answers accountResetQuestion1 and accountResetQuestion2 correctly according to accountResetAnswer1 and accountResetAnswer2

//TODO 2) update username and/or email only if valid

router.put("userInfo/:id", validateJWT, async (req, res) => {
router.put("/passphrase/:id", validateJWT, async (req, res) => {
const { id } = req.params;
const {
username,
email,
passwordhash,
isAdmin,
accountResetQuestion1,
accountResetQuestion2,
accountResetAnswer1,
accountResetAnswer2,
} = req.body.user;

try {
const user = await UserModel.update(
{
username: username,
email: email,
passwordhash: passwordhash,
isAdmin: isAdmin,
accountResetQuestion1: accountResetQuestion1,
accountResetQuestion2: accountResetQuestion2,
accountResetAnswer1: accountResetAnswer1,
accountResetAnswer2: accountResetAnswer2,
// update username, email and passwordhash
const user = await UserModel.update(
{
username: username,
email: email,
passwordhash: passwordhash,
},
{
where: {
id: id,
},
{
where: {
id: id,
},
}
);
res.status(200).json({
message: "User successfully updated!",
user: user,
});
}
);
res.status(200).json({
message: "User successfully updated!",
user: user,
});
} catch (error) {
res.status(500).json({
message: "Failed to update user",
});
}
});

// if validateJWT is true, then we can delete the user
router.delete("userInfo/:id", validateJWT, async (req, res) => {
router.delete(":id", validateJWT, async (req, res) => {
const { id } = req.params;
try {
const user = await UserModel.destroy({
Expand Down
15 changes: 3 additions & 12 deletions controllers/videopostcontroller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ let validateJWT = require("../middleware/validate-session");
const { VideoPostModel } = require("../models");
const uuid = require("uuid");

console.log(VideoPostModel);

//! Create Video Post
router.post("/content/", validateJWT, async (req, res) => {
const {
Expand Down Expand Up @@ -116,16 +114,12 @@ router.put("/content/:userId/:videoID", validateJWT, async (req, res) => {
gameDate,
nbaSeason,
isPlayoffs,
clutch,
adminHighlighted,
adminDelete
clutch
} =
req.body.videopost;

//? VideoID is the primary key of the table. Needed to grab for the user's video post in the where clause.
const videoID = req.params.videoID;
const videoOwner = req.user.uuid;
const userId = req.params.userId;
const { userId, videoID } = req.params;

const updateVideoPost = await VideoPostModel.update(
{
Expand All @@ -138,14 +132,11 @@ router.put("/content/:userId/:videoID", validateJWT, async (req, res) => {
gameDate: gameDate,
nbaSeason: nbaSeason,
isPlayoffs: isPlayoffs,
clutch: clutch,
adminHighlighted: adminHighlighted,
adminDelete: adminDelete
clutch: clutch
},
{
where: {
videoID: videoID,
videoOwner: videoOwner,
userId: userId
}
}
Expand Down

0 comments on commit 48c58cd

Please sign in to comment.