Routes for adding users, teams and events added. Image upload to be done - #1
Routes for adding users, teams and events added. Image upload to be done#1KavyaChopra04 wants to merge 18 commits into
Conversation
Add files via upload
ACES-ACM-IITD
left a comment
There was a problem hiding this comment.
Also please install prettier as a dev-dependency and format the code.
| @@ -0,0 +1,9 @@ | |||
| doctype 5 | |||
There was a problem hiding this comment.
What exactly is the purpose of this file?
There was a problem hiding this comment.
Nothing, was trying stuff out with image upload. Removed
| const curr_user=await User.findOne({name : req.body.name}); | ||
| if(curr_user==null) | ||
| { | ||
| res.json("current user not registered: error") |
There was a problem hiding this comment.
Please send a proper json format when using res.json. You could maintain a fixed schema while returning. For example to send an error response:
res.json(
{
err: true,
msg: "error message here"
}
)
To send an ok response -
res.json({err: false, msg: ""})
|
|
||
| } | ||
| }) | ||
| app.route(adminAuth, '/addteamhere'). |
There was a problem hiding this comment.
Rename this to /addteam. Also add a GET endpoint that renders the appropriate form for all theee viz '/addteam', '/addevent', '/adduser'
| var mongoose = require("mongoose"), | ||
| Schema = mongoose.Schema, | ||
| bcrypt = require("bcrypt"), | ||
| SALT_WORK_FACTOR = 10; |
There was a problem hiding this comment.
What is the function of SALT_WORK_FACTOR exactly?
There was a problem hiding this comment.
Password hashing using bcrypt. This is the number of rounds it uses to hash passwords I believe.
| const multer = require('multer'); | ||
| const storage = multer.diskStorage({ | ||
| destination: function(req, file, cb) { | ||
| cb(null, '/public/uploads/'); | ||
| }, | ||
| filename: function(req, file, cb) { | ||
| cb(null, new Date().toISOString() + file.originalname); | ||
| } | ||
| }); | ||
|
|
||
| const fileFilter = (req, file, cb) => { | ||
| // reject a file | ||
| if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') { | ||
| cb(null, true); | ||
| } else { | ||
| cb(null, false); | ||
| } | ||
| }; | ||
|
|
||
| const upload = multer({ | ||
| storage: storage, | ||
| fileFilter: fileFilter | ||
| }); | ||
|
|
There was a problem hiding this comment.
If this is not working please refer to the following page from node docs utilising node-formidable. I have tried it out, and file upload works with this approach. Make sure you uninstall multer before switching to formidable.
ACES-ACM-IITD
left a comment
There was a problem hiding this comment.
What is the install.Unix.sh file?
| .get(auth, function(req, res) { | ||
| res.render(path + '/public/user'); | ||
| }); |
There was a problem hiding this comment.
Let's do away with the GET and POST endpoints of '/user'. We don't need it anymore.
There was a problem hiding this comment.
Again, was just trying out stuff with image upload. Removed.
| post(async function(req,res){ | ||
| try { | ||
| const yearobj = await Team.findOne({ year: req.body.year}); | ||
| const curr_user=await User.findOne({name : req.body.name}); |
There was a problem hiding this comment.
Change this to lookup using email
ACES-ACM-IITD
left a comment
There was a problem hiding this comment.
Do format the entire codebase once done, and also strip unnecessary console.log messages.
| const user = new User(fields); | ||
| var oldPath = files.profile_pic.filepath; | ||
| console.log(__dirname) | ||
| var newPath = path1.join(__dirname, '/../../upload')+ '/'+fields.username |
There was a problem hiding this comment.
Please use path.join consistently to join path items.
| var newPath = path1.join(__dirname, '/../../upload')+ '/'+fields.username | |
| var newPath = path1.join(__dirname, '/../../upload', fields.username) |
| console.log(files) | ||
|
|
||
| const user = new User(fields); | ||
| var oldPath = files.profile_pic.filepath; |
There was a problem hiding this comment.
What exactly is oldPath ? If I am posting a file for the first time what would be the value?
| var oldPath = files.profile_pic.filepath; | ||
| console.log(__dirname) | ||
| var newPath = path1.join(__dirname, '/../../upload')+ '/'+fields.username | ||
| var rawData = fs.readFileSync(oldPath) |
There was a problem hiding this comment.
Followup to the earlier question: Is this guaranteed to work? Does it throw any error if say oldPath does not exist on the filesystem?
| console.log(fields) | ||
| console.log(files) |
There was a problem hiding this comment.
I believe these were debugging aids? If so please strip these console.log statements before the final merge.
| var oldPath = (files.gallery)[i].filepath; | ||
| var newPath = path1.join(__dirname, '/../../upload')+ '/'+(files.gallery)[i].originalFilename | ||
| var rawData = fs.readFileSync(oldPath) | ||
| fs.writeFile(newPath, rawData, function(err){ | ||
| if(err){console.log(err); return res.json({err: true})} | ||
|
|
||
| }) |
There was a problem hiding this comment.
I think the same snippet was used in /addevent as well? Maybe abstract it out as a function?
| const curr_user=await User.findOne({email : req.body.email}); | ||
| if(curr_user==null) | ||
| { | ||
| res.json("current user not registered: error") |
| let sz= files.gallery.length | ||
| console.log("size is ", sz) | ||
| for(let i=0;i<sz;i++) |
There was a problem hiding this comment.
Also you can use javascript's forEach method. It looks more elegant and alleviates worries about sizes and lengths 😄
| app.route("/addteam").get(adminAuth, async function(req,res){ | ||
| const teamobj=await Team.find() | ||
| const sz=teamobj.length | ||
| const cleanedobj = [] | ||
| console.log(teamobj) | ||
| for(let i=0;i<sz;i++) | ||
| { | ||
| const newobj={} | ||
| newobj.year=teamobj[i].year | ||
| newobj.positions=[] | ||
| let j= teamobj[i].positions.length | ||
| for(let k =0;k<j;k++) | ||
| { | ||
| const secnewobj={} | ||
| secnewobj.position_name=teamobj[i].positions[k].position_name; | ||
| secnewobj.people=[] | ||
| let l = teamobj[i].positions[k].people.length; | ||
| console.log("iterating over ", teamobj[i].year, " and " , teamobj[i].positions[k].position_name, " with strength ", l) | ||
| for(let p=0;p<l;p++) | ||
| { | ||
| console.log("searching for ", teamobj[i].positions[k].people[p]) | ||
| const user = await User.findOne({_id: teamobj[i].positions[k].people[p]}) | ||
| console.log(user) | ||
| if(user!=null) | ||
| { | ||
| secnewobj.people.push(user.name) | ||
| } | ||
|
|
||
| } | ||
| newobj.positions.push(secnewobj) | ||
| } | ||
| cleanedobj.push(newobj) | ||
| } | ||
| res.json(cleanedobj) | ||
| }) |
There was a problem hiding this comment.
I think I am having trouble understanding the code.
Why do we need so many loops? This endpoint is supposed to add a member to the team right? So you just need to find a team object in the given year, with the given position_name. I believe there are inbuilt methods in mongoose to execute such complicated queries. Do check them out, If not then you will have to change the schema so that you are able to directly fetch the required object with mongoose API. We don't want to write too many for loops as it makes the code incomprehensible.
No description provided.