-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
π update functionality for Admin profile (#931)
* update functionality for Admin profile * update functionality for Admin profile * update functionality admin page * update functionality admin page * #update functionality admin page * #update functionality admin page * #update functionality admin page * update admin api fix
- Loading branch information
1 parent
03ab618
commit 9c7e4b9
Showing
8 changed files
with
290 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,42 @@ | ||
const Admin = require('../../models/Admin'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
module.exports =async (req, res) => { | ||
const admin = await Admin.findById(req.params.id); | ||
|
||
if (!admin) { | ||
return res.status(404).json({ error: 'Admin not found' }); | ||
} | ||
|
||
// Delete previous image if it exists | ||
if (admin.image && admin.image!=="undefined" && req.file?.path) { | ||
if (fs.existsSync(path.join(__dirname,'..' ,'..','..', admin.image))) { | ||
fs.unlinkSync(path.join(__dirname,'..' ,'..','..', admin.image)); | ||
} | ||
} | ||
|
||
try { | ||
const updatedAdmin = await Admin.findByIdAndUpdate( | ||
req.params.id, | ||
{ | ||
$set: req.body, | ||
}, | ||
{ new: true } | ||
); | ||
res.status(200).json(updatedAdmin); | ||
const updateFields = { | ||
firstName:req.body.firstName, | ||
lastName:req.body.lastName, | ||
contact:req.body.contact, | ||
username:req.body.username | ||
}; | ||
if (req.file?.path) { | ||
updateFields.image = req.file.path; | ||
}else{ | ||
updateFields.image = admin.image; | ||
} | ||
const updatedAdmin = await Admin.findByIdAndUpdate( | ||
req.params.id, | ||
{ $set: updateFields }, | ||
{ new: true } | ||
); | ||
return res.status(200).json({"Req.Body":updateFields,"updatedDoc":updatedAdmin}); | ||
} catch (err) { | ||
res.status(500).json(err); | ||
return res.status(500).json(err); | ||
} | ||
|
||
res.send('Done'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.