Skip to content

Commit

Permalink
Merge branch 'main' into TJA/Likes
Browse files Browse the repository at this point in the history
  • Loading branch information
carolrs committed May 16, 2023
2 parents 3fac572 + 0d27392 commit 81e34ab
Show file tree
Hide file tree
Showing 23 changed files with 31,708 additions and 106 deletions.
55 changes: 55 additions & 0 deletions api/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,61 @@ const PostsController = {
});
});
},

// Pablo UPDATE version 3 6:40pm Monday 15th May
Update: async (req, res) => {
try {
const post_id = req.params.id;
console.log(post_id)
const update = req.body;
const updatedPost = await Post.findOneAndUpdate({ _id: post_id }, update, { new: true });
const token = await TokenGenerator.jsonwebtoken(req.user_id);
res.json({token})
// res.json({mssg: "You have updated this post!", token: token, updatedPost: updatedPost})
} catch (err) {
res.status(500).json({error: "messed up again!"})
}
},

// Tom, Pablo and Ana version 2 Monday pm
// Update: (req, res) => {
// const post_id = req.params.id;
// const update = req.body;
// Post.findOneAndUpdate({ _id: post_id }, update, { new: true },
// (async (updatedDoc) => {
// if (!updatedDoc) {
// res.status(400);
// }

// // res.status(204).json({ message: 'OK' });
// }))
// const token = await TokenGenerator.jsonwebtoken(req.user_id);
// console.log(token)
// console.log("HELLLO")
// res.status(204).json({ message: 'OK', token: token });
// ;
// },

// Ana, Tom and Pablo version 1 Monday pm
// Update: async (req, res) => {
// console.log("Hello")
// const post_id = req.params.id;
// const update = req.body;
// console.log(req.user_id)
// try {
// const updatedDoc = await Post.findOneAndUpdate({ _id: post_id }, update, {
// new: true,
// });
// const token = await TokenGenerator.jsonwebtoken(req.user_id);
// console.log(req.user_id)
// console.log(token)
// res.status(204).json({ message: 'OK', token: token });

// } catch (err) {
// res.status(500); //server error
// console.log(err);
// }
// },
};

module.exports = PostsController;
13 changes: 10 additions & 3 deletions api/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
const User = require("../models/user");
const { validateEmail } = require("../helpers/validation");

const UsersController = {
Create: (req, res) => {
Create: (req, res) => {
const user = new User(req.body);
user.save((err) => {
if(!validateEmail(user.email)){
res.status(400).json({
message: 'Invalid email address!'
})
} else {
user.save((err) => {
if (err) {
res.status(400).json({message: 'Bad request'})
res.status(400).json({message: 'Bad request'})
} else {
res.status(201).json({ message: 'OK' });
}
});
}
},
};

Expand Down
5 changes: 5 additions & 0 deletions api/helpers/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports.validateEmail = (email) => {
//regex to validate email
return String(email).toLocaleLowerCase()
.match(/^([a-z\d\.-]+)@([a-z\d-]+)\.([a-z]{2,12})(\.[a-z]{1,12})?$/)
}
1 change: 1 addition & 0 deletions api/models/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mongoose = require("mongoose");

const PostSchema = new mongoose.Schema({
message: String,
comments: [], // AddComments: Added comments (empty array) to schema
authorUserID: {
type: mongoose.Schema.Types.ObjectId,
required: true,
Expand Down
Loading

0 comments on commit 81e34ab

Please sign in to comment.