Ajout initial des fichiers de la version mysal

This commit is contained in:
Rampeur
2025-08-08 18:34:15 +02:00
parent 29635cdf83
commit 9a534d5a17
30 changed files with 1381 additions and 0 deletions
+74
View File
@@ -0,0 +1,74 @@
const User = require("../models/User");
const asyncHandler = require("../middlewares/asyncHandler");
const ErrorResponse = require("../util/errorResponse");
module.exports.getProfile = asyncHandler(async (req, res, next) => {
const username = req.params.username;
const { loggedUser } = req;
const user = await User.findOne({ where: { username: username } });
if (!user) {
return next(new ErrorResponse(`User not found`, 404));
}
let isFollowing = false;
if (loggedUser) {
const followers = await user.getFollowers();
isFollowing = followers.some((user) => user.id === loggedUser.id);
}
const profile = {
username,
bio: user.bio,
image: user.image,
following: isFollowing,
};
res.status(200).json({ profile });
});
module.exports.followUser = asyncHandler(async (req, res, next) => {
const username = req.params.username;
const { loggedUser } = req;
const userToFollow = await User.findOne({ where: { username: username } });
if (!userToFollow) {
return next(new ErrorResponse(`User not found`, 404));
}
const currentUser = await User.findByPk(loggedUser.id);
await userToFollow.addFollower(currentUser);
const profile = {
username: username,
bio: userToFollow.dataValues.bio,
image: userToFollow.dataValues.image,
following: true,
};
res.status(200).json({ profile });
});
module.exports.unfollowUser = asyncHandler(async (req, res, next) => {
const username = req.params.username;
const { loggedUser } = req;
const userToUnfollow = await User.findOne({ where: { username: username } });
if (!userToUnfollow) {
return next(new ErrorResponse(`User not found`, 404));
}
const currentUser = await User.findByPk(loggedUser.id);
await userToUnfollow.removeFollower(currentUser);
const profile = {
username: username,
bio: userToUnfollow.dataValues.bio,
image: userToUnfollow.dataValues.image,
following: false,
};
res.status(200).json({ profile });
});