Skip to content

Commit

Permalink
Feature #166405806 List user's bookmared articles
Browse files Browse the repository at this point in the history
  • Loading branch information
mwibutsa committed Jun 3, 2019
1 parent 813e11e commit 6347486
Show file tree
Hide file tree
Showing 29 changed files with 424 additions and 365 deletions.
195 changes: 111 additions & 84 deletions controllers/article.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions controllers/articlecomment.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ArticleComment {
const newComment = {
userid: req.user,
articleid,
comment: commentBody
comment: commentBody,
};
let comment = await ArticleCommentModel.create(newComment);
const author = await userModel.findOne({ attributes: ['id', 'username', 'bio', 'image'], where: { id: newComment.userid } });
Expand Down Expand Up @@ -80,7 +80,7 @@ class ArticleComment {
try {
let comment = await ArticleCommentModel.update(
{ comment: commentBody },
{ where: { id: commentid }, returning: true }
{ where: { id: commentid }, returning: true },
);
[, [comment]] = comment;
const author = await userModel.findOne({ attributes: ['id', 'username', 'bio', 'image'], where: { id: req.user } });
Expand Down
22 changes: 11 additions & 11 deletions controllers/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,37 @@ class Search {
attributes: { exclude: ['id', 'slug', 'title', 'body', 'authorid', 'views', 'image', 'createdAt', 'updatedAt'] },
where: {
taglist: {
[Op.contains]: [tag]
[Op.contains]: [tag],
},
}
},
});
const searchAuthor = await UserModel.findAll({
attributes: { exclude: ['id', 'password', 'provider', 'provideruserid', 'verified', 'inapp_notifications', 'email_notifications', 'role', 'createdAt', 'updatedAt'] },
where: {
[Op.or]: {
firstname: { [Op.like]: `%${author}%` }, lastname: { [Op.like]: `%${author}%` }, username: { [Op.like]: `%${author}%` }, email: { [Op.like]: `%${author}%` }, bio: { [Op.like]: `%${author}%` }, image: { [Op.like]: `%${author}%` }
}
}
firstname: { [Op.like]: `%${author}%` }, lastname: { [Op.like]: `%${author}%` }, username: { [Op.like]: `%${author}%` }, email: { [Op.like]: `%${author}%` }, bio: { [Op.like]: `%${author}%` }, image: { [Op.like]: `%${author}%` },
},
},
});

const searchArticle = await articleModel.findAll({
attributes: { exclude: ['id', 'provider', 'provideruserid', 'password', 'createdAt', 'updatedAt'] },
where: {
[Op.or]: {
title: { [Op.like]: `%${keyword}%` }, slug: { [Op.like]: `%${keyword}%` }, body: { [Op.like]: `%${keyword}%` }, description: { [Op.like]: `%${keyword}%` },
}
}
},
},
});
const searchUser = await UserModel.findAll({
attributes: { exclude: ['id', 'provider', 'provideruserid', 'password', 'createdAt', 'updatedAt'] },
where: {
[Op.or]: {
firstname: { [Op.like]: `%${keyword}%` }, lastname: { [Op.like]: `%${keyword}%` }, email: { [Op.like]: `%${keyword}%` }, username: { [Op.like]: `%${keyword}%` }, bio: { [Op.like]: `%${keyword}%` }
}
}
firstname: { [Op.like]: `%${keyword}%` }, lastname: { [Op.like]: `%${keyword}%` }, email: { [Op.like]: `%${keyword}%` }, username: { [Op.like]: `%${keyword}%` }, bio: { [Op.like]: `%${keyword}%` },
},
},
});
return res.status(200).json({
searchArticle, searchUser, searchTag, searchAuthor
searchArticle, searchUser, searchTag, searchAuthor,
});
} catch (error) {
logError(error);
Expand Down
115 changes: 43 additions & 72 deletions controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const {
followers: followersModel,
notifications: notificationModel,
articlereadingstats: ArticleReadingStats,
roleassignment: AssignRoleModel
roleassignment: AssignRoleModel,
} = model;

/**
Expand All @@ -45,18 +45,18 @@ class User {
static async signUpWithEmail(req, res) {
const data = req.body;
const newUser = {
...data
...data,
};
// check if the user does not already exist
const emailUsed = await UserModel.findOne({
where: {
email: newUser.email
}
email: newUser.email,
},
});
const userNameUsed = await UserModel.findOne({
where: {
username: newUser.username
}
username: newUser.username,
},
});
const uniqueEmailUsername = helper.handleUsed(emailUsed, userNameUsed);
if (uniqueEmailUsername === true) {
Expand All @@ -66,24 +66,17 @@ class User {
const verification = {
userid: result.id,
hash: verificationHash,
status: 'Pending'
status: 'Pending',
};
await UserVerificationModel.create(verification);

let userAccount = select.pick(result, [
'id',
'firstname',
'lastname',
'username',
'email',
'image'
]);
let userAccount = select.pick(result, ['id', 'firstname', 'lastname', 'username', 'email', 'image']);
const token = helper.generateToken(userAccount);
userAccount = select.pick(result, ['username', 'email', 'bio', 'image']);
return helper.authenticationResponse(res, token, userAccount);
}
return res.status(400).json({
error: uniqueEmailUsername
error: uniqueEmailUsername,
});
}

Expand All @@ -99,31 +92,24 @@ class User {
where: {
[Op.or]: [
{
email
email,
},
{
username: email
}
]
}
username: email,
},
],
},
});
// verify password
if (user && helper.comparePassword(password, user.password)) {
// return user and token
let userAccount = select.pick(user, [
'id',
'firstname',
'lastname',
'username',
'email',
'image'
]);
let userAccount = select.pick(user, ['id', 'firstname', 'lastname', 'username', 'email', 'image']);
const token = helper.generateToken(userAccount);
userAccount = select.pick(user, ['username', 'email', 'bio', 'image']);
return helper.authenticationResponse(res, token, userAccount);
}
return res.status(401).json({
error: 'Invalid username or password'
error: 'Invalid username or password',
});
}

Expand All @@ -139,12 +125,12 @@ class User {
if (blacklisting) {
return res.status(200).send({
status: 200,
message: 'Successfully logged out'
message: 'Successfully logged out',
});
}
return res.status(500).send({
status: 500,
error: 'Something went wrong'
error: 'Something went wrong',
});
}

Expand Down Expand Up @@ -173,17 +159,10 @@ class User {
bio: req.user.bio,
image: req.user.image,
provider: req.user.provider,
provideruserid: req.user.provideruserid
provideruserid: req.user.provideruserid,
};
const result = await UserModel.socialUsers(ruser);
let userAccount = select.pick(result, [
'id',
'firstname',
'lastname',
'username',
'email',
'image'
]);
let userAccount = select.pick(result, ['id', 'firstname', 'lastname', 'username', 'email', 'image']);
const token = helper.generateToken(userAccount);
userAccount = select.pick(result, ['username', 'email', 'bio', 'image']);
return res.redirect(`${process.env.FRONTEND_URL}?token=${token}`);
Expand Down Expand Up @@ -213,7 +192,7 @@ class User {
const result = await new Mailer(email, 'Password reset', resetLink).sender();
res.status(202).json({
message: result,
email
email,
});
}
}
Expand All @@ -240,7 +219,7 @@ class User {
const result = await UserModel.resetpassword(password, decode.id);
notify.emit('resetpassword', decode.id);
res.status(201).json({
data: result
data: result,
});
} catch (error) {
return res.status(400).json({ message: error.message });
Expand Down Expand Up @@ -287,27 +266,18 @@ class User {
...data,
email: email.length ? email : data.email,
username: username.length ? username : data.username,
image: data.image
image: data.image,
},
{
where: {
username
username,
},
returning: true
}
returning: true,
},
);
res
.status(201)
.json(
select.pick(updatedUser[1][0], [
'firstname',
'lastname',
'email',
'username',
'bio',
'image'
])
);
.json(select.pick(updatedUser[1][0], ['firstname', 'lastname', 'email', 'username', 'bio', 'image']));
}

/**
Expand All @@ -329,12 +299,12 @@ class User {
res.status(201).json({
status: 201,
message: 'followed',
follower: followedUser.username
follower: followedUser.username,
});
} else {
res.status(409).json({
status: 409,
message: "you can't follow you self"
message: "you can't follow you self",
});
}
} catch (error) {
Expand All @@ -361,12 +331,12 @@ class User {
res.status(201).json({
status: 201,
message: 'unfollowed',
follower: unfollowedUser.username
follower: unfollowedUser.username,
});
} else {
res.status(409).json({
status: 409,
message: "you can't unfollow you self"
message: "you can't unfollow you self",
});
}
} catch (error) {
Expand All @@ -388,12 +358,12 @@ class User {
res.status(200).json({
status: 200,
count: result.length,
notifications: result
notifications: result,
});
} catch (error) {
res.status(400).json({
status: 400,
error: 'bad request'
error: 'bad request',
});
}
}
Expand All @@ -411,12 +381,12 @@ class User {
const notification = await notificationModel.read(notificationId, userId);
res.status(201).json({
status: 201,
notification
notification,
});
} catch (error) {
res.status(400).json({
status: 400,
error: 'bad request'
error: 'bad request',
});
}
}
Expand All @@ -434,12 +404,12 @@ class User {
const followers = await followersModel.followers(profile.dataValues.id);
res.status(200).json({
status: 200,
followers: followers.length
followers: followers.length,
});
} catch (error) {
res.status(400).json({
status: 400,
error: 'bad request'
error: 'bad request',
});
}
}
Expand All @@ -457,12 +427,12 @@ class User {
const followings = await followingModel.followings(profile.dataValues.id);
res.status(200).json({
status: 200,
following: followings.length
following: followings.length,
});
} catch (error) {
res.status(400).json({
status: 400,
error: 'bad request'
error: 'bad request',
});
}
}
Expand All @@ -483,12 +453,12 @@ class User {
const following = await followingModel.following(user.id, profileId);
res.status(200).json({
status: 200,
response: following === null ? 'false' : 'true'
response: following === null ? 'false' : 'true',
});
} catch (error) {
res.status(400).json({
status: 400,
error: 'incorrent profile'
error: 'incorrent profile',
});
}
}
Expand Down Expand Up @@ -607,9 +577,10 @@ class User {
const { username } = req.params;
const profile = await UserModel.findOne({
where: { username },
include: [{ model: followersModel }, { model: followingModel }]
include: [{ model: followersModel }, { model: followingModel }],
});
if (profile) {
delete profile.dataValues.password;
res.status(200).json({ status: 200, profile });
} else {
res.status(404).json({ stats: 404, error: 'This page is not available' });
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
extended: true,
}));
app.use((express.json()));
app.use(session({
secret: process.env.secretKey,
resave: false,
saveUninitialized: true
saveUninitialized: true,
}));

app.use(passport.initialize());
Expand Down
Loading

0 comments on commit 6347486

Please sign in to comment.