-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from AveselsJS/Following-and-followers
Following and followers
- Loading branch information
Showing
10 changed files
with
230 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
$(document).ready(() => { | ||
|
||
if(selectedTab === "followers") { | ||
loadFollowers(); | ||
} | ||
else { | ||
loadFollowing(); | ||
} | ||
}); | ||
|
||
function loadFollowers() { | ||
$.get(`/api/users/${profileUserId}/followers`, results => { | ||
outputUsers(results.followers, $(".resultsContainer")); | ||
}) | ||
} | ||
|
||
function loadFollowing() { | ||
$.get(`/api/users/${profileUserId}/following`, results => { | ||
outputUsers(results.following, $(".resultsContainer")); | ||
}) | ||
} | ||
|
||
function outputUsers(results, container) { | ||
container.html(""); | ||
|
||
results.forEach(result => { | ||
const html = createUserHtml(result, true); | ||
container.append(html); | ||
}); | ||
|
||
if(results.length == 0) { | ||
container.append("<span class='noResults'>Ещё нет ниодной записи</span>") | ||
} | ||
} | ||
|
||
function createUserHtml(userData, showFollowButton) { | ||
|
||
const name = userData.firstName + " " + userData.lastName; | ||
|
||
return `<div class='user'> | ||
<div class='userImageContainer'> | ||
<img src='${userData.profilePic}'> | ||
</div> | ||
<div class='userDetailsContainer'> | ||
<div class='header'> | ||
<a href='/profile/${userData.username}'>${name}</a> | ||
<span class='username'>@${userData.username}</span> | ||
</div> | ||
</div> | ||
</div>`; | ||
} |
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,59 @@ | ||
const express = require('express'); | ||
const bodyParser = require('body-parser'); | ||
const User = require('../../schemas/UserSchema') | ||
const Post = require('../../schemas/PostSchema'); | ||
const app = express(); | ||
const router = express.Router(); | ||
|
||
router.put("/:userId/follow", async (req, res, next) => { | ||
|
||
let userId = req.params.userId; | ||
let user = await User.findById(userId); | ||
|
||
if (user == null) return res.sendStatus(404); | ||
|
||
const isFollowing = user.followers && user.followers.includes(req.session.user._id); | ||
|
||
const option = isFollowing ? "$pull" : "$addToSet"; | ||
|
||
req.session.user = await User.findByIdAndUpdate(req.session.user._id, { [option]: { following: userId }}, {new: true}) | ||
.catch(err => { | ||
console.log(err); | ||
res.status(400); | ||
}) | ||
|
||
User.findByIdAndUpdate(userId, { [option]: { followers: req.session.user._id }}) | ||
.catch(err => { | ||
console.log(err); | ||
res.status(400); | ||
}) | ||
|
||
res.status(200).send(req.session.user) | ||
}) | ||
|
||
router.get("/:userId/following", async (req, res, next) => { | ||
User.findById(req.params.userId) | ||
.populate("following") | ||
.then(results => { | ||
res.status(200).send(results) | ||
}) | ||
.catch(error => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}) | ||
}) | ||
|
||
router.get("/:userId/followers", async (req, res, next) => { | ||
User.findById(req.params.userId) | ||
.populate("followers") | ||
.then(results => { | ||
res.status(200).send(results) | ||
}) | ||
.catch(error => { | ||
console.log(error); | ||
res.sendStatus(400); | ||
}) | ||
}) | ||
|
||
|
||
module.exports = router; |
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,18 @@ | ||
extends layouts/main-layout.pug | ||
|
||
block content | ||
if !profileUser | ||
span.errorMessage Такого пользователя не существует или он не был найден. Пожалуйста укажите верный никнейм пользователя. | ||
else | ||
script. | ||
const profileUserId = '!{profileUser._id}' | ||
const selectedTab = '!{selectedTab}' | ||
|
||
.tabsContainer | ||
+createTab("Подписки", `/profile/${profileUser.username}/following`, selectedTab == 'following') | ||
+createTab("Подписчики", `/profile/${profileUser.username}/followers`, selectedTab == 'followers') | ||
|
||
.resultsContainer | ||
|
||
block scripts | ||
script(src="/js/followersAndFollowing.js") |
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