-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c532aa0
commit 86a3fae
Showing
19 changed files
with
953 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import cloudinary from 'cloudinary'; | ||
|
||
import models from '../models/index'; | ||
import sendProfile from '../../helpers/results/send.profile'; | ||
import status from '../../helpers/constants/status.codes'; | ||
import errorMessages from '../../helpers/constants/error.messages'; | ||
|
||
const { Article, User } = models; | ||
|
||
/** | ||
* containing all user's | ||
* profile model controller(update, getUserProfile, read) | ||
* @export | ||
* @class ProfileController | ||
*/ | ||
export default class ProfileController { | ||
/** | ||
* | ||
* update current user's profile | ||
* @static | ||
* @param {*} req The request Object | ||
* @param {*} res The response Object | ||
* @memberof ProfileController | ||
* @return {Object} The response Object | ||
*/ | ||
static async update(req, res) { | ||
let profileImage = 'noimage.jpg'; | ||
const { | ||
country, firstName, lastName, address, gender, phoneNumber, bio | ||
} = req.body; | ||
// upload the image to cloudinary | ||
if (req.file) { | ||
const image = await cloudinary.v2.uploader.upload(req.file.path); | ||
profileImage = image.secure_url; | ||
} | ||
const updatedProfile = await User.update({ | ||
country, firstName, lastName, bio, address, gender, profileImage, phoneNumber | ||
}, { | ||
where: { | ||
id: req.user.id | ||
} | ||
}); | ||
return sendProfile(res, status.OK, 'updated correctly', updatedProfile); | ||
} | ||
|
||
/** | ||
* | ||
* get another user's profile | ||
* @static | ||
* @param {*} req The request Object | ||
* @param {*} res The response Object | ||
* @memberof ProfileController | ||
* @return {Object} The response Object | ||
*/ | ||
static async read(req, res) { | ||
const { username } = req.params; | ||
const { limit, offset } = req.query; | ||
// check if the user exists and send his profile | ||
try { | ||
const defaultPaginationLimit = 10; | ||
const paginationLimit = limit || defaultPaginationLimit; | ||
const user = await User.findOne({ | ||
where: { | ||
username | ||
}, | ||
attributes: ['username', 'email', 'profileImage', 'bio', 'firstName', 'lastName', 'address', 'phoneNumber', 'gender', 'country', 'facebook', 'twitter'], | ||
include: [{ | ||
model: Article, | ||
offset, | ||
limit: paginationLimit, | ||
}], | ||
}); | ||
if (user) return sendProfile(res, status.OK, undefined, user); | ||
return sendProfile(res, status.NOT_FOUND, errorMessages.noUser); | ||
} catch (err) { | ||
return res.status(status.SERVER_ERROR).json(err); | ||
} | ||
} | ||
} |
File renamed without changes.
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
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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Router } from 'express'; | ||
import profileController from '../controllers/profileController'; | ||
import upload from '../../middlewares/upload'; | ||
import validateInputs from '../../middlewares/validations/body.inputs'; | ||
|
||
|
||
const profileRouter = new Router(); | ||
const fields = ['country', 'lastName', 'firstName', 'address', 'gender', 'phoneNumber', 'bio', 'profileImage', 'facebook', 'twitter']; | ||
|
||
profileRouter.patch('/', upload.single('profileImage'), validateInputs(true, 'profile', fields), | ||
profileController.update); | ||
profileRouter.get('/:username', profileController.read); | ||
|
||
export default profileRouter; |
Oops, something went wrong.