Skip to content

Routes for adding users, teams and events added. Image upload to be done - #1

Open
KavyaChopra04 wants to merge 18 commits into
ACES-ACM-IITD:masterfrom
KavyaChopra04:master
Open

Routes for adding users, teams and events added. Image upload to be done#1
KavyaChopra04 wants to merge 18 commits into
ACES-ACM-IITD:masterfrom
KavyaChopra04:master

Conversation

@KavyaChopra04

Copy link
Copy Markdown

No description provided.

@ACES-ACM-IITD ACES-ACM-IITD left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Also please install prettier as a dev-dependency and format the code.

Comment thread views/upload_form.pug Outdated
@@ -0,0 +1,9 @@
doctype 5

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What exactly is the purpose of this file?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Nothing, was trying stuff out with image upload. Removed

Comment thread app/routes/index.js Outdated
const curr_user=await User.findOne({name : req.body.name});
if(curr_user==null)
{
res.json("current user not registered: error")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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: ""})

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

Comment thread app/routes/index.js Outdated

}
})
app.route(adminAuth, '/addteamhere').

@ACES-ACM-IITD ACES-ACM-IITD Aug 30, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Rename this to /addteam. Also add a GET endpoint that renders the appropriate form for all theee viz '/addteam', '/addevent', '/adduser'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

Comment thread app/models/users.js
var mongoose = require("mongoose"),
Schema = mongoose.Schema,
bcrypt = require("bcrypt"),
SALT_WORK_FACTOR = 10;

@ACES-ACM-IITD ACES-ACM-IITD Aug 30, 2022

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What is the function of SALT_WORK_FACTOR exactly?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Password hashing using bcrypt. This is the number of rounds it uses to hash passwords I believe.

Comment thread app/routes/index.js Outdated
Comment on lines +18 to +41
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
});

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

@ACES-ACM-IITD ACES-ACM-IITD left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What is the install.Unix.sh file?

Comment thread app/routes/index.js Outdated
Comment on lines 242 to 244
.get(auth, function(req, res) {
res.render(path + '/public/user');
});

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Let's do away with the GET and POST endpoints of '/user'. We don't need it anymore.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Again, was just trying out stuff with image upload. Removed.

Comment thread app/routes/index.js Outdated
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});

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Change this to lookup using email

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Done

@ACES-ACM-IITD ACES-ACM-IITD left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Do format the entire codebase once done, and also strip unnecessary console.log messages.

Comment thread app/routes/index.js Outdated
const user = new User(fields);
var oldPath = files.profile_pic.filepath;
console.log(__dirname)
var newPath = path1.join(__dirname, '/../../upload')+ '/'+fields.username

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please use path.join consistently to join path items.

Suggested change
var newPath = path1.join(__dirname, '/../../upload')+ '/'+fields.username
var newPath = path1.join(__dirname, '/../../upload', fields.username)

Comment thread app/routes/index.js
console.log(files)

const user = new User(fields);
var oldPath = files.profile_pic.filepath;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

What exactly is oldPath ? If I am posting a file for the first time what would be the value?

Comment thread app/routes/index.js Outdated
var oldPath = files.profile_pic.filepath;
console.log(__dirname)
var newPath = path1.join(__dirname, '/../../upload')+ '/'+fields.username
var rawData = fs.readFileSync(oldPath)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Followup to the earlier question: Is this guaranteed to work? Does it throw any error if say oldPath does not exist on the filesystem?

Comment thread app/routes/index.js Outdated
Comment on lines +273 to +274
console.log(fields)
console.log(files)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I believe these were debugging aids? If so please strip these console.log statements before the final merge.

Comment thread app/routes/index.js Outdated
Comment on lines +319 to +325
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})}

})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I think the same snippet was used in /addevent as well? Maybe abstract it out as a function?

Comment thread app/routes/index.js Outdated
const curr_user=await User.findOne({email : req.body.email});
if(curr_user==null)
{
res.json("current user not registered: error")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Consistent error schema?

Comment thread app/routes/index.js Outdated
Comment on lines +315 to +317
let sz= files.gallery.length
console.log("size is ", sz)
for(let i=0;i<sz;i++)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Also you can use javascript's forEach method. It looks more elegant and alleviates worries about sizes and lengths 😄

Comment thread app/routes/index.js
Comment on lines +382 to +416
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)
})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

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.

@ACES-ACM-IITD ACES-ACM-IITD reopened this Sep 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants